"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
fs/accounting: fix deadlock on GetBytes
Nick Craig-Woodbe2273560935
diff --git a/fs/accounting/stats.go b/fs/accounting/stats.goindex c666e39ff..8eabb12cc 100644--- a/fs/accounting/stats.go+++ b/fs/accounting/stats.go@@ -22,14 +22,14 @@ func init() {// StatsInfo accounts all transferstype StatsInfo struct {- lock sync.RWMutex+ mu sync.RWMutexbytes int64errors int64lastError errorchecks int64- checking stringSet+ checking *stringSettransfers int64- transferring stringSet+ transferring *stringSetdeletes int64start time.TimeinProgress *inProgress@@ -38,8 +38,8 @@ type StatsInfo struct {// NewStats cretates an initialised StatsInfofunc NewStats() *StatsInfo {return &StatsInfo{- checking: make(stringSet, fs.Config.Checkers),- transferring: make(stringSet, fs.Config.Transfers),+ checking: newStringSet(fs.Config.Checkers),+ transferring: newStringSet(fs.Config.Transfers),start: time.Now(),inProgress: newInProgress(),}@@ -47,8 +47,8 @@ func NewStats() *StatsInfo {// String convert the StatsInfo to a string for printingfunc (s *StatsInfo) String() string {- s.lock.RLock()- defer s.lock.RUnlock()+ s.mu.RLock()+dt := time.Now().Sub(s.start)dtSeconds := dt.Seconds()speed := 0.0@@ -74,10 +74,15 @@ Elapsed time: %10vs.checks,s.transfers,dtRounded)- if len(s.checking) > 0 {++ // checking and transferring have their own locking so unlock+ // here to prevent deadlock on GetBytes+ s.mu.RUnlock()++ if !s.checking.empty() {fmt.Fprintf(buf, "Checking:\n%s\n", s.checking)}- if len(s.transferring) > 0 {+ if !s.transferring.empty() {fmt.Fprintf(buf, "Transferring:\n%s\n", s.transferring)}return buf.String()@@ -90,51 +95,51 @@ func (s *StatsInfo) Log() {// Bytes updates the stats for bytes bytesfunc (s *StatsInfo) Bytes(bytes int64) {- s.lock.Lock()- defer s.lock.Unlock()+ s.mu.Lock()+ defer s.mu.Unlock()s.bytes += bytes}// GetBytes returns the number of bytes transferred so farfunc (s *StatsInfo) GetBytes() int64 {- s.lock.Lock()- defer s.lock.Unlock()+ s.mu.Lock()+ defer s.mu.Unlock()return s.bytes}// Errors updates the stats for errorsfunc (s *StatsInfo) Errors(errors int64) {- s.lock.Lock()- defer s.lock.Unlock()+ s.mu.Lock()+ defer s.mu.Unlock()s.errors += errors}// GetErrors reads the number of errorsfunc (s *StatsInfo) GetErrors() int64 {- s.lock.RLock()- defer s.lock.RUnlock()+ s.mu.RLock()+ defer s.mu.RUnlock()return s.errors}// GetLastError returns the lastErrorfunc (s *StatsInfo) GetLastError() error {- s.lock.RLock()- defer s.lock.RUnlock()+ s.mu.RLock()+ defer s.mu.RUnlock()return s.lastError}// Deletes updates the stats for deletesfunc (s *StatsInfo) Deletes(deletes int64) int64 {- s.lock.Lock()- defer s.lock.Unlock()+ s.mu.Lock()+ defer s.mu.Unlock()s.deletes += deletesreturn s.deletes}// ResetCounters sets the counters (bytes, checks, errors, transfers) to 0func (s *StatsInfo) ResetCounters() {- s.lock.RLock()- defer s.lock.RUnlock()+ s.mu.RLock()+ defer s.mu.RUnlock()s.bytes = 0s.errors = 0s.checks = 0@@ -144,63 +149,59 @@ func (s *StatsInfo) ResetCounters() {// ResetErrors sets the errors count to 0func (s *StatsInfo) ResetErrors() {- s.lock.RLock()- defer s.lock.RUnlock()+ s.mu.RLock()+ defer s.mu.RUnlock()s.errors = 0}// Errored returns whether there have been any errorsfunc (s *StatsInfo) Errored() bool {- s.lock.RLock()- defer s.lock.RUnlock()+ s.mu.RLock()+ defer s.mu.RUnlock()return s.errors != 0}// Error adds a single error into the stats and assigns lastErrorfunc (s *StatsInfo) Error(err error) {- s.lock.Lock()- defer s.lock.Unlock()+ s.mu.Lock()+ defer s.mu.Unlock()s.errors++s.lastError = err}// Checking adds a check into the statsfunc (s *StatsInfo) Checking(remote string) {- s.lock.Lock()- defer s.lock.Unlock()- s.checking[remote] = struct{}{}+ s.checking.add(remote)}// DoneChecking removes a check from the statsfunc (s *StatsInfo) DoneChecking(remote string) {- s.lock.Lock()- defer s.lock.Unlock()- delete(s.checking, remote)+ s.checking.del(remote)+ s.mu.Lock()s.checks+++ s.mu.Unlock()}// GetTransfers reads the number of transfersfunc (s *StatsInfo) GetTransfers() int64 {- s.lock.RLock()- defer s.lock.RUnlock()+ s.mu.RLock()+ defer s.mu.RUnlock()return s.transfers}// Transferring adds a transfer into the statsfunc (s *StatsInfo) Transferring(remote string) {- s.lock.Lock()- defer s.lock.Unlock()- s.transferring[remote] = struct{}{}+ s.transferring.add(remote)}// DoneTransferring removes a transfer from the stats//// if ok is true then it increments the transfers countfunc (s *StatsInfo) DoneTransferring(remote string, ok bool) {- s.lock.Lock()- defer s.lock.Unlock()- delete(s.transferring, remote)+ s.transferring.del(remote)if ok {+ s.mu.Lock()s.transfers+++ s.mu.Unlock()}}diff --git a/fs/accounting/stringset.go b/fs/accounting/stringset.goindex 09b397ab5..2b18c9df7 100644--- a/fs/accounting/stringset.go+++ b/fs/accounting/stringset.go@@ -3,15 +3,49 @@ package accountingimport ("sort""strings"+ "sync")// stringSet holds a set of strings-type stringSet map[string]struct{}+type stringSet struct {+ mu sync.RWMutex+ items map[string]struct{}+}++// newStringSet creates a new empty string set of capacity size+func newStringSet(size int) *stringSet {+ return &stringSet{+ items: make(map[string]struct{}, size),+ }+}++// add adds remote to the set+func (ss *stringSet) add(remote string) {+ ss.mu.Lock()+ ss.items[remote] = struct{}{}+ ss.mu.Unlock()+}++// del removes remote from the set+func (ss *stringSet) del(remote string) {+ ss.mu.Lock()+ delete(ss.items, remote)+ ss.mu.Unlock()+}++// empty returns whether the set has any items+func (ss *stringSet) empty() bool {+ ss.mu.RLock()+ defer ss.mu.RUnlock()+ return len(ss.items) == 0+}// Strings returns all the strings in the stringSet-func (ss stringSet) Strings() []string {- strings := make([]string, 0, len(ss))- for name := range ss {+func (ss *stringSet) Strings() []string {+ ss.mu.RLock()+ defer ss.mu.RUnlock()+ strings := make([]string, 0, len(ss.items))+ for name := range ss.items {var out stringif acc := Stats.inProgress.get(name); acc != nil {out = acc.String()@@ -26,6 +60,6 @@ func (ss stringSet) Strings() []string {}// String returns all the file names in the stringSet joined by newline-func (ss stringSet) String() string {+func (ss *stringSet) String() string {return strings.Join(ss.Strings(), "\n")}