diff --git a/formal/catalogs/go-parity.json b/formal/catalogs/go-parity.json index 4e876fa2..26bcfce3 100644 --- a/formal/catalogs/go-parity.json +++ b/formal/catalogs/go-parity.json @@ -3551,7 +3551,7 @@ "putRemote", "recover" ], - "sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b" + "sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9" }, { "path": "go/deadline.go", @@ -3722,7 +3722,7 @@ "remoteValue", "localResult" ], - "sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b" + "sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9" } ] } @@ -3874,7 +3874,7 @@ "putLocal", "shared" ], - "sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b" + "sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9" }, { "path": "go/api.go", @@ -3944,7 +3944,7 @@ "canRecover", "recover" ], - "sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b" + "sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9" }, { "path": "go/shadow.go", @@ -4260,7 +4260,7 @@ "errorEvent", "duration" ], - "sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b" + "sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9" }, { "path": "go/shadow.go", diff --git a/formal/catalogs/mutations.json b/formal/catalogs/mutations.json index ef645b9c..4441d9f1 100644 --- a/formal/catalogs/mutations.json +++ b/formal/catalogs/mutations.json @@ -631,8 +631,8 @@ }, { "path": "go/engine.go", - "before": "\tvar started time.Duration\n\tfor {\n\t\t// A caller that already left starts and joins nothing.\n\t\tif err := ctx.Err(); err != nil {\n\t\t\tvar zero T\n\t\t\treturn zero, err\n\t\t}\n\t\t// Read the clock before taking the lock. The caller-supplied clock is the\n\t\t// only external code on this path; a panic from it must not leave c.mu\n\t\t// held, or the scope cleanup in Enable would deadlock.\n\t\tstarted = elapsedNow(c.settings.clock)\n\t\tc.mu.Lock()\n\t\tif owner != nil && !owner.live {\n\t\t\tc.mu.Unlock()\n\t\t\treturn x.work(owner)\n\t\t}\n\t\tf := flights[x.key]\n\t\tif f == nil {\n\t\t\tbreak\n\t\t}\n\t\tf.followers++\n\t\tf.waiters++\n\t\tc.mu.Unlock()\n\t\tx.event(\"coalesced\", \"\", map[string]any{\"scope\": label})\n\t\tselect {\n\t\tcase <-f.done:\n\t\tcase <-ctx.Done():\n\t\t}\n\t\tif err := ctx.Err(); err != nil {\n\t\t\tc.mu.Lock()\n\t\t\tf.followers--\n\t\t\tleave(f)\n\t\t\tc.mu.Unlock()\n\t\t\tvar zero T\n\t\t\treturn zero, err\n\t\t}\n\t\t// The work was cancelled because no caller waited for it, and this\n\t\t// follower joined before it returned. With time left, the follower starts\n\t\t// or joins fresh work instead of taking that failure as its own. The work\n\t\t// unpublishes its flight before it settles; a flight still published here\n\t\t// would be joined again forever, so its result stands.\n\t\tif f.err != nil && f.cutShort {\n\t\t\tc.mu.Lock()\n\t\t\tunpublished := flights[x.key] != f\n\t\t\tc.mu.Unlock()\n\t\t\tif unpublished {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t\treturn followerResult[T](f)\n\t}\n\t// Followers share this work, so the leader's cancellation ends only its own\n\t// wait; the work is cancelled when no caller waits for it.\n\tshared, cancel := context.WithCancel(context.WithoutCancel(ctx))\n\tf := &flight{done: make(chan struct{}), started: started, waiters: 1, cancel: cancel}\n\tflights[x.key] = f\n\tc.mu.Unlock()\n\tx.ctx = shared\n\tx.flight = f\n\tgo func() {\n\t\tf.value, f.err = callSafely(func() (T, error) { return x.work(owner) })\n\t\tif shared.Err() != nil {\n\t\t\t// A source timeout is the work's own outcome, which a follower inherits.\n\t\t\t// errors.As runs the error's own methods; one that panics is not a timeout.\n\t\t\ttimedOut, _ := callSafely(func() (bool, error) { return errors.As(f.err, new(*FallbackTimeoutError)), nil })\n\t\t\tf.cutShort = !timedOut\n\t\t}\n\t\tcancel()\n\t\tc.mu.Lock()\n\t\tif flights[x.key] == f {\n\t\t\tdelete(flights, x.key)\n\t\t}\n\t\tclose(f.done)\n\t\tc.mu.Unlock()\n\t}()\n\tselect {\n\tcase <-f.done:\n\tcase <-ctx.Done():\n\t}\n\tif err := ctx.Err(); err != nil {\n\t\tc.mu.Lock()\n\t\tleave(f)\n\t\tc.mu.Unlock()\n\t\tvar zero T\n\t\treturn zero, err\n\t}\n\treturn followerResult[T](f)\n}", - "after": "\t// Fault injection: process flights live in one registry shared by every\n\t// instance, guarded by one lock so the shared map itself stays race-free.\n\tmu := &c.mu\n\tif owner == nil {\n\t\tmu = &sharedProcessFlightsMu\n\t}\n\tvar started time.Duration\n\tfor {\n\t\t// A caller that already left starts and joins nothing.\n\t\tif err := ctx.Err(); err != nil {\n\t\t\tvar zero T\n\t\t\treturn zero, err\n\t\t}\n\t\t// Read the clock before taking the lock. The caller-supplied clock is the\n\t\t// only external code on this path; a panic from it must not leave c.mu\n\t\t// held, or the scope cleanup in Enable would deadlock.\n\t\tstarted = elapsedNow(c.settings.clock)\n\t\tmu.Lock()\n\t\tif owner != nil && !owner.live {\n\t\t\tmu.Unlock()\n\t\t\treturn x.work(owner)\n\t\t}\n\t\tf := flights[x.key]\n\t\tif f == nil {\n\t\t\tbreak\n\t\t}\n\t\tf.followers++\n\t\tf.waiters++\n\t\tmu.Unlock()\n\t\tx.event(\"coalesced\", \"\", map[string]any{\"scope\": label})\n\t\tselect {\n\t\tcase <-f.done:\n\t\tcase <-ctx.Done():\n\t\t}\n\t\tif err := ctx.Err(); err != nil {\n\t\t\tmu.Lock()\n\t\t\tf.followers--\n\t\t\tleave(f)\n\t\t\tmu.Unlock()\n\t\t\tvar zero T\n\t\t\treturn zero, err\n\t\t}\n\t\t// The work was cancelled because no caller waited for it, and this\n\t\t// follower joined before it returned. With time left, the follower starts\n\t\t// or joins fresh work instead of taking that failure as its own. The work\n\t\t// unpublishes its flight before it settles; a flight still published here\n\t\t// would be joined again forever, so its result stands.\n\t\tif f.err != nil && f.cutShort {\n\t\t\tmu.Lock()\n\t\t\tunpublished := flights[x.key] != f\n\t\t\tmu.Unlock()\n\t\t\tif unpublished {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t\treturn followerResult[T](f)\n\t}\n\t// Followers share this work, so the leader's cancellation ends only its own\n\t// wait; the work is cancelled when no caller waits for it.\n\tshared, cancel := context.WithCancel(context.WithoutCancel(ctx))\n\tf := &flight{done: make(chan struct{}), started: started, waiters: 1, cancel: cancel}\n\tflights[x.key] = f\n\tmu.Unlock()\n\tx.ctx = shared\n\tx.flight = f\n\tgo func() {\n\t\tf.value, f.err = callSafely(func() (T, error) { return x.work(owner) })\n\t\tif shared.Err() != nil {\n\t\t\t// A source timeout is the work's own outcome, which a follower inherits.\n\t\t\t// errors.As runs the error's own methods; one that panics is not a timeout.\n\t\t\ttimedOut, _ := callSafely(func() (bool, error) { return errors.As(f.err, new(*FallbackTimeoutError)), nil })\n\t\t\tf.cutShort = !timedOut\n\t\t}\n\t\tcancel()\n\t\tmu.Lock()\n\t\tif flights[x.key] == f {\n\t\t\tdelete(flights, x.key)\n\t\t}\n\t\tclose(f.done)\n\t\tmu.Unlock()\n\t}()\n\tselect {\n\tcase <-f.done:\n\tcase <-ctx.Done():\n\t}\n\tif err := ctx.Err(); err != nil {\n\t\tmu.Lock()\n\t\tleave(f)\n\t\tmu.Unlock()\n\t\tvar zero T\n\t\treturn zero, err\n\t}\n\treturn followerResult[T](f)\n}" + "before": "\tvar started time.Duration\n\tfor {\n\t\t// A caller that already left starts and joins nothing.\n\t\tif err := ctx.Err(); err != nil {\n\t\t\tvar zero T\n\t\t\treturn zero, err\n\t\t}\n\t\t// Read the clock before taking the lock. The caller-supplied clock is the\n\t\t// only external code on this path; a panic from it must not leave c.mu\n\t\t// held, or the scope cleanup in Enable would deadlock.\n\t\tstarted = elapsedNow(c.settings.clock)\n\t\tc.mu.Lock()\n\t\tif owner != nil && !owner.live {\n\t\t\tc.mu.Unlock()\n\t\t\treturn x.work(owner)\n\t\t}\n\t\tf := flights[x.key]\n\t\tif f == nil {\n\t\t\tbreak\n\t\t}\n\t\tf.followers++\n\t\tf.waiters++\n\t\tc.mu.Unlock()\n\t\tx.event(\"coalesced\", \"\", map[string]any{\"scope\": label})\n\t\tselect {\n\t\tcase <-f.done:\n\t\tcase <-ctx.Done():\n\t\t}\n\t\tif err := ctx.Err(); err != nil {\n\t\t\tc.mu.Lock()\n\t\t\tf.followers--\n\t\t\tleave(f)\n\t\t\tc.mu.Unlock()\n\t\t\tvar zero T\n\t\t\treturn zero, err\n\t\t}\n\t\t// The work was cancelled because no caller waited for it, and this\n\t\t// follower joined before it returned. With time left, the follower starts\n\t\t// or joins fresh work instead of taking that failure as its own. The work\n\t\t// unpublishes its flight before it settles; a flight still published here\n\t\t// would be joined again forever, so its result stands.\n\t\tif f.err != nil && f.cutShort {\n\t\t\tc.mu.Lock()\n\t\t\tunpublished := flights[x.key] != f\n\t\t\tc.mu.Unlock()\n\t\t\tif unpublished {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t\treturn followerResult[T](f)\n\t}\n\t// Followers share this work, so the leader's cancellation ends only its own\n\t// wait; the work is cancelled when no caller waits for it.\n\tshared, cancel := context.WithCancel(context.WithoutCancel(ctx))\n\tf := &flight{done: make(chan struct{}), started: started, waiters: 1, cancel: cancel}\n\tflights[x.key] = f\n\tc.mu.Unlock()\n\tx.ctx = shared\n\tx.flight = f\n\tgo func() {\n\t\tf.value, f.err = callSafely(func() (T, error) { return x.work(owner) })\n\t\tif shared.Err() != nil {\n\t\t\t// A source timeout is the work's own outcome, which a follower inherits.\n\t\t\t// errors.As runs the error's own methods; one that panics is not a timeout.\n\t\t\ttimedOut, _ := callSafely(func() (bool, error) { return errors.As(f.err, new(*FallbackTimeoutError)), nil })\n\t\t\tf.cutShort = !timedOut\n\t\t}\n\t\tcancel()\n\t\tc.mu.Lock()\n\t\tif flights[x.key] == f {\n\t\t\tdelete(flights, x.key)\n\t\t}\n\t\tclose(f.done)\n\t\tc.mu.Unlock()\n\t}()\n\tselect {\n\tcase <-f.done:\n\tcase <-ctx.Done():\n\t}\n\tif err := ctx.Err(); err != nil {\n\t\tc.mu.Lock()\n\t\tleave(f)\n\t\tc.mu.Unlock()\n\t\tvar zero T\n\t\treturn zero, err\n\t}\n\t// Like a caller without coalescing, the leader keeps a value that its work\n\t// returned together with an error; followers get only the error.\n\tif f.err != nil {\n\t\tv, _ := assertValue[T](f.value)\n\t\treturn v, f.err\n\t}\n\treturn followerResult[T](f)\n}", + "after": "\t// Fault injection: process flights live in one registry shared by every\n\t// instance, guarded by one lock so the shared map itself stays race-free.\n\tmu := &c.mu\n\tif owner == nil {\n\t\tmu = &sharedProcessFlightsMu\n\t}\n\tvar started time.Duration\n\tfor {\n\t\t// A caller that already left starts and joins nothing.\n\t\tif err := ctx.Err(); err != nil {\n\t\t\tvar zero T\n\t\t\treturn zero, err\n\t\t}\n\t\t// Read the clock before taking the lock. The caller-supplied clock is the\n\t\t// only external code on this path; a panic from it must not leave c.mu\n\t\t// held, or the scope cleanup in Enable would deadlock.\n\t\tstarted = elapsedNow(c.settings.clock)\n\t\tmu.Lock()\n\t\tif owner != nil && !owner.live {\n\t\t\tmu.Unlock()\n\t\t\treturn x.work(owner)\n\t\t}\n\t\tf := flights[x.key]\n\t\tif f == nil {\n\t\t\tbreak\n\t\t}\n\t\tf.followers++\n\t\tf.waiters++\n\t\tmu.Unlock()\n\t\tx.event(\"coalesced\", \"\", map[string]any{\"scope\": label})\n\t\tselect {\n\t\tcase <-f.done:\n\t\tcase <-ctx.Done():\n\t\t}\n\t\tif err := ctx.Err(); err != nil {\n\t\t\tmu.Lock()\n\t\t\tf.followers--\n\t\t\tleave(f)\n\t\t\tmu.Unlock()\n\t\t\tvar zero T\n\t\t\treturn zero, err\n\t\t}\n\t\t// The work was cancelled because no caller waited for it, and this\n\t\t// follower joined before it returned. With time left, the follower starts\n\t\t// or joins fresh work instead of taking that failure as its own. The work\n\t\t// unpublishes its flight before it settles; a flight still published here\n\t\t// would be joined again forever, so its result stands.\n\t\tif f.err != nil && f.cutShort {\n\t\t\tmu.Lock()\n\t\t\tunpublished := flights[x.key] != f\n\t\t\tmu.Unlock()\n\t\t\tif unpublished {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t\treturn followerResult[T](f)\n\t}\n\t// Followers share this work, so the leader's cancellation ends only its own\n\t// wait; the work is cancelled when no caller waits for it.\n\tshared, cancel := context.WithCancel(context.WithoutCancel(ctx))\n\tf := &flight{done: make(chan struct{}), started: started, waiters: 1, cancel: cancel}\n\tflights[x.key] = f\n\tmu.Unlock()\n\tx.ctx = shared\n\tx.flight = f\n\tgo func() {\n\t\tf.value, f.err = callSafely(func() (T, error) { return x.work(owner) })\n\t\tif shared.Err() != nil {\n\t\t\t// A source timeout is the work's own outcome, which a follower inherits.\n\t\t\t// errors.As runs the error's own methods; one that panics is not a timeout.\n\t\t\ttimedOut, _ := callSafely(func() (bool, error) { return errors.As(f.err, new(*FallbackTimeoutError)), nil })\n\t\t\tf.cutShort = !timedOut\n\t\t}\n\t\tcancel()\n\t\tmu.Lock()\n\t\tif flights[x.key] == f {\n\t\t\tdelete(flights, x.key)\n\t\t}\n\t\tclose(f.done)\n\t\tmu.Unlock()\n\t}()\n\tselect {\n\tcase <-f.done:\n\tcase <-ctx.Done():\n\t}\n\tif err := ctx.Err(); err != nil {\n\t\tmu.Lock()\n\t\tleave(f)\n\t\tmu.Unlock()\n\t\tvar zero T\n\t\treturn zero, err\n\t}\n\t// Like a caller without coalescing, the leader keeps a value that its work\n\t// returned together with an error; followers get only the error.\n\tif f.err != nil {\n\t\tv, _ := assertValue[T](f.value)\n\t\treturn v, f.err\n\t}\n\treturn followerResult[T](f)\n}" } ], "requiredDetections": [ diff --git a/go/engine.go b/go/engine.go index 8b810d10..25568c7c 100644 --- a/go/engine.go +++ b/go/engine.go @@ -283,6 +283,12 @@ func (x *execution[T]) singleFlight(flights map[string]*flight, owner *scope, la var zero T return zero, err } + // Like a caller without coalescing, the leader keeps a value that its work + // returned together with an error; followers get only the error. + if f.err != nil { + v, _ := assertValue[T](f.value) + return v, f.err + } return followerResult[T](f) } diff --git a/go/flight_lifetime_test.go b/go/flight_lifetime_test.go index 529d52a4..77bcad82 100644 --- a/go/flight_lifetime_test.go +++ b/go/flight_lifetime_test.go @@ -566,3 +566,32 @@ func TestFollowerOfCancelledWorkThatPanicsRetries(t *testing.T) { } }) } + +func TestCoalescedLeaderKeepsAValueReturnedWithAnError(t *testing.T) { + for _, policy := range []Policy{{LocalTTL: time.Minute}, {LocalTTL: time.Minute, RequestLocal: true}} { + synctest.Test(t, func(t *testing.T) { + f := newLifetimeFixture(policy) + ctx, done := f.cache.Enable(context.Background()) + defer done() + failed := errors.New("partial result") + gate := make(chan struct{}) + load := func(context.Context) (int, error) { f.calls.Add(1); <-gate; return 7, failed } + type result struct { + v int + err error + } + leader, follower := make(chan result, 1), make(chan result, 1) + go func() { v, err := f.get(ctx, load); leader <- result{v, err} }() + synctest.Wait() + go func() { v, err := f.get(ctx, load); follower <- result{v, err} }() + synctest.Wait() + close(gate) + if l, fo := <-leader, <-follower; l.v != 7 || !errors.Is(l.err, failed) || fo.v != 0 || !errors.Is(fo.err, failed) { + t.Fatalf("leader = (%d, %v), follower = (%d, %v); want (7, %v) as without coalescing, and (0, %v)", l.v, l.err, fo.v, fo.err, failed, failed) + } + if v, err := f.get(ctx, func(context.Context) (int, error) { f.calls.Add(1); return 9, nil }); v != 9 || err != nil || f.calls.Load() != 2 { + t.Fatalf("next call = (%d, %v) after %d source calls; want (9, nil) after 2: a value returned with an error is not cached", v, err, f.calls.Load()) + } + }) + } +}