Skip to content

fix(harness): close the transport the losing claim race drops (#897) - #898

Merged
cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/claim-race-transport-close
Nov 18, 2026
Merged

cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/claim-race-transport-close

Conversation

@cwiklik

@cwiklik cwiklik commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Closes #897.

Found by @ingpaolodettori-dev reviewing #896, who named it as the same defect one caller up and
suggested a separate PR rather than widening that one. Branched off main (28db50d6), independent
of #896 — different file, no overlap.

The defect

take() (harness/src/select-sandbox.ts:763-815) builds a SandboxExecClient, and so a gRPC
channel, per lease (:776-800). In the first-turn affinity claim race the loser returns the lease
and drops got (:932-937):

await got.release().catch(() => {});
if (raced) throw new SandboxAffinityPendingError(winner.sandboxId, 0);
return select(env, headCwd, sessionId, opts, deps, true);

Neither exit closes the transport: the raced branch throws, and the recursion takes a fresh lease and
a fresh transport. raced is reached from a 503 the client retries, so this adds one unclosed channel
per retry under first-turn concurrency — and like #894, it is invisible to fd and connection-count
checks, because grpc-js shares subchannels globally and the dead channels pile up on one live
Subchannel's stateListeners.

The fix

await got.transport.close().catch(() => {});

beside the release. Safe there because take() neither seeds nor execs, so nothing is outstanding on
the transport it just built. Best effort for the same reason as the release next to it: a failed close
must not stop the loser converging on the winner — two concurrent first turns of one session have to
end up in one workspace.

:935 is the only such site, checked rather than assumed. remember() (:564-578) is best-effort by
design and never throws, so no affinity-write failure can skip a return got; and the affinity-prior
path (:847) either returns got or throws SandboxPoolSaturatedError from the !got branch, where
no transport was built.

Tests

One test in harness/test/select-sandbox-tiers.test.ts, beside the two existing claim-race tests
(:446, :466) and reusing their fakes. It injects a per-id transport rather than the shared
fakeTransport, so it asserts which channel was closed, not how many were:

Assertion Why
closed is ['m-0'] the loser's channel is closed, exactly once
the selection is still m-1 the winner's is left open — a close that reached it would be the opposite bug

Shown capable of failing: with the close line removed it reports
AssertionError: expected [] to deeply equal [ 'm-0' ].

Verification

Check Result
vitest run --root harness test/select-sandbox{,-tiers,-workload}.test.ts 84 passed
vitest run --root harness 943 passed, 3 pre-existing failures
make typecheck clean
SKIP=hadolint-docker make lint clean (hadolint skipped, no Docker)
Negative control (drop the close) the new test fails

The 3 failures are promote.test.ts's projectRoot case and two context-overlay.test.ts "in a real
shell" cases. They fail identically on this branch's base with nothing applied, and neither file
imports select-sandbox.

Not measured: how much this recovers. It needs concurrent first turns of one session plus a retrying
client to reproduce at all, so the evidence here is the test and its negative control, not a heap
diff — unlike #894, which leaked on every turn.

Assisted-By: Claude Code

…tl#897)

`take()` builds a gRPC channel per lease, so the loser of the first-turn affinity claim race
gave the LEASE back and dropped a live channel: neither exit closed it, since the `raced`
branch throws and the recursion takes a fresh lease and a fresh transport. That branch is
reached from a 503 the client retries, so it accumulates one channel per retry under
first-turn concurrency.

Same defect as rossoctl#894, one caller up, found by the review of rossoctl#896 rather than by a measurement.
Closing there is safe because `take()` neither seeds nor execs, so nothing is outstanding on
the transport it just built. Best effort like the release beside it: a failed close must not
stop the loser converging on the winner.

`:935` is the only such site. `remember()` is best-effort and never throws, so no affinity
write can skip a `return got`, and the affinity-prior path either returns `got` or throws from
the `!got` branch, where no transport exists.

The test distinguishes the two transports rather than counting closes, so it also pins that the
close does NOT reach the winner's channel, and it was shown capable of failing: with the close
removed it reports `expected [] to deeply equal [ 'm-0' ]`.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik
cwiklik requested a review from pdettori as a code owner November 18, 2026 19:53
@cwiklik cwiklik added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 18, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Author: cwiklik (MEMBER — maintainer)
Areas reviewed: TypeScript harness (harness/src/select-sandbox.ts, harness/test/select-sandbox-tiers.test.ts)
Agent/IDE config (.claude/.vscode): none
Commits: 1, signed off (DCO pass)
CI status: all 18 checks passing at e0890b5; base main has advanced to d2355b0c, no overlap with the two files here (mergeable: true)

Verified the defect and the fix against the head tree.

The leak is real. take() builds the transport (and a fresh SandboxExecClient) per lease at :776-800, and neither seeds nor execs on it, so nothing is outstanding when the loser drops it. Both exits from the lost-claim branch — the raced throw and the recursive select(..., true) with a fresh lease and channel — abandoned it.

:935 is the only drop site. take() returns null before building anything; remember() catches its own errors and never throws, so no affinity-write failure can skip a return got; the affinity-prior path and the winner branch return got; the !got/saturated branches throw before a transport exists.

Closing here is safe. SandboxTransport.close() is Promise<void> and idempotent; GrpcRelayTransport's implementation is async, idempotent, and closes the underlying client. That client is constructed per lease (defaultExecClient → makeRelayExecClient builds a new SandboxExecClient on each call), not shared or cached, so closing it can neither double-close nor tear down another turn's channel. The .catch(() => {}) mirrors the release beside it, and close() returns a promise on this path, so a synchronous throw cannot displace the intended SandboxAffinityPendingError.

The test pins both halves. Per-id transports: closed is ['m-0'] (loser) while sel.sandboxId is m-1 (winner left open). Its negative control — expected [] to deeply equal [ 'm-0' ] — is the right failure.

No blocking issues, no suggestions.

Assisted-By: Claude Code

@cwiklik
cwiklik merged commit 841c965 into rossoctl:main Nov 18, 2026
17 checks passed
@pdettori pdettori added this to the v0.11.0 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Affinity claim race leaks a gRPC channel: the losing first turn releases its lease but not its transport

3 participants