From 7666c41972c3ce530b75d636160fefa1beddba05 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 10 Nov 2026 01:12:45 -0800 Subject: [PATCH 1/2] chore(quest): claim quest/m0/test-flakes-2/go-recv-cancel.md Quest-Claim: afda9ed9af8e68e3 From 9bc08f5b2a124b1fcf340e8e9bdf3de12e4beb2e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 10 Nov 2026 03:59:02 -0800 Subject: [PATCH 2/2] fix(go): a cancelled stream read waits for its native call The wrapper returned as soon as ctx ended while the native read kept polling, so a group appended right after could be taken by the abandoned read and destroyed. Reads without an object cancel now let the generated binding cancel its own future and wait for it; CallRelease, now unused, is gone. Unquarantines TestRecvGroupCancelKeepsConsumer. Co-Authored-By: Claude Opus 5.5 --- go/wrapper/internal/bridge/call.go | 35 ++++++++++++------------ go/wrapper/internal/bridge/call_test.go | 26 ++++++++++++++++-- go/wrapper/moq_test.go | 2 -- go/wrapper/origin.go | 6 +--- go/wrapper/video/video.go | 12 +------- quest/m0/test-flakes-2/README.md | 1 - quest/m0/test-flakes-2/go-recv-cancel.md | 24 ---------------- 7 files changed, 43 insertions(+), 63 deletions(-) delete mode 100644 quest/m0/test-flakes-2/go-recv-cancel.md diff --git a/go/wrapper/internal/bridge/call.go b/go/wrapper/internal/bridge/call.go index dd2453e8013..f27b68a545c 100644 --- a/go/wrapper/internal/bridge/call.go +++ b/go/wrapper/internal/bridge/call.go @@ -19,13 +19,17 @@ type Handle interface { // run races a blocking FFI call against ctx. // // `cancel` is the object's own cancel() for a call that owns a session or -// listener, so a cancelled Connect still tears the client down. Stream reads -// pass nil: a cancel aborts only that read and leaves the stream usable. For a -// call with a cancel, the generated binding is given a context that does not -// cancel with the caller (see objectCallContext); otherwise it can return -// ctx.Err() into the result channel first and skip that abort. The blocked -// goroutine then unwinds on its own; the result channel is buffered so -// its send never blocks and it can't leak. +// listener, so a cancelled Connect still tears the client down. For such a call +// the generated binding is given a context that does not cancel with the +// caller (see objectCallContext); otherwise it can return ctx.Err() into the +// result channel first and skip that abort. The blocked goroutine then unwinds +// on its own; the result channel is buffered so its send never blocks and it +// can't leak. +// +// Stream reads pass nil: a cancel aborts only that read and leaves the stream +// usable. The generated binding cancels its own future on ctx, so run waits for +// it instead of racing. Returning early would leave the abandoned read still +// polling, free to take the next item and lose it to release. // // release, for a call that returns a native handle, disposes of a result nobody // received. Cancelling is not a retraction: select picks at random when the @@ -40,6 +44,9 @@ func run[T any](ctx context.Context, cancel func(), call func() (T, error), rele var zero T return zero, err } + if cancel == nil { + return call() + } type result struct { val T @@ -53,9 +60,7 @@ func run[T any](ctx context.Context, cancel func(), call func() (T, error), rele select { case <-ctx.Done(): - if cancel != nil { - cancel() - } + cancel() if release != nil { // Exactly one receiver takes the result, so it is this one once the // caller has given up. Waiting here would put the native unwind on @@ -83,20 +88,14 @@ func Call[T any](ctx context.Context, cancel func(), call func(context.Context) } // CallHandle is Call for a call that returns a native handle, which is -// destroyed rather than abandoned when the caller has already given up. Left to +// destroyed rather than abandoned when an object-owned call has already given +// up (the generated binding destroys its own for a call without cancel). Left to // the Go finalizer it would stay live in the meantime: a subscription still // running on the wire, or an incoming request accepted and never answered. func CallHandle[T Handle](ctx context.Context, cancel func(), call func(context.Context) (T, error)) (T, error) { return run(ctx, cancel, func() (T, error) { return call(objectCallContext(ctx, cancel)) }, releaseHandle[T]) } -// CallRelease is CallHandle for a result that may hold a native handle without -// being one, such as an enum with a handle in one variant: release disposes of -// whatever a result nobody received holds. -func CallRelease[T any](ctx context.Context, cancel func(), call func(context.Context) (T, error), release func(T)) (T, error) { - return run(ctx, cancel, func() (T, error) { return call(objectCallContext(ctx, cancel)) }, release) -} - // objectCallContext is the context the generated binding sees. Object-owned // calls already abort via cancel; sharing the caller's ctx lets the binding diff --git a/go/wrapper/internal/bridge/call_test.go b/go/wrapper/internal/bridge/call_test.go index 28ad0992527..43e8a7e47b7 100644 --- a/go/wrapper/internal/bridge/call_test.go +++ b/go/wrapper/internal/bridge/call_test.go @@ -25,6 +25,7 @@ func (s *stubHandle) Destroy() { // // The call parks until the context is cancelled, so the deadline branch wins // without a race, and only then produces the handle nobody is left to receive. +// Only an object-owned call is abandoned this way; one without cancel waits. func TestRunHandleReleasesAResultNobodyReceived(t *testing.T) { ctx, cancelCtx := context.WithCancel(context.Background()) started := make(chan struct{}) @@ -36,7 +37,7 @@ func TestRunHandleReleasesAResultNobodyReceived(t *testing.T) { cancelCtx() }() - val, err := CallHandle(ctx, nil, func(context.Context) (*stubHandle, error) { + val, err := CallHandle(ctx, func() {}, func(context.Context) (*stubHandle, error) { close(started) <-finish return stub, nil @@ -73,7 +74,7 @@ func TestRunHandleIgnoresAResultThatIsNoHandle(t *testing.T) { // own goroutine, so a nil dereference there takes the process down rather // than failing here. released := make(chan struct{}) - _, err := CallRelease(ctx, nil, func(context.Context) (*stubHandle, error) { + _, err := run(ctx, func() {}, func() (*stubHandle, error) { close(started) <-finish return nil, nil @@ -89,6 +90,27 @@ func TestRunHandleIgnoresAResultThatIsNoHandle(t *testing.T) { <-released } +// A cancelled read without an object cancel returns only once the call has: the +// generated binding owns that cancellation, and a read left running would take +// the next item from the stream and lose it. +func TestRunWaitsForACallWithoutObjectCancel(t *testing.T) { + ctx, cancelCtx := context.WithCancel(context.Background()) + returned := false + + _, err := Call(ctx, nil, func(callCtx context.Context) (int, error) { + cancelCtx() + <-callCtx.Done() + returned = true + return 0, callCtx.Err() + }) + if !errors.Is(err, context.Canceled) { + t.Fatalf("err = %v, want context.Canceled", err) + } + if !returned { + t.Fatal("Call returned before the cancelled call did") + } +} + // A context that is already done starts no call at all, so a request that would // have resolved immediately never reaches the native side. func TestRunHandleStartsNothingForADoneContext(t *testing.T) { diff --git a/go/wrapper/moq_test.go b/go/wrapper/moq_test.go index 8f7c8bb22ff..43afd81e650 100644 --- a/go/wrapper/moq_test.go +++ b/go/wrapper/moq_test.go @@ -1276,8 +1276,6 @@ func TestRecvGroupCancelRace(t *testing.T) { // TestRecvGroupCancelKeepsConsumer confirms a cancelled read aborts only // itself: the same consumer still receives the next group. func TestRecvGroupCancelKeepsConsumer(t *testing.T) { - // Quarantined: flaky under load, see /quest/m0/test-flakes-2/go-recv-cancel.md - t.Skip("flaky under load: the RecvGroup after a cancelled one sometimes never sees the appended group") broadcast, err := moq.NewBroadcastProducer() if err != nil { diff --git a/go/wrapper/origin.go b/go/wrapper/origin.go index 4b0ac44da28..d49f155e47e 100644 --- a/go/wrapper/origin.go +++ b/go/wrapper/origin.go @@ -193,11 +193,7 @@ func (c *Claim) SetCost(cost uint64) error { // // Cancelling ctx abandons only this wait: the claim stays advertised. func (c *Claim) Requested(ctx context.Context) (*BroadcastRequest, error) { - inner, err := bridge.CallRelease(ctx, nil, c.inner.Requested, func(request **ffi.MoqBroadcastRequest) { - if request != nil { - (*request).Destroy() - } - }) + inner, err := bridge.Call(ctx, nil, c.inner.Requested) if err != nil || inner == nil { return nil, err } diff --git a/go/wrapper/video/video.go b/go/wrapper/video/video.go index f1b39716fd0..e431be7fe4c 100644 --- a/go/wrapper/video/video.go +++ b/go/wrapper/video/video.go @@ -207,20 +207,10 @@ func (ReadFrame) read() {} func (ReadGap) read() {} func (ReadEnd) read() {} -// releaseRead destroys the decoded frame a read nobody received holds. -func releaseRead(raw *ffi.MoqVideoRead) { - if raw == nil { - return - } - if read, ok := (*raw).(ffi.MoqVideoReadFrame); ok { - read.Frame.Destroy() - } -} - // Next returns the next decoded frame, gap, or encoder end, or (nil, nil) when the // track ends. Close each frame when done with it. func (d *Consumer) Next(ctx context.Context) (Read, error) { - raw, err := bridge.CallRelease(ctx, nil, d.inner.Next, releaseRead) + raw, err := bridge.Call(ctx, nil, d.inner.Next) if err != nil || raw == nil { return nil, err } diff --git a/quest/m0/test-flakes-2/README.md b/quest/m0/test-flakes-2/README.md index 00379061cf2..a77e44674b1 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -272,4 +272,3 @@ Public API: none. Wire: none. - [js/common's worklet dev-server test holds up under load](/quest/m0/test-flakes-2/worklet-dev-server.md) - the dev-server test passes on a loaded host, and its `test.skip` is removed - [Interop's finish cases hold up under load](/quest/m0/test-flakes-2/interop-finish.md) - every publisher's finish cells pass under load, and the scenario's quarantine is removed -- [The Go wrapper's cancelled-RecvGroup test holds up under load](/quest/m0/test-flakes-2/go-recv-cancel.md) - a `RecvGroup` after a cancelled one is woken by the next group, and its `t.Skip` is removed diff --git a/quest/m0/test-flakes-2/go-recv-cancel.md b/quest/m0/test-flakes-2/go-recv-cancel.md deleted file mode 100644 index 27d010f2119..00000000000 --- a/quest/m0/test-flakes-2/go-recv-cancel.md +++ /dev/null @@ -1,24 +0,0 @@ -# [S] The Go wrapper's cancelled-RecvGroup test holds up under load - -## Goal - -`go/wrapper/moq_test.go`'s `TestRecvGroupCancelKeepsConsumer` passes under a -loaded `just check`, and its `t.Skip` is removed. - -## Plan - -Planned 2026-11-09 (maintainer's standing rule that flakes join this epic). -It failed once in a loaded local `just check` for -[moq-dev/moq#8363](https://github.com/moq-dev/moq/pull/8363): after a -`RecvGroup` cancelled in flight, the next `RecvGroup` on the same consumer did -not see a group appended right after it and timed out at 5 s. It passed 30 of -30 runs alone. #8363 does not touch the path: `MoqTrackConsumer::recv_group` -reads through `Polled::wait` in `rs/moq-ffi/src/ffi.rs`. - -A 5 s stall on an already-appended group is a lost wakeup, not slowness. -Reproduce under CPU contention (such as `-race -count` beside CPU burners), -then check whether the cancelled call's waiter stays registered on the bridge -and takes the wake meant for the next call. Fix it at the cause, remove the -`t.Skip`, and delete this quest. - -Public API: none. Wire: none.