[PR #160] [MERGED] fix: thread safety issue and len inaccuracy in Map #338

Closed
opened 2026-02-16 01:15:36 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langgenius/dify-plugin-daemon/pull/160
Author: @lengyhua
Created: 3/28/2025
Status: Merged
Merged: 3/30/2025
Merged by: @Yeuoly

Base: mainHead: main


📝 Commits (1)

  • c904632 fix: thread safety issue and len inaccuracy in Map

📊 Changes

2 files changed (+141 additions, -2 deletions)

View changed files

📝 internal/utils/mapping/sync.go (+30 -2)
internal/utils/mapping/sync_test.go (+111 -0)

📄 Description

Thread-Safety Issue:

Even if atomic operations (e.g., atomic.AddInt32) and mutexes (e.g., sync.RWMutex) are used, modifying multiple variables (e.g., store and len) in a single method creates a critical section. For example:
In Store, the sequence Load → AddInt32 → Store is not atomic as a whole, leading to potential race conditions.
If two goroutines simultaneously call Store for the same key, both might see loaded = false and increment len, resulting in an incorrect count.

len Inaccuracy:

The len field is intended to track the number of unique keys in the map.
In the original Store method, len was incremented unconditionally, even when overwriting an existing key. This caused len to overcount entries.

// Goroutine 1
m.Store("key", 1)  // loaded = false → len++ → store.Store

// Goroutine 2
m.Store("key", 2)  // loaded = false → len++ → store.Store

// Result:
// len = 2 (incorrect), but the map contains only 1 entry ("key").

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langgenius/dify-plugin-daemon/pull/160 **Author:** [@lengyhua](https://github.com/lengyhua) **Created:** 3/28/2025 **Status:** ✅ Merged **Merged:** 3/30/2025 **Merged by:** [@Yeuoly](https://github.com/Yeuoly) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (1) - [`c904632`](https://github.com/langgenius/dify-plugin-daemon/commit/c904632417dd18615c1d258e6b403a385b887adf) fix: thread safety issue and len inaccuracy in Map ### 📊 Changes **2 files changed** (+141 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `internal/utils/mapping/sync.go` (+30 -2) ➕ `internal/utils/mapping/sync_test.go` (+111 -0) </details> ### 📄 Description **Thread-Safety Issue:** > Even if atomic operations (e.g., atomic.AddInt32) and mutexes (e.g., sync.RWMutex) are used, modifying multiple variables (e.g., store and len) in a single method creates a critical section. For example: > In Store, the sequence Load → AddInt32 → Store is not atomic as a whole, leading to potential race conditions. > If two goroutines simultaneously call Store for the same key, both might see loaded = false and increment len, resulting in an incorrect count. > **len Inaccuracy:** > The len field is intended to track the number of unique keys in the map. > In the original Store method, len was incremented unconditionally, even when overwriting an existing key. This caused len to overcount entries. ``` // Goroutine 1 m.Store("key", 1) // loaded = false → len++ → store.Store // Goroutine 2 m.Store("key", 2) // loaded = false → len++ → store.Store // Result: // len = 2 (incorrect), but the map contains only 1 entry ("key"). ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 01:15:36 -05:00
yindo closed this issue 2026-02-16 01:15:36 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-plugin-daemon#338