Skip to content

fix(runtime): don't exit when a client disconnects mid-request - #182

Merged
kahme247 merged 3 commits into
ompweb-dev:mainfrom
andrebrait:fix/client-abort-no-exit
Oct 1, 2026
Merged

kahme247 merged 3 commits into
ompweb-dev:mainfrom
andrebrait:fix/client-abort-no-exit

Conversation

@andrebrait

@andrebrait andrebrait commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

omp-web's crash handler (instrumentation.node.ts, from #61) exits with code 2 on every uncaughtException. A client that disconnects mid-request can surface there as Error: aborted (ECONNRESET), so closing a tab with a request in flight could take the server down.

Why that error is uncaught: for POST/PUT requests that pass through proxy.ts, Next's body cloning (replaceRequestBody in next/dist/server/body-streams.js) copies the buffered stream's _events onto the request, which drops the request's error listeners, Next's own included. If the client disconnects before the route handler attaches a new one, Node emits aborted with no listener. This is upstream vercel/next.js#99278 (auto-closed without a fix; no fix PR exists). The code is unchanged in 16.3.8 and 16.4.0-canary.53.

Next's own uncaughtException handler only logs this.

Fix

  • The handler now journals these errors (message aborted + code ECONNRESET) to diagnostics.log as one-line client-abort entries and keeps serving.
  • Every other uncaught exception still records a [crash] entry and exits with code 2, including non-Error throws.
  • The journal now follows PI_CODING_AGENT_DIR: it stays at ~/.omp/omp-web/diagnostics.log by default and moves to <agent dir>/omp-web/ when the agent dir is overridden (the same default-agent-dir check lib/omp/paths.ts uses for the XDG layout). Isolated dev servers and tests used to write the real journal.

Verification

  • lib/instrumentation-crash-policy.test.mjs runs register() in a child process with an isolated HOME and a missing omp binary:
    • a client abort keeps the process alive and is journaled as client-abort;
    • look-alikes (read ECONNRESET, aborted by peer, a different code) plus null and undefined still exit with code 2.
    • an isolated agent dir gets its own journal and leaves ~/.omp/omp-web untouched, while the default agent dir still uses ~/.omp/omp-web;
    • It fails on the base handler.
  • Real repro on next dev (fresh server, page load, 40 POSTs to /api/omp-update aborted at random times):
    • before the fix the server died in about 1 of 3 runs;
    • after it, 12 runs stayed up, and the journal recorded client-abort entries with no [crash].
  • 120 aborts against next start, some with artificial event-loop stalls, never reproduced the crash on the base code either, so production exposure appears low.
  • tsc --noEmit passes and eslint passes on the changed files.
  • npm test: 902 pass, 1 fail. The failure is components/MessageView.test.mjs, a load-timing flake that passes when run alone; this PR doesn't touch that file.

For POSTs that pass through proxy.ts, Next's body cloning
(replaceRequestBody) copies the buffered stream's _events onto the
request, dropping its error listeners (vercel/next.js#99278). A client
that disconnects before the route re-attaches one makes Node emit an
unhandled `Error: aborted` (ECONNRESET). Next's own handler only logs
it, but the crash journal handler exited with code 2, so closing a tab
with a request in flight could take the server down (reproduced under
next dev).

Journal these as `client-abort` and keep serving; every other uncaught
exception still exits.
…line short

Non-Error throws (`throw null`) made the handler itself throw (exit 7,
no journal). Client aborts now journal one stackless line so a burst of
disconnects cannot rotate real crash records out quickly.
The journal always went to ~/.omp/omp-web, so dev servers and tests run
with PI_CODING_AGENT_DIR still wrote the real diagnostics.log. Follow the
agent-dir override (same default-agent-dir check as the XDG layout):
~/.omp/omp-web by default, <agent dir>/omp-web when overridden.
@andrebrait
andrebrait force-pushed the fix/client-abort-no-exit branch from 6b72a2d to 894e890 Compare September 30, 2026 19:26
@kahme247
kahme247 merged commit 840d4dd into ompweb-dev:main Oct 1, 2026
3 checks passed
@andrebrait
andrebrait deleted the fix/client-abort-no-exit branch October 2, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants