Skip to content

Commit 36f891b

Browse files
MagicalTuxclaude
andcommitted
Fix data race in DoUntil
Set c.done and c.exp under the mutex lock to prevent race condition when concurrent goroutines read these fields. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2a743bc commit 36f891b

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

unison.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func (g *Group[K, T]) Do(key K, fn func() (T, error)) (T, error) {
4343
g.mu.Unlock()
4444

4545
c.val, c.err = fn()
46-
c.done = true
4746

4847
g.mu.Lock()
4948
delete(g.m, key)
@@ -85,8 +84,11 @@ func (g *Group[K, T]) DoUntil(key K, dur time.Duration, fn func() (T, error)) (T
8584
g.mu.Unlock()
8685

8786
c.val, c.err = fn()
87+
88+
g.mu.Lock()
8889
c.exp = time.Now().Add(dur)
8990
c.done = true
91+
g.mu.Unlock()
9092

9193
c.wg.Done()
9294

0 commit comments

Comments
 (0)