Repository navigation
fix(mcpx): supervisor per-connection writer + bounded write deadline (#2390) - #2861
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 28 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8b3251ea27
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| s.mu.Lock() | ||
| if s.attached != nil { | ||
| if _, werr := s.attached.Write(data); werr != nil { | ||
| w := s.attached | ||
| s.mu.Unlock() | ||
| if w != nil { |
There was a problem hiding this comment.
Keep the unattached decision atomic with buffering
When stdout arrives concurrently with an attachment, the pump can snapshot s.attached == nil and release s.mu, then handleAttach can drain the buffer and publish its writer before the pump reaches s.buf.Write(data). That already-read chunk is consequently left in the buffer until another attachment, while subsequent chunks go directly to the current daemon, withholding output and reversing stream order. Keep the nil-writer decision and buffer append atomic relative to the attach drain/publication; the per-writer mutex only orders chunks routed to that writer.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid — fixing. The unattached path will keep the nil-writer decision and the tail-buffer append atomic under s.mu (a memory append, not a connection write): the pump holds s.mu across buf.Write when the snapshot is nil, so an already-read chunk is either inside the drain an attach flushes, or routed to the published writer — never stranded in the buffer behind a live stream. The per-writer mutex continues to order only chunks routed to a writer. Fix commit follows as soon as the laptop worktree is reachable again (SSH tunnel is down this shift); CI on the current head is fully green, and this thread stays open until the fix lands.
There was a problem hiding this comment.
Fixed in 3a272fb (pushed): the pump now holds s.mu across the tail-buffer append when the snapshot writer is nil — the nil decision and buf.Write are atomic relative to handleAttach drain/publish, so a just-read chunk is either inside the drained buffer or routed to the published writer. buf.Write is a bounded memory append under the buffer's own mutex, not a connection write, so the #2390 no-write-under-s.mu rule stands. Verified on the laptop: go test ./internal/mcpclient/ -run TestSupervised -race PASS; full package PASS.
| s.mu.Unlock() | ||
| werr := w.flushLocked(reply, buffered) | ||
| w.mu.Unlock() |
There was a problem hiding this comment.
Coordinate child-exit closure with the initial flush
If the handshake snapshots ChildState == "running" and the child exits without further stdout while the initial buffered flush is in progress, pumpDone can close without waiting on this writer. finishWhenDone then sees attachedEver == true and reported == false and immediately closes the connection, interrupting the handshake or buffered flush and losing bytes already drained from the tail buffer. Previously, holding s.mu through the flush prevented that closure. Coordinate final connection closure with completion of the initial flush without restoring socket writes under s.mu.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid — fixing. finishWhenDone will take a flush-completion barrier on the connWriter before closing the attached connection: acquiring the writer mutex (which the initial flush holds across publish+flush, and which is bounded by the write deadline) and releasing it before Close. The stdout pump is already done at that point, so the only writer the barrier can wait on is the initial flush — closure can no longer interrupt it, and no socket write returns to s.mu. Fix commit follows as soon as the laptop worktree is reachable again (SSH tunnel is down this shift); this thread stays open until the fix lands.
There was a problem hiding this comment.
Fixed in 3a272fb (pushed): connWriter.closeAfterFlush() — finishWhenDone now acquires/releases the writer mutex (the barrier the initial flush holds across publish+flush, bounded by AttachedWriteTimeout) before closing the attached connection, at both close sites in finishWhenDone. The pump has exited at those points (pumpDone), so the flush is the only possible holder; no socket write returns to s.mu. Verified on the laptop: go test ./internal/mcpclient/ -run TestSupervised -race PASS; full package PASS.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3a272fbb7d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| s.buf.Write(data) | ||
| s.attached.Close() | ||
| s.attached = nil | ||
| s.mu.Lock() | ||
| if s.attached == w { | ||
| s.attached = nil |
There was a problem hiding this comment.
Route failed old-writer chunks through the current attachment
If a daemon detaches while a stdout write is outstanding, detach can clear the old writer and a new attachment can drain the buffer and publish its writer before the old write returns an error. This error path then appends the older chunk to the tail buffer even though the new attachment is live; subsequent chunks reach the new daemon directly, leaving the older output stranded until another attachment and reversing stream order. A controlled reproduction produced live B\n on the replacement connection with A\n left buffered. Coordinate recovery of failed writes with attachment drain/publication, including when the failed writer has already been replaced.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in the follow-up commit (pushed): the recovery path now snapshots the current writer under s.mu — with no replacement, the tail-buffer append happens under s.mu (atomic with the next attach drain, same as the unattached path); with a replacement writer already published, the chunk is routed to that writer outside s.mu, serialised on its mutex behind its initial flush, so it precedes later live chunks instead of stranding in the buffer. The pump is single-goroutine, so routing within the same iteration preserves stream order. If the replacement write also fails, the chunk falls back to the buffer and that writer is dropped the same way. Laptop: go test ./internal/mcpclient/ -race (supervised subset) and full package PASS.
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Closes #2390. Implements the triage settlements from queue task 3559 exactly:
s.mu: new per-connectionconnWriter(own mutex + conn).pumpStdoutonly snapshots the writer pointer unders.mu;handleAttachpublishes the writer unders.muand releases it before writing.AttachedWriteTimeout, defaultDefaultAttachedWriteTimeout= 5s, package-level and test-overridable), set before each write. On expiry/error the chunk goes to the tail buffer, the attach is cleared only if it still points at that same writer (pointer identity), and the conn is closed — so the pump drains the child pipe to EOF, the recorded exit surfaces, and detach/re-attach proceed.supervised_test.go):TestSupervisedStalledReaderAttachDropped— a daemon that never reads, a child emitting ~5 MiB then exiting; assertsRunSupervisorfinishes within a timeout and the stalled connection was closed by the supervisor.TestSupervisedAttachFlushOrdering— raw attach asserts reply first, then the 100-frame buffered burst in order, then a live echo. New scripted children:floodchild,burstchild.Untouched per scope: wire protocol/handshake version, tail-buffer sizing and
droppedBytesaccounting, the client-side transport, and the default-off supervised flag.Verification is CI-first: this PR's checks run the mcpx suite (including
-racecoverage in the mcpx workflows). Queue task 3978.Need help on this PR? Tag
@codesmith-botwith what you need.