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

Merged
TH
4d ago
+291-7

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