sftpfs: fix race condition

This commit is contained in:
Nicola Murino
2020-12-23 17:15:55 +01:00
parent c69d63c1f8
commit 7ab7941ddd

View File

@@ -10,6 +10,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time" "time"
"github.com/eikenb/pipeat" "github.com/eikenb/pipeat"
@@ -99,6 +100,7 @@ func (c *SFTPFsConfig) EncryptCredentials(additionalData string) error {
// SFTPFs is a Fs implementation for SFTP backends // SFTPFs is a Fs implementation for SFTP backends
type SFTPFs struct { type SFTPFs struct {
sync.Mutex
connectionID string connectionID string
config *SFTPFsConfig config *SFTPFsConfig
sshClient *ssh.Client sshClient *ssh.Client
@@ -476,6 +478,9 @@ func (fs *SFTPFs) GetMimeType(name string) (string, error) {
// Close the connection // Close the connection
func (fs *SFTPFs) Close() error { func (fs *SFTPFs) Close() error {
fs.Lock()
defer fs.Unlock()
var sftpErr, sshErr error var sftpErr, sshErr error
if fs.sftpClient != nil { if fs.sftpClient != nil {
sftpErr = fs.sftpClient.Close() sftpErr = fs.sftpClient.Close()
@@ -498,6 +503,9 @@ func (fs *SFTPFs) checkConnection() error {
} }
func (fs *SFTPFs) createConnection() error { func (fs *SFTPFs) createConnection() error {
fs.Lock()
defer fs.Unlock()
var err error var err error
clientConfig := &ssh.ClientConfig{ clientConfig := &ssh.ClientConfig{
User: fs.config.Username, User: fs.config.Username,
@@ -547,6 +555,10 @@ func (fs *SFTPFs) wait() {
// we don't detect the event. // we don't detect the event.
fs.err <- fs.sftpClient.Wait() fs.err <- fs.sftpClient.Wait()
fsLog(fs, logger.LevelDebug, "sftp channel closed") fsLog(fs, logger.LevelDebug, "sftp channel closed")
fs.Lock()
defer fs.Unlock()
if fs.sshClient != nil { if fs.sshClient != nil {
fs.sshClient.Close() fs.sshClient.Close()
} }