Skip to content

Conversation

@eaglexiang
Copy link

No description provided.

@eaglexiang eaglexiang mentioned this pull request Sep 26, 2025
@eaglexiang
Copy link
Author

fix issue: #136

@mojocn mojocn requested a review from Copilot September 29, 2025 09:58
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a data race condition in the memory store by moving a comparison operation inside the mutex lock to prevent reading s.numStored and s.collectNum without proper synchronization.

  • Introduces a local variable needCollect to capture the collection decision while holding the mutex
  • Moves the collection threshold check from outside the critical section to inside it

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@mojocn mojocn requested a review from Copilot September 29, 2025 10:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@mojocn
Copy link
Owner

mojocn commented Sep 29, 2025

Looking at this pull request, let me analyze the change to understand if it's necessary for fixing a data race problem.Looking at the pull request #137 by eaglexiang, this change is addressing a potential data race condition in the memoryStore.Set() method. Let me analyze whether this fix is necessary:

The Problem:
The original code has a race condition where:

  1. The mutex is unlocked on line 62: s.Unlock()
  2. Then on line 63, it checks s.numStored > s.collectNum without holding the lock
  3. This means another goroutine could modify s.numStored between the unlock and the condition check

The Fix:
The proposed change:

  • Evaluates needCollect := s.numStored > s.collectNum while still holding the mutex (line 62)
  • Unlocks the mutex
  • Then uses the captured boolean value needCollect for the condition check

Is it necessary?
Yes, this fix is necessary and good practice for the following reasons:

  1. Race Condition Prevention: Without this fix, there's a window where s.numStored could be modified by another goroutine between the unlock and the condition check, leading to inconsistent behavior.

  2. Data Integrity: The decision to trigger garbage collection should be based on the state at the time the mutex was held, not on a potentially stale value read after releasing the lock.

  3. Concurrent Safety: In a multi-threaded environment where multiple goroutines might be calling Set() simultaneously, this race condition could cause the garbage collection logic to behave unpredictably.

  4. Minimal Performance Impact: The fix adds just one line and a boolean variable, with negligible performance overhead.

The change is a textbook example of properly handling shared state in concurrent Go programs. It ensures that the decision to collect is made atomically with the state modification, which is exactly what you want for thread-safe code.

Recommendation: Accept this pull request - it's a solid fix for a real concurrency issue.

@mojocn mojocn merged commit 74e19a0 into mojocn:master Sep 29, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants