Repository navigation
fix(sandbox): honor context cancellation in Watch #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -395,6 +395,14 @@ func checkTerminalPhase(sb *Sandbox, name string, target SandboxPhase) (*Sandbox | |
| } | ||
| } | ||
|
|
||
| // Watch streams status changes for one sandbox. The first event is | ||
| // EventAdded, later ones EventModified, and EventDeleted once the sandbox is | ||
| // deleting. | ||
| // | ||
| // The channel closes when the server ends the stream, when StopOnTerminal | ||
| // sees a terminal phase, when ctx is cancelled, or after Stop. Cancelling ctx | ||
| // or calling Stop closes it without an EventError: EventError reports only a | ||
| // stream failure, such as the gateway becoming unavailable. | ||
| func (s *sandboxClient) Watch(ctx context.Context, workspace, name string, opts ...WatchOptions) (WatchInterface[*Sandbox], error) { | ||
| if name == "" { | ||
| return nil, &StatusError{Code: ErrorInvalidArgument, Message: "sandbox name must not be empty"} | ||
|
|
@@ -444,9 +452,7 @@ func (s *sandboxClient) Watch(ctx context.Context, workspace, name string, opts | |
| } else if sandbox.Status.Phase == SandboxDeleting { | ||
| eventType = EventDeleted | ||
| } | ||
| select { | ||
| case ch <- Event[*Sandbox]{Type: eventType, Object: sandbox}: | ||
| case <-w.done: | ||
| if !w.emit(streamCtx, Event[*Sandbox]{Type: eventType, Object: sandbox}) { | ||
| return | ||
| } | ||
| if watchOpts.StopOnTerminal && (sandbox.Status.Phase == SandboxReady || sandbox.Status.Phase == SandboxCompleted || sandbox.Status.Phase == SandboxStopped || sandbox.Status.Phase == SandboxError) { | ||
|
|
@@ -457,16 +463,10 @@ func (s *sandboxClient) Watch(ctx context.Context, workspace, name string, opts | |
| var recvErr error | ||
| ev, recvErr = stream.Recv() | ||
| if recvErr != nil { | ||
| if recvErr != io.EOF { | ||
| select { | ||
| case <-w.done: | ||
| return | ||
| default: | ||
| } | ||
| select { | ||
| case ch <- Event[*Sandbox]{Type: EventError, Err: converter.FromGRPCError(recvErr)}: | ||
| case <-w.done: | ||
| } | ||
| // A cancelled stream after Stop or ctx cancellation is the | ||
| // caller's own doing, not a failure to report. | ||
| if recvErr != io.EOF && !w.stopped() && streamCtx.Err() == nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: The newly documented guarantee that Stop closes the Watch channel without an Why this matters: That test passes whether or not Stop cancels the stream or suppresses the error, and the two new tests only exercise ctx cancellation. A regression where Stop no longer cancels Suggested fix: Add Source: test-quality agent
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Watch now closes the channel silently when ctx's deadline expires. The guard Why this matters: A caller using Suggested fix: Mention the deadline case and how to detect it in both places. For the godoc, something like Source: gator agent |
||
| w.emit(streamCtx, Event[*Sandbox]{Type: EventError, Err: converter.FromGRPCError(recvErr)}) | ||
| } | ||
| return | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: The new close/error contract for Watch is documented on the unexported
sandboxClient.Watchmethod, so it never shows up in godoc. The exportedSandboxInterface.Watch(openshell/v1/sandbox.go:120) that callers actually use still has no doc comment.Why this matters: Someone reading
go doc v1.SandboxInterfaceor pkg.go.dev to find out whether ctx cancellation produces anEventErrorwon't see this comment, sincesandboxClientis unexported. Sibling methods on the same interface (ListProviders,ListAllProvidersatsandbox.go:113-116) carry their docs on the interface, so the new contract is only discoverable indocs/src/api/sandboxes.mdor by reading the implementation, and the fake inopenshell/v1/fakehas nothing to conform to.Suggested fix: Move (or copy) the comment onto
WatchinSandboxInterfaceinopenshell/v1/sandbox.go, following theListProvidersprecedent, and drop or shorten the copy on the unexported method.Source: architecture agent