Skip to content

Commit 667518f

Browse files
authored
x/sync/singleflight: Add lazy map init to Forget
Forget is missing lazy map initialization. If Forget is called before any other method it will panic.
1 parent 0de741c commit 667518f

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

singleflight/singleflight.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ func (g *Group) doCall(c *call, key string, fn func() (interface{}, error)) {
204204
// an earlier call to complete.
205205
func (g *Group) Forget(key string) {
206206
g.mu.Lock()
207+
if g.m == nil {
208+
g.m = make(map[string]*call)
209+
}
207210
if c, ok := g.m[key]; ok {
208211
c.forgotten = true
209212
}

singleflight/singleflight_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,8 @@ func TestPanicDoSharedByDoChan(t *testing.T) {
318318
t.Errorf("Test subprocess failed, but the crash isn't caused by panicking in Do")
319319
}
320320
}
321+
322+
func TestForgetEarly(t *testing.T) {
323+
var g Group
324+
g.Forget("key") // calling Forget before Do/DoChan should not panic
325+
}

0 commit comments

Comments
 (0)