From 6d37b9b541cd7ee811ae1ae162f0ac24a82dfb03 Mon Sep 17 00:00:00 2001 From: Tiago Neves Date: Sat, 17 Oct 2026 14:23:59 +0100 Subject: [PATCH] fix(api): do not crash the process on the known undici socket-end assertion A single-occurrence critical alert (BIT-31305) traced to node09 CT 264: the worker crashed with AssertionError [ERR_ASSERTION]: false == true at Parser.finish in Node's bundled undici. That is nodejs/undici#5360 -- the HTTP/1 parser asserts !this.paused when the remote socket ends while the parser is paused for backpressure, and throws uncatchably from the socket end handler. pm2 already restarted the worker within the same second, but watchProcess() was also killing the whole process (and losing any other in-flight work) for this one already-closed connection. Recognize the narrow ERR_ASSERTION + undici + Parser.finish signature in watchProcess() and report it as a warning without exiting; every other uncaught exception still fails fast as before. Paperclip-Issue: BIT-31305 Paperclip-Run: 02f17eaf-2939-43e0-93b2-f6d047452558 --- apps/api/src/lib/reporter.ts | 15 +++++++++++++++ apps/api/test/reporter.test.ts | 23 ++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/apps/api/src/lib/reporter.ts b/apps/api/src/lib/reporter.ts index 4eb54f1..f989545 100644 --- a/apps/api/src/lib/reporter.ts +++ b/apps/api/src/lib/reporter.ts @@ -125,6 +125,14 @@ export class ErrorReporter { watchProcess(component: string): void { if (!this.enabled) return; process.on("uncaughtException", (err) => { + if (isUndiciSocketEndAssertion(err)) { + // Known upstream bug (nodejs/undici#5360): undici's HTTP/1 parser asserts !this.paused when the remote + // socket ends while the parser is paused for backpressure. It fires from the socket's own "end" handler, + // so no try/catch around the request can reach it. The one connection involved is already gone; nothing + // else in this process is corrupted, so log it and keep running instead of killing a healthy worker/api. + this.reportError(err, component, { uncaught: true, undici_known_bug: "nodejs/undici#5360" }, "warning"); + return; + } this.reportError(err, component, { uncaught: true }, "critical"); void this.flush().finally(() => process.exit(1)); }); @@ -177,6 +185,13 @@ function frameOf(stack?: string): { file: string; line: number } | null { return null; } +/** nodejs/undici#5360: `assert(!this.paused)` / `false == true` thrown from the parser when a socket ends mid-backpressure. */ +function isUndiciSocketEndAssertion(err: unknown): boolean { + if (!(err instanceof Error) || (err as { code?: unknown }).code !== "ERR_ASSERTION") return false; + const stack = err.stack ?? ""; + return /undici/.test(stack) && /Parser\.finish|onHttpSocketEnd|onParserTimeout/.test(stack); +} + /** The reporter of this deployment (off without ERROR_REPORTING_TOKEN, and in tests). */ export function reporterFor( config: { ERROR_REPORTING_TOKEN?: string | undefined; ERROR_REPORTING_ENDPOINT: string; ERROR_REPORTING_DISABLED: boolean; NODE_ENV: string; APP_VERSION: string }, diff --git a/apps/api/test/reporter.test.ts b/apps/api/test/reporter.test.ts index a182228..ac55b02 100644 --- a/apps/api/test/reporter.test.ts +++ b/apps/api/test/reporter.test.ts @@ -1,6 +1,6 @@ import pino from "pino"; import { Writable } from "node:stream"; -import { afterAll, describe, expect, it } from "vitest"; +import { afterAll, describe, expect, it, vi } from "vitest"; import { buildApp } from "../src/app.js"; import { loadConfig } from "../src/config.js"; import { createDb } from "../src/db.js"; @@ -42,6 +42,27 @@ describe("errors to errors.bitreport.pt", () => { expect(other!.fingerprint).not.toBe(first!.fingerprint); }); + it("survives the known undici socket-end assertion (nodejs/undici#5360) instead of exiting", async () => { + const hub = fakeHub(); + const reporter = reporterWith(hub.fetchImpl); + const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => undefined as never); + const before = process.listeners("uncaughtException"); + reporter.watchProcess("worker"); + try { + const err = new Error("false == true"); + err.name = "AssertionError"; + (err as { code?: string }).code = "ERR_ASSERTION"; + err.stack = `AssertionError [ERR_ASSERTION]: false == true\n at Parser.finish (node:internal/deps/undici/undici:6165:9)\n at TLSSocket. (node:internal/deps/undici/undici:6499:36)`; + process.emit("uncaughtException", err); + await reporter.flush(); + expect(exitSpy).not.toHaveBeenCalled(); + expect(hub.sent.flatMap((s) => s.events)).toMatchObject([{ level: "warning", context: { undici_known_bug: "nodejs/undici#5360" } }]); + } finally { + for (const l of process.listeners("uncaughtException")) if (!before.includes(l)) process.removeListener("uncaughtException", l); + exitSpy.mockRestore(); + } + }); + it("does nothing without a key", async () => { const hub = fakeHub(); const reporter = reporterWith(hub.fetchImpl, ""); -- 2.47.3