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
127 lines
6.1 KiB
TypeScript
127 lines
6.1 KiB
TypeScript
import pino from "pino";
|
|
import { Writable } from "node:stream";
|
|
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";
|
|
import { ErrorReporter } from "../src/lib/reporter.js";
|
|
|
|
type Sent = { url: string; auth: string | null; events: { level: string; message: string; fingerprint: string; stacktrace?: string; context: Record<string, unknown> }[] };
|
|
|
|
function fakeHub() {
|
|
const sent: Sent[] = [];
|
|
const fetchImpl = (async (url: string | URL | Request, init?: RequestInit) => {
|
|
sent.push({ url: String(url), auth: new Headers(init?.headers).get("authorization"), events: JSON.parse(String(init?.body)).events });
|
|
return new Response("{}", { status: 200 });
|
|
}) as typeof fetch;
|
|
return { sent, fetchImpl };
|
|
}
|
|
|
|
const reporterWith = (fetchImpl: typeof fetch, token: string | undefined = "ingest-key") =>
|
|
new ErrorReporter({ token, endpoint: "https://errors.test/", environment: "production", release: "abc1234", fetchImpl });
|
|
|
|
describe("errors to errors.bitreport.pt", () => {
|
|
it("sends errors to the hub's ingest with the project's key, once per error for a while", async () => {
|
|
const hub = fakeHub();
|
|
const reporter = reporterWith(hub.fetchImpl);
|
|
const boom = () => new Error("archive scan failed for 7d1e9c1a-4c4f-4a55-9d2f-0f1c2b3a4d5e");
|
|
reporter.reportError(boom(), "worker", { sourceId: "x" });
|
|
reporter.reportError(boom(), "worker");
|
|
reporter.reportError(new Error("something else"), "worker");
|
|
await reporter.flush();
|
|
expect(hub.sent).toHaveLength(1);
|
|
expect(hub.sent[0]).toMatchObject({ url: "https://errors.test/v1/ingest/events", auth: "Bearer ingest-key" });
|
|
const [first, other] = hub.sent[0]!.events;
|
|
expect(first).toMatchObject({
|
|
level: "error",
|
|
message: expect.stringContaining("archive scan failed"),
|
|
context: { project_slug: "photoreport", gitea_repo: "BitReport.pt/photoreport", component: "worker", environment: "production", release: "abc1234", sourceId: "x" },
|
|
});
|
|
expect(first!.stacktrace).toContain("reporter.test.ts");
|
|
expect(first!.context.file_path).toContain("reporter.test.ts");
|
|
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.<anonymous> (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, "");
|
|
reporter.reportError(new Error("x"), "api");
|
|
await reporter.flush();
|
|
expect(hub.sent).toHaveLength(0);
|
|
});
|
|
|
|
it("reports the log's errors, and warnings that carry an error, not the rest", async () => {
|
|
const hub = fakeHub();
|
|
const reporter = reporterWith(hub.fetchImpl);
|
|
const sink = new Writable({ write: (_c, _e, cb) => cb() });
|
|
const log = pino({ level: "info", hooks: reporter.logHooks("worker") }, sink);
|
|
log.info("worker started");
|
|
log.warn({ fileId: "f1" }, "slow");
|
|
log.warn({ err: new Error("cloud timeout"), fileId: "f2" }, "archive: image not indexed");
|
|
log.error({ err: new Error("render crashed") }, "render failed");
|
|
log.error("no error object here");
|
|
await reporter.flush();
|
|
const events = hub.sent.flatMap((s) => s.events);
|
|
expect(events.map((e) => [e.level, e.message])).toEqual([
|
|
["warning", "Error: cloud timeout"],
|
|
["error", "Error: render crashed"],
|
|
["error", "no error object here"],
|
|
]);
|
|
expect(events[0]!.context).toMatchObject({ log_message: "archive: image not indexed", fileId: "f2" });
|
|
});
|
|
});
|
|
|
|
describe("browser errors through the API", () => {
|
|
const hub = fakeHub();
|
|
const config = loadConfig({
|
|
NODE_ENV: "test",
|
|
DATABASE_URL: process.env.DATABASE_URL_TEST ?? "postgresql://[email protected]:5432/photoreport_test",
|
|
JWT_SECRET: "test-secret-test-secret-test-secret-123",
|
|
});
|
|
const db = createDb(config.DATABASE_URL);
|
|
const reporter = reporterWith(hub.fetchImpl);
|
|
const appP = buildApp({ config, db, reporter });
|
|
afterAll(async () => {
|
|
await (await appP).close();
|
|
await db.$disconnect();
|
|
});
|
|
|
|
it("passes the app's and the sites' errors on with the server's key", async () => {
|
|
const app = await appP;
|
|
let res = await app.inject({
|
|
method: "POST",
|
|
url: "/v1/client-errors",
|
|
payload: { events: [{ message: "TypeError: x is undefined", stack: "TypeError: x is undefined\n at Gallery (https://app.test/assets/index.js:1:200)", component: "app", url: "https://app.test/library", release: "abc1234" }] },
|
|
});
|
|
expect(res.statusCode).toBe(202);
|
|
res = await app.inject({ method: "POST", url: "/v1/client-errors", payload: { events: [{ message: "x", component: "evil" }] } });
|
|
expect(res.statusCode).toBe(400);
|
|
await reporter.flush();
|
|
const events = hub.sent.flatMap((s) => s.events);
|
|
expect(events).toHaveLength(1);
|
|
expect(events[0]).toMatchObject({ message: "TypeError: x is undefined", context: { component: "app", client_url: "https://app.test/library", project_slug: "photoreport" } });
|
|
expect(hub.sent[0]!.auth).toBe("Bearer ingest-key");
|
|
});
|
|
});
|