]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add panic when defer occurs during unlock
authorMatt Joiner <anacrolix@gmail.com>
Mon, 2 Jun 2025 04:55:23 +0000 (14:55 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 2 Jun 2025 04:55:23 +0000 (14:55 +1000)
Will help debug if it occurs

deferrwl.go

index df0c1f51a2c91714f2c8c9f69fbfc5ad3331e165..61292f5771bad7d9881739f53355fd9640444b68 100644 (file)
@@ -15,6 +15,8 @@ type lockWithDeferreds struct {
        internal      sync.RWMutex
        unlockActions []func()
        m             map[uintptr]struct{}
+       // Currently unlocking, defers should not occur?
+       unlocking bool
 }
 
 func (me *lockWithDeferreds) Lock() {
@@ -23,6 +25,7 @@ func (me *lockWithDeferreds) Lock() {
 
 func (me *lockWithDeferreds) Unlock() {
        defer me.internal.Unlock()
+       me.unlocking = true
        startLen := len(me.unlockActions)
        for i := range startLen {
                me.unlockActions[i]()
@@ -32,6 +35,7 @@ func (me *lockWithDeferreds) Unlock() {
        }
        me.unlockActions = me.unlockActions[:0]
        clear(me.unlockActions)
+       me.unlocking = false
 }
 
 func (me *lockWithDeferreds) RLock() {
@@ -43,6 +47,9 @@ func (me *lockWithDeferreds) RUnlock() {
 }
 
 func (me *lockWithDeferreds) Defer(action func()) {
+       if me.unlocking {
+               panic("defer called while unlocking")
+       }
        me.unlockActions = append(me.unlockActions, action)
 }