Skip to content

Commit 61fa4df

Browse files
committed
internal/memoize: fix race on read of handle.function
Late into CL 206879 I started nulling out a handle's function when the handle finished running. That invalidated a previous assumption that the field was immutable. Fix the assumption, and since the case of having multiple computations in flight is at least a little bit possible, try harder to avoid duplicate work. Fixes golang/go#35995. Change-Id: Ib5e3640f931f95e35748f28f5a82cf75585b305d Reviewed-on: https://go-review.googlesource.com/c/tools/+/210077 Run-TryBot: Heschi Kreinick <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent e140590 commit 61fa4df

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

internal/memoize/memoize.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,14 @@ func (h *Handle) run(ctx context.Context) interface{} {
193193
h.cancel = cancel
194194
h.state = stateRunning
195195
h.done = make(chan struct{})
196+
function := h.function // Read under the lock
196197
go func() {
197-
v := h.function(childCtx)
198+
// Just in case the function does something expensive without checking
199+
// the context, double-check we're still alive.
200+
if childCtx.Err() != nil {
201+
return
202+
}
203+
v := function(childCtx)
198204
if childCtx.Err() != nil {
199205
return
200206
}

0 commit comments

Comments
 (0)