aboutsummaryrefslogtreecommitdiffhomepage
path: root/lazy
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2023-03-04 18:08:29 +0100
committerBjørn Erik Pedersen <[email protected]>2023-03-04 21:29:05 +0100
commit3bbeb5688c4452a336af07be1e8cec44dac7d6ce (patch)
treef7e9f79839d1c050c148b5265c066f98a088fa8a /lazy
parent184a67ac4704b88083c43a68a5f85800192d7ff1 (diff)
downloadhugo-3bbeb5688c4452a336af07be1e8cec44dac7d6ce.tar.gz
hugo-3bbeb5688c4452a336af07be1e8cec44dac7d6ce.zip
Fix "context canceled" with partial
Make sure the context used for timeouts isn't created based on the incoming context, as we have cases where this can cancel the context prematurely. Fixes #10789
Diffstat (limited to 'lazy')
-rw-r--r--lazy/init.go7
-rw-r--r--lazy/init_test.go6
2 files changed, 4 insertions, 9 deletions
diff --git a/lazy/init.go b/lazy/init.go
index 4de2a83f7..bfb9c4e07 100644
--- a/lazy/init.go
+++ b/lazy/init.go
@@ -180,14 +180,15 @@ func (ini *Init) checkDone() {
}
func (ini *Init) withTimeout(ctx context.Context, timeout time.Duration, f func(ctx context.Context) (any, error)) (any, error) {
- ctx, cancel := context.WithTimeout(ctx, timeout)
+ // Create a new context with a timeout not connected to the incoming context.
+ waitCtx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
c := make(chan verr, 1)
go func() {
v, err := f(ctx)
select {
- case <-ctx.Done():
+ case <-waitCtx.Done():
return
default:
c <- verr{v: v, err: err}
@@ -195,7 +196,7 @@ func (ini *Init) withTimeout(ctx context.Context, timeout time.Duration, f func(
}()
select {
- case <-ctx.Done():
+ case <-waitCtx.Done():
return nil, errors.New("timed out initializing value. You may have a circular loop in a shortcode, or your site may have resources that take longer to build than the `timeout` limit in your Hugo config file.")
case ve := <-c:
return ve.v, ve.err
diff --git a/lazy/init_test.go b/lazy/init_test.go
index 499ea2cce..efc329d79 100644
--- a/lazy/init_test.go
+++ b/lazy/init_test.go
@@ -126,12 +126,6 @@ func TestInitAddWithTimeoutTimeout(t *testing.T) {
init := New().AddWithTimeout(100*time.Millisecond, func(ctx context.Context) (any, error) {
time.Sleep(500 * time.Millisecond)
- select {
- case <-ctx.Done():
- return nil, nil
- default:
- }
- t.Fatal("slept")
return nil, nil
})