"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
sftp: fix data race when using --sftp-ssh
Dirk Petersen7181430fa18b
diff --git a/backend/sftp/ssh_external.go b/backend/sftp/ssh_external.goindex caa472bb6..04525b40c 100644--- a/backend/sftp/ssh_external.go+++ b/backend/sftp/ssh_external.go@@ -11,6 +11,7 @@ import ("slices""strings""sync"+ "sync/atomic""time""github.com/rclone/rclone/fs"@@ -80,8 +81,9 @@ type sshSessionExternal struct {cancel func()startCalled boolrunningSFTP bool- waitOnce sync.Once // ensure Wait() is only called once- waitErr error // result of the Wait() call+ waitOnce sync.Once // ensure Wait() is only called once+ waitErr error // result of the Wait() call+ hasExited atomic.Bool // set once Wait() has returned}func (f *Fs) newSSHSessionExternal() *sshSessionExternal {@@ -176,7 +178,8 @@ func (s *sshSessionExternal) StdoutPipe() (io.Reader, error) {// Return whether the command has finished or notfunc (s *sshSessionExternal) exited() bool {- return s.cmd.ProcessState != nil+ // Don't read s.cmd.ProcessState as Wait() writes it from another goroutine+ return s.hasExited.Load()}// Wait for the command to exit@@ -185,6 +188,7 @@ func (s *sshSessionExternal) Wait() error {// This is safe even if Wait() is called from multiple goroutines.s.waitOnce.Do(func() {s.waitErr = s.cmd.Wait()+ s.hasExited.Store(true)if s.waitErr == nil {fs.Debugf(s.f, "ssh external: command exited OK")} else {diff --git a/backend/sftp/ssh_external_test.go b/backend/sftp/ssh_external_test.goindex b2a51f904..201e1ce9e 100644--- a/backend/sftp/ssh_external_test.go+++ b/backend/sftp/ssh_external_test.go@@ -82,3 +82,21 @@ func TestSSHExternalCloseMultipleCalls(t *testing.T) {// Verify the process has exitedassert.True(t, session.exited())}++// TestSSHExternalExitedWhileWaiting verifies that exited() can be called+// while another goroutine is waiting for the process. Run with -race.+func TestSSHExternalExitedWhileWaiting(t *testing.T) {+ f := &Fs{+ opt: Options{+ SSH: fs.SpaceSepList{"echo", "test"},+ },+ }+ session := f.newSSHSessionExternal()+ assert.NoError(t, session.Start("exit 0"))++ go func() {+ _ = session.Wait()+ }()++ assert.Eventually(t, session.exited, 10*time.Second, time.Millisecond)+}