Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions formal/catalogs/go-parity.json
Original file line number Diff line number Diff line change
Expand Up @@ -3551,7 +3551,7 @@
"putRemote",
"recover"
],
"sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b"
"sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9"
},
{
"path": "go/deadline.go",
Expand Down Expand Up @@ -3722,7 +3722,7 @@
"remoteValue",
"localResult"
],
"sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b"
"sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9"
}
]
}
Expand Down Expand Up @@ -3874,7 +3874,7 @@
"putLocal",
"shared"
],
"sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b"
"sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9"
},
{
"path": "go/api.go",
Expand Down Expand Up @@ -3944,7 +3944,7 @@
"canRecover",
"recover"
],
"sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b"
"sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9"
},
{
"path": "go/shadow.go",
Expand Down Expand Up @@ -4260,7 +4260,7 @@
"errorEvent",
"duration"
],
"sha256": "29ef764c03bbffaee2a37cc2fe526c7761e7ba2c23a0f4b445741b061e53271b"
"sha256": "7872cc5aa3eada704c89f1e11ddd5ee619ebb5533f5aed95cf74f228bea0f3f9"
},
{
"path": "go/shadow.go",
Expand Down
4 changes: 2 additions & 2 deletions formal/catalogs/mutations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
6 changes: 6 additions & 0 deletions go/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
29 changes: 29 additions & 0 deletions go/flight_lifetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,32 @@ func TestFollowerOfCancelledWorkThatPanicsRetries(t *testing.T) {
}
})
}

func TestCoalescedLeaderKeepsAValueReturnedWithAnError(t *testing.T) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agent: T1 (low, V). This test pins only the leader. Suggested companion test: it pins that followers get only the error, and that a value returned with an error is not cached. It passes at d5887b0 with -race -count=3. It fails on a mutant where followerResult also returns the value ("follower = (7, partial result), want (0, partial result)"), and no native test catches that mutant today.

func TestCoalescedFollowerGetsOnlyTheErrorAndNothingIsCached(t *testing.T) {
	synctest.Test(t, func(t *testing.T) {
		f := newLifetimeFixture(Policy{LocalTTL: time.Minute})
		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 }
		leader := make(chan int, 1)
		go func() { v, _ := f.get(ctx, load); leader <- v }()
		synctest.Wait()
		var fv int
		follower := make(chan error, 1)
		go func() { v, err := f.get(ctx, load); fv = v; follower <- err }()
		synctest.Wait()
		close(gate)
		if v := <-leader; v != 7 {
			t.Fatalf("leader value = %d, want 7", v)
		}
		if err := <-follower; fv != 0 || !errors.Is(err, failed) {
			t.Fatalf("follower = (%d, %v), want (0, %v)", fv, err, 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())
		}
	})
}

— Levicus Connecto 🤖

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())
}
})
}
}
Loading