Skip to content

fix(sandbox): honor context cancellation in Watch - #57

Open
letv1nnn wants to merge 2 commits into
ai-openshell:mainfrom
letv1nnn:refactor/watch-helpers-36
Open

letv1nnn wants to merge 2 commits into
ai-openshell:mainfrom
letv1nnn:refactor/watch-helpers-36

Conversation

@letv1nnn

Copy link
Copy Markdown
Contributor

Fixes #36

The sandbox Watch goroutine now uses watcher.emit/stopped instead of its own selects, so it shares delivery logic with WatchLogs. That also fixes a leak: if a caller cancelled ctx without reading, the goroutine stayed blocked on a full channel.

Behaviour change: cancelling ctx now closes the channel without a final EventError(Cancelled), matching Stop, WatchLogs and the fake client. Callers that need to know why a watch ended should check ctx.Err().

Testing: two new tests (cancel with a full channel, cancel closes without an error). Both fail against the old code. Ran -race -count=10 and mise run ci.

Fixes ai-openshell#36

Signed-off-by: Artem Lytvyn <letv1n2007@icloud.com>
@letv1nnn
letv1nnn requested review from a team as code owners November 17, 2026 23:04

@rhuss rhuss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cc-review Summary

What Went Well

  • openshell/v1/sandbox_client.go Watch (line 455) now sends through the shared w.emit(streamCtx, ...) helper, which also selects on ctx.Done(). A producer blocked on a full 64-slot buffer exits when the caller cancels, and status Watch now shares one send-or-stop path with retryWatchLogs in sandbox_watch_logs.go, as issue #36 asked.
  • openshell/v1/sandbox_client.go:466-469 collapses two nested selects into one guard with a comment explaining why. It uses the same suppression rule as retryWatchLogs (sandbox_watch_logs.go:169), so caller-initiated cancellation is no longer reported as a failure while real gateway errors still surface as EventError.
  • The Watch goroutine's defer close(ch) and defer streamCancel() close the channel and release the stream context on every exit path (EOF, error, Stop, ctx cancel, StopOnTerminal).
  • TestSandboxWatch_ContextCancelUnblocksFullChannel in openshell/v1/sandbox_client_test.go waits with require.Eventually until len(ch) == cap(ch) before cancelling and bounds delivered events by cap(ch)+1, so it hits the blocked-producer path directly instead of relying on timing.
  • The sandboxEvents and drainSandboxWatch helpers (sandbox_client_test.go lines 1166-1196) keep the new tests short, and the 5s drain timeout turns a producer that never closes into a clear failure instead of a hung suite.
  • docs/src/api/sandboxes.md:230 keeps the user-facing docs in sync with the new close/error contract.

Findings

Severity File Description Source
Minor openshell/v1/sandbox_client.go:398 The new close/error contract for Watch is documented on the unexported sandboxClient.Watch met... architecture
Minor openshell/v1/sandbox_client.go:468 The newly documented guarantee that Stop closes the Watch channel without an EventError (even... test-quality
Minor openshell/v1/sandbox_client.go:468 Watch now closes the channel silently when ctx's deadline expires. The guard recvErr != io.EOF... gator

Review Details

These findings were produced by cc-review's reviewer agents and posted without a per-finding review.

  • Findings posted: 3
  • Findings reviewed and not posted: 0
  • Gate outcome: PASS
  • Participating reviewers: architecture, test-quality, gator

Comment thread openshell/v1/sandbox_client.go Resolved
Comment thread openshell/v1/sandbox_client.go Resolved
Comment thread openshell/v1/sandbox_client.go Resolved
…tract on the interface

Signed-off-by: Artem Lytvyn <letv1n2007@icloud.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move the sandbox Watch goroutine onto the shared watcher helpers

2 participants