From 7ab7941dddcfa005d7a758ce3dde0628c4e6134a Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Wed, 23 Dec 2020 17:15:55 +0100 Subject: [PATCH] sftpfs: fix race condition --- vfs/sftpfs.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vfs/sftpfs.go b/vfs/sftpfs.go index 6a6063c4..343a56a4 100644 --- a/vfs/sftpfs.go +++ b/vfs/sftpfs.go @@ -10,6 +10,7 @@ import ( "path" "path/filepath" "strings" + "sync" "time" "github.com/eikenb/pipeat" @@ -99,6 +100,7 @@ func (c *SFTPFsConfig) EncryptCredentials(additionalData string) error { // SFTPFs is a Fs implementation for SFTP backends type SFTPFs struct { + sync.Mutex connectionID string config *SFTPFsConfig sshClient *ssh.Client @@ -476,6 +478,9 @@ func (fs *SFTPFs) GetMimeType(name string) (string, error) { // Close the connection func (fs *SFTPFs) Close() error { + fs.Lock() + defer fs.Unlock() + var sftpErr, sshErr error if fs.sftpClient != nil { sftpErr = fs.sftpClient.Close() @@ -498,6 +503,9 @@ func (fs *SFTPFs) checkConnection() error { } func (fs *SFTPFs) createConnection() error { + fs.Lock() + defer fs.Unlock() + var err error clientConfig := &ssh.ClientConfig{ User: fs.config.Username, @@ -547,6 +555,10 @@ func (fs *SFTPFs) wait() { // we don't detect the event. fs.err <- fs.sftpClient.Wait() fsLog(fs, logger.LevelDebug, "sftp channel closed") + + fs.Lock() + defer fs.Unlock() + if fs.sshClient != nil { fs.sshClient.Close() }