From 605841baade709e28734db2080e0afea6b940c0f Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Sat, 11 Jun 2022 00:19:05 -0700 Subject: [PATCH] lockdep: print more info for the unbalance unlock case * print a lock type * print a list of taken locks PiperOrigin-RevId: 454310284 --- pkg/sync/locking/lockdep.go | 9 ++++++++- pkg/sync/locking/lockdep_test.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/sync/locking/lockdep.go b/pkg/sync/locking/lockdep.go index 0e1703b08..f5739f874 100644 --- a/pkg/sync/locking/lockdep.go +++ b/pkg/sync/locking/lockdep.go @@ -117,6 +117,7 @@ func AddGLock(class *MutexClass, subclass uint32) { // DelGLock deletes a lock from the current goroutine. func DelGLock(class *MutexClass, subclass uint32) { + origClass := class if subclass != 0 { class = class.subclasses.Load(subclass) } @@ -126,7 +127,13 @@ func DelGLock(class *MutexClass, subclass uint32) { panic("the current goroutine doesn't have locks") } if _, ok := (*currentLocks)[class]; !ok { - panic("unlock of an unknown lock") + var b strings.Builder + fmt.Fprintf(&b, "unbalance unlock: %s:%d:\n", *classMap.Load(origClass), subclass) + fmt.Fprintf(&b, "Current locks:\n") + for c := range *currentLocks { + fmt.Fprintf(&b, "\t%s\n", *classMap.Load(c)) + } + panic(b.String()) } delete(*currentLocks, class) diff --git a/pkg/sync/locking/lockdep_test.go b/pkg/sync/locking/lockdep_test.go index 9a9d12b01..c00d39b91 100644 --- a/pkg/sync/locking/lockdep_test.go +++ b/pkg/sync/locking/lockdep_test.go @@ -104,3 +104,17 @@ func TestReverseNested(t *testing.T) { t.Error("The reverse lock order hasn't been detected") } + +func TestUnknownLock(t *testing.T) { + m1 := testMutex{} + m2 := test2RWMutex{} + m1.Lock() + m2.Lock() + defer func() { + if r := recover(); r != nil { + t.Logf("Got expected panic: %s", r) + } + }() + m2.NestedUnlock() + t.Error("An unknown lock has not been detected.") +}