Repository navigation
fix(harness): close the transport the losing claim race drops (#897) - #898
Conversation
…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]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
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
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), independentof #896 — different file, no overlap.
The defect
take()(harness/src/select-sandbox.ts:763-815) builds aSandboxExecClient, and so a gRPCchannel, per lease (
:776-800). In the first-turn affinity claim race the loser returns the leaseand drops
got(:932-937):Neither exit closes the transport: the
racedbranch throws, and the recursion takes a fresh lease anda fresh transport.
racedis reached from a 503 the client retries, so this adds one unclosed channelper 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'sstateListeners.The fix
beside the release. Safe there because
take()neither seeds nor execs, so nothing is outstanding onthe 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.
:935is the only such site, checked rather than assumed.remember()(:564-578) is best-effort bydesign and never throws, so no affinity-write failure can skip a
return got; and the affinity-priorpath (
:847) either returnsgotor throwsSandboxPoolSaturatedErrorfrom the!gotbranch, whereno 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 sharedfakeTransport, so it asserts which channel was closed, not how many were:closedis['m-0']m-1Shown capable of failing: with the close line removed it reports
AssertionError: expected [] to deeply equal [ 'm-0' ].Verification
vitest run --root harness test/select-sandbox{,-tiers,-workload}.test.tsvitest run --root harnessmake typecheckSKIP=hadolint-docker make lintThe 3 failures are
promote.test.ts'sprojectRootcase and twocontext-overlay.test.ts"in a realshell" 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