fix(server): add process-level crash handlers unhandledRejection/uncaughtException/clientError (P1)

Registers three crash-prevention handlers at module level so they are
installed before the first request arrives. These are no-ops on the
happy-path /v1/chat/completions request cycle.

  process.on('unhandledRejection', ...)  — logs and stays alive
  process.on('uncaughtException', ...)   — logs and stays alive
  server.on('clientError', ...)          — destroys the broken socket

process.exit is intentionally NOT called in unhandledRejection /
uncaughtException — the daemon logs the event and continues serving. The
clientError handler destroys the socket to release the file descriptor; if
left open, broken sockets from mid-upload client aborts or malformed HTTP
headers accumulate and exhaust fd limits.

server.on('clientError') is attached inside createOlpServer() (the factory
function) so it is present on every server instance, including test servers
created via the factory — enabling the 45f listener-count assertion.

Authority (ALIGNMENT.md Rule 1 + Rule 5):
  Entry-surface scope — OpenAI Chat Completions API contract unchanged.
  https://platform.openai.com/docs/api-reference/chat
  happy-path /v1/chat/completions is unaffected; these handlers only fire on
  low-level TCP/parse errors or unhandled promises outside the request cycle.
  Port source: OCP server.mjs:2134-2146.

Tests (Suite 45):
  45a–45c: Task A — stdin EPIPE guard fires and logs stdin_write_error in
    anthropic, codex, and mistral providers (mock spawn with EventEmitter
    stdin that emits EPIPE).
  45d: process.listenerCount('unhandledRejection') >= 1
  45e: process.listenerCount('uncaughtException') >= 1
  45f: server created by createOlpServer() has a clientError listener >= 1

Total test count: 819 pass / 0 fail (813 pre-existing + 6 new).

CHANGELOG.md: adds Unreleased entry for both P1 crash-hardening fixes per
release_kit phase_rolling_mode policy (Phase 7 closed at v0.7.0; D-day
commits land under Unreleased until Phase 8 close).

Co-Authored-By: Claude <claude-sonnet-4-6> <noreply@anthropic.com>
This commit is contained in:
2026-05-31 17:18:15 +10:00
co-authored by Claude <claude-sonnet-4-6> <noreply@anthropic.com>
parent a503441ea3
commit 422512d2b2
3 changed files with 290 additions and 2 deletions
+14 -1
View File
@@ -4,7 +4,20 @@ All notable changes to OLP land here. Per `CLAUDE.md` release_kit overlay, this
## Unreleased
(no in-flight changes)
### P1 daemon-crash hardening (fix/olp-p1-crash-hardening)
- **fix(providers): guard stdin EPIPE on fast-failing CLI spawn** — added
`proc.stdin.on('error', ...)` guard in `lib/providers/anthropic.mjs`,
`lib/providers/codex.mjs`, and `lib/providers/mistral.mjs` before any
`stdin.write()` / `stdin.end()` call. Prevents a fast-failing CLI (auth
error, bad model, oversized prompt) from crashing the daemon via an
unhandled EPIPE on the Node stdin Writable. Port of OCP server.mjs:780.
- **fix(server): process-level crash handlers unhandledRejection / uncaughtException / clientError** —
registered `process.on('unhandledRejection', ...)`,
`process.on('uncaughtException', ...)`, and `server.on('clientError', ...)`
at module level. The daemon logs the event and stays alive; `process.exit`
is intentionally not called. Port of OCP server.mjs:2134-2146.
## v0.7.0 — 2026-05-29 — Phase 7 close: Solution 1 isolation + opus 4.8