From bebf6322ff01a503942d2a35c6a97af82a4e717c Mon Sep 17 00:00:00 2001 From: sado <51654192+sado0823@users.noreply.github.com> Date: Fri, 26 Aug 2022 20:07:25 +0800 Subject: [PATCH] fix resource manager dead lock (#2302) Co-authored-by: sado --- core/syncx/resourcemanager.go | 2 +- core/syncx/resourcemanager_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/syncx/resourcemanager.go b/core/syncx/resourcemanager.go index e863ba0a..f556f924 100644 --- a/core/syncx/resourcemanager.go +++ b/core/syncx/resourcemanager.go @@ -57,8 +57,8 @@ func (manager *ResourceManager) GetResource(key string, create func() (io.Closer } manager.lock.Lock() + defer manager.lock.Unlock() manager.resources[key] = resource - manager.lock.Unlock() return resource, nil }) diff --git a/core/syncx/resourcemanager_test.go b/core/syncx/resourcemanager_test.go index a62b7892..725b8d13 100644 --- a/core/syncx/resourcemanager_test.go +++ b/core/syncx/resourcemanager_test.go @@ -74,6 +74,12 @@ func TestResourceManager_UseAfterClose(t *testing.T) { return nil, errors.New("fail") }) assert.NotNil(t, err) + + assert.Panics(t, func() { + _, err = manager.GetResource("key", func() (io.Closer, error) { + return &dummyResource{age: 123}, nil + }) + }) } }