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, "");