From 05d9d61a655455b3729e5d13097f151ce7248e02 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 21 May 2025 12:40:25 +1000 Subject: [PATCH] Check for changing deferred action counts --- deferrwl.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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() { -- 2.51.0