From ae5970dceb822744efe7876bd346ea3a0e572ff0 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 2 Jun 2025 14:55:23 +1000 Subject: [PATCH] Add panic when defer occurs during unlock Will help debug if it occurs --- deferrwl.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/deferrwl.go b/deferrwl.go index df0c1f51..61292f57 100644 --- a/deferrwl.go +++ b/deferrwl.go @@ -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) } -- 2.51.0