]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Check for changing deferred action counts
authorMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2025 02:40:25 +0000 (12:40 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2025 02:40:25 +0000 (12:40 +1000)
deferrwl.go

index bf95be244f064880a6117a1358304e5db634d1af..0f1b3a5443717f3d08327066709005ac83c880a2 100644 (file)
@@ -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() {