From 028d6f4e8d39dd748f81d7f22247fa98477774e5 Mon Sep 17 00:00:00 2001 From: AI Assistant Date: Sun, 8 Mar 2026 18:09:07 +0800 Subject: [PATCH] fix: use defer Unlock in Incr/Decr to prevent lock leak - Add defer ca.Unlock() in Incr() and Decr() functions - Fix potential deadlock when error occurs in type switch - Pass race detection test --- cache/runtime/cache_runtime.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cache/runtime/cache_runtime.go b/cache/runtime/cache_runtime.go index 839cc66..c7c07da 100644 --- a/cache/runtime/cache_runtime.go +++ b/cache/runtime/cache_runtime.go @@ -122,6 +122,7 @@ func (ca *RuntimeCache) initValue(key string, value interface{}, ttl int64) erro // Incr increase int64 counter in runtime cache. func (ca *RuntimeCache) Incr(key string) (int64, error) { ca.Lock() + defer ca.Unlock() itemObj, ok := ca.items.Load(key) if !ok { // if not exists, auto set new with 0 @@ -148,8 +149,6 @@ func (ca *RuntimeCache) Incr(key string) (int64, error) { return 0, errors.New("item val is not (u)int (u)int32 (u)int64") } - ca.Unlock() - val, _ := strconv.ParseInt(fmt.Sprint(item.value), 10, 64) return val, nil } @@ -157,6 +156,7 @@ func (ca *RuntimeCache) Incr(key string) (int64, error) { // Decr decrease counter in runtime cache. func (ca *RuntimeCache) Decr(key string) (int64, error) { ca.Lock() + defer ca.Unlock() itemObj, ok := ca.items.Load(key) if !ok { // if not exists, auto set new with 0 @@ -194,7 +194,6 @@ func (ca *RuntimeCache) Decr(key string) (int64, error) { default: return 0, errors.New("item val is not int int64 int32") } - ca.Unlock() val, _ := strconv.ParseInt(fmt.Sprint(item.value), 10, 64) return val, nil