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
35 changes: 17 additions & 18 deletions go/wrapper/internal/bridge/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions go/wrapper/internal/bridge/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions go/wrapper/moq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions go/wrapper/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 1 addition & 11 deletions go/wrapper/video/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion quest/m0/test-flakes-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 0 additions & 24 deletions quest/m0/test-flakes-2/go-recv-cancel.md

This file was deleted.