diff --git a/internal/memoize/memoize.go b/internal/memoize/memoize.go index 8660111f99..232692c391 100644 --- a/internal/memoize/memoize.go +++ b/internal/memoize/memoize.go @@ -193,8 +193,14 @@ func (h *Handle) run(ctx context.Context) interface{} { h.cancel = cancel h.state = stateRunning h.done = make(chan struct{}) + function := h.function // Read under the lock go func() { - v := h.function(childCtx) + // Just in case the function does something expensive without checking + // the context, double-check we're still alive. + if childCtx.Err() != nil { + return + } + v := function(childCtx) if childCtx.Err() != nil { return }