From: Matt Joiner Date: Wed, 21 May 2025 02:40:25 +0000 (+1000) Subject: Check for changing deferred action counts X-Git-Tag: v1.59.0~131 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=05d9d61a655455b3729e5d13097f151ce7248e02;p=btrtrc.git Check for changing deferred action counts --- diff --git a/deferrwl.go b/deferrwl.go index bf95be24..0f1b3a54 100644 --- a/deferrwl.go +++ b/deferrwl.go @@ -1,6 +1,10 @@ package torrent -import "github.com/anacrolix/sync" +import ( + "fmt" + + "github.com/anacrolix/sync" +) // Runs deferred actions on Unlock. Note that actions are assumed to be the results of changes that // would only occur with a write lock at present. The race detector should catch instances of defers @@ -15,12 +19,15 @@ func (me *lockWithDeferreds) Lock() { } func (me *lockWithDeferreds) Unlock() { - unlockActions := me.unlockActions - for i := 0; i < len(unlockActions); i += 1 { - unlockActions[i]() + defer me.internal.Unlock() + startLen := len(me.unlockActions) + for i := range startLen { + me.unlockActions[i]() + } + if len(me.unlockActions) != startLen { + panic(fmt.Sprintf("num deferred changed while running: %v -> %v", startLen, len(me.unlockActions))) } - me.unlockActions = unlockActions[:0] - me.internal.Unlock() + me.unlockActions = me.unlockActions[:0] } func (me *lockWithDeferreds) RLock() {