Skip to content

fix(daemon): release the diagnostics read stream when a caller hangs up - #5159

Merged
thymikee merged 4 commits into
mainfrom
fix/diagnostics-stream-teardown
Nov 28, 2026
Merged

thymikee merged 4 commits into
mainfrom
fix/diagnostics-stream-teardown

Conversation

@thymikee

@thymikee thymikee commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Summary

GET /sessions/<session>/requests/<requestId>/diagnostics streams the record with stream.pipe(res), which only unpipes when the caller hangs up. The read stream stayed open and leaked its descriptor.

The route keeps that shape and adds finished(res, () => stream.destroy()): any response end, hang-up or error destroys the record stream. The error listener stays from creation to close, so an abandoned open that then fails cannot reach uncaughtException and close the daemon.

Before / after

Caller destroys its socket after 64 KiB of a 32 MiB record:

  • base: the stream stays open and leaks the descriptor
  • head: the stream closes, fd is null

Caller hangs up before the record opens:

  • base: the stream is never destroyed
  • head: the stream is destroyed; a later EACCES is caught

Status codes and response bytes are unchanged.

Validation

At 24e1a99795b7b611a17b25db0f675a6af9c42f24:

  • focused test file: 14 passed on Node 26.11.1 and 22.12.0
  • four tests fail on base: mid-download hang-up, both held-open hang-ups, caller gone before createReadStream; the two EACCES cases also fail on 0c09e17 with an uncaught exception
  • first-read EIO 500, open-failure 500 and full download guard existing behaviour
  • temporary uncommitted probes: real EACCES answers 500 JSON, a later read error truncates
  • test/wire-compat: 51 passed; pnpm check:daemon-wire-compat, typecheck, oxlint and oxfmt clean
  • unchanged: a stream.destroy() without an error, which no route code calls, would leave the response open

CI owns the full gate and device lanes.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.67 MB 6.67 MB +22 B
Package (unpacked) 6.67 MB 6.67 MB +22 B
Package (download) 2.04 MB 2.04 MB -3 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 21.7 ms 20.6 ms -1.1 ms
CLI --help 48.5 ms 42.7 ms -5.8 ms

@blacksmith-sh

This comment has been minimized.

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot 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.

Apex Sentinel: 1 new finding on bdbc3a8. The verdict and the full list are in the summary comment.

@thymikee
thymikee force-pushed the fix/diagnostics-stream-teardown branch from bdbc3a8 to cb9fc62 Compare November 27, 2026 17:42
@thymikee
thymikee force-pushed the fix/diagnostics-stream-teardown branch from cb9fc62 to 778d7e1 Compare November 27, 2026 17:42
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot 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.

Apex Sentinel: 1 new finding on 778d7e1. The verdict and the full list are in the summary comment.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot 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.

Apex Sentinel: 1 new finding on d7baba3. The verdict and the full list are in the summary comment.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 0c09e17, one abandoned diagnostics fetch can close the daemon if the record also fails to open. This ends every session, and it is a regression.

The only early error listener on the record stream is the one that once(stream, 'ready', { signal }) adds. An abort removes it. If the caller is already gone during authorize or stat, it is never added. The finally block then destroys a stream that is still opening. If that open fails (EACCES, EMFILE, or ENOENT after the stat), the stream emits error with no listener. With a real EACCES file, both a caller gone during authorize and a hang-up during the open wait raised uncaughtException; the base route raised none. src/daemon.ts sends that to failFatally, which exits with code 1. The rule: the record stream must have an error listener from creation until close, on every route, including abort. Attach it synchronously right after createReadStream, for example with the finished(...).catch() guard without awaiting it, because a held open never settles. Please add tests where the open fails after the caller has hung up, and where the caller is gone before createReadStream; neither may raise uncaughtException.

Could the base route stay as it was (response head, stream.on('error', (e) => failStreamedHttpResponse(res, e)), stream.pipe(res)), with only finished(res, () => stream.destroy()) from node:stream added after createReadStream? In an in-memory run, this destroyed the stream after a mid-body hang-up, a caller gone during authorize, and a hang-up during a held open, with no uncaughtException when that open then failed with EACCES. A connected EACCES and a first-read EIO still answered 500, a later read error still truncated, and a full download completed. It removes the sink, sendHead, once, pipeline, the inner try and isPrematureClose. It does not change the hang after a synthetic stream.destroy() with no error, which no route code triggers.

