Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: use defer Unlock in Incr/Decr to prevent lock leak (#312)
* chore: bump version to 1.8.3

* fix: use defer Unlock in Incr/Decr to prevent lock leak (#311)

- Add defer ca.Unlock() in Incr() and Decr() functions
- Fix potential deadlock when error occurs in type switch
- Pass race detection test

Co-authored-by: AI Assistant <ai@devfeel.io>

---------

Co-authored-by: AI Assistant <ai@devfeel.io>
  • Loading branch information
devfeel and AI Assistant authored Mar 8, 2026
commit 3b077e245c6e0bc252f6dba5cff060461961b866
5 changes: 2 additions & 3 deletions cache/runtime/cache_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -148,15 +149,14 @@ 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
}

// 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
Expand Down Expand Up @@ -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
Expand Down
Loading