From 517e7d927199f6bfe1ac8e7b06c5308bc2b7c022 Mon Sep 17 00:00:00 2001 From: buda Date: Mon, 27 Jun 2022 20:56:03 +0400 Subject: [PATCH] accounting: fix unknown length file transfers count 3 transfers each #6213 This was caused by nested calls to NewTransfer/Done. This fixes the problem by only incrementing transfers if the remote is present in the transferMap which means we only increment it once. --- fs/accounting/stats.go | 6 +++--- fs/accounting/transfermap.go | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/fs/accounting/stats.go b/fs/accounting/stats.go index b4b32a8ee..fc17f95ac 100644 --- a/fs/accounting/stats.go +++ b/fs/accounting/stats.go @@ -714,10 +714,10 @@ func (s *StatsInfo) NewTransferRemoteSize(remote string, size int64) *Transfer { // DoneTransferring removes a transfer from the stats // -// if ok is true then it increments the transfers count +// if ok is true and it was in the transfermap (to avoid incrementing in case of nested calls, #6213) then it increments the transfers count func (s *StatsInfo) DoneTransferring(remote string, ok bool) { - s.transferring.del(remote) - if ok { + existed := s.transferring.del(remote) + if ok && existed { s.mu.Lock() s.transfers++ s.mu.Unlock() diff --git a/fs/accounting/transfermap.go b/fs/accounting/transfermap.go index ed64bf369..4fb30a079 100644 --- a/fs/accounting/transfermap.go +++ b/fs/accounting/transfermap.go @@ -34,10 +34,13 @@ func (tm *transferMap) add(tr *Transfer) { } // del removes a transfer from the map by name -func (tm *transferMap) del(remote string) { +func (tm *transferMap) del(remote string) bool { tm.mu.Lock() + _, exists := tm.items[remote] delete(tm.items, remote) tm.mu.Unlock() + + return exists } // merge adds items from another map