Not blocking: these can be taken or left: complete the held open after the hang-up and assert a closed fd on success and no uncaughtException on error; say in the PR text that three new tests fail on base while the EIO test guards existing behavior, and drop or qualify the mid-body before/after pair (a real read error already truncates on base); delete the errored/pending check, which always sees a pending stream with no error, or drop the open wait; replace isPrematureClose with only if (disconnect.aborted) return;, since no code on this route reaches its other case; and move withIO, nearly a copy of the helper in upload-http-deadline.test.ts, to a shared test helper with a timeout parameter.

These threads no longer apply and can be resolved: #5159 (comment) (the open wait now takes the disconnect signal; the missing listener after abort is covered above), #5159 (comment) (a first-read EIO now answers 500 JSON), #5159 (comment) (the shared responseDisconnectSignal is now used; the listener gap is covered above), and #5159 (comment) (a premature close returns quietly only when the response is destroyed, and otherwise the response is truncated).

The required evidence is in place. All 22 checks pass at this commit. No test covers an open that fails after a hang-up, so the passing checks do not cover this problem.

Not verified here: the Node 22.12 floor, since reproductions ran only on Node 26.11.1. The route and its two sibling modules were run in memory with stubbed authorize, path and error classes and a delayed fs.open, not through createDaemonHttpServer or a spawned daemon. The daemon exit was traced in the code, not observed. The test file was not run locally; the base and head comparison comes from the in-memory runs. A record that changes size during the read was not examined, and content-length still comes from the earlier stat, as on base.

Before merge, keep an error listener on the record stream from creation to close (or use the finished(res) design above) and add a test where the open fails after the caller has hung up.

An abandoned diagnostics fetch destroyed a record stream that was still
opening with no `error` listener, so a failed open reached
`uncaughtException` and closed the daemon.

Restore the `pipe` route, whose listener lives from creation to close, and
destroy the stream when the response finishes, errors or is abandoned.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@thymikee

Copy link
Copy Markdown
Member Author

Re the review: fixed at 24e1a99 with the shape you suggested. The route is back to main's head, stream.on('error', …) and stream.pipe(res), plus finished(res, () => stream.destroy()) after createReadStream. The sink, sendHead, open wait, disconnect signal, pipeline and isPrematureClose are gone.

New tests cover an open that fails with EACCES after a hang-up, and a caller gone before createReadStream. Both raise an uncaught exception on 0c09e17 and pass now. The held-open test now completes the open and asserts a closed fd. The synthetic mid-body destroy test is removed. The shared withIO helper move is not included.

Also checked on Node 22.12.0 and 26.11.1: real EACCES answers 500 JSON, a later read error truncates, a full download completes. The wire-compat ack carries the new digest (it differs from main's by the added line). The four listed threads are already resolved.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 24e1a99

Since the review of 0c09e17: 0 new · 0 still open · 0 no longer raised

No open findings.

Reviewed with this repository's own Sentinel configuration (f0b3cb684685).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@thymikee

Copy link
Copy Markdown
Member Author

At 24e1a99, the earlier problem is fixed: an abandoned diagnostics fetch with a failing record open can no longer close the daemon. finished(res) destroys a stream that is still opening when the caller hangs up, and the error listener stays attached. No new regression was found, and nothing else needs to change before merge.

These threads no longer apply and can be resolved: #5159 (comment) (the open wait is gone, and a hang-up destroys an opening stream), #5159 (comment) (a first-read EIO answers 500 JSON, with a test), #5159 (comment) (the hand-written disconnect wiring is gone, and an already-gone caller is covered and tested), and #5159 (comment) (isPrematureClose is gone and real read errors truncate the response; only an error-free destroy() in mid-body would hang, as on base, and no route code does that).

The required evidence is in place.

CI is green. One earlier Body rules run on this commit was cancelled, a later run succeeded, and that check does not exercise the changed route.

Not verified here: no test or reproduction was run. Failure behavior on base and on the earlier commit comes from reading the code, and the new behavior comes from reading the code plus the passing CI unit run on Node 24.13.1. How finished() treats an already closed response was read only in Node 26.11.1 source, so the Node 22.12 floor rests on the author's local run. HTTP/1.1 pipelining, where a queued response loses its caller, was not examined; base does not release the stream there either. No committed test covers a read error after the first chunk; that path was only traced in code.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 27, 2026
@thymikee
thymikee merged commit c39c5a2 into main Nov 28, 2026
23 of 26 checks passed
@thymikee
thymikee deleted the fix/diagnostics-stream-teardown branch November 28, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant