Skip to content

The open-source app everyone uses to manage agents at work

fix(server): stop reporting expected managed-cloud transients to Sentry (#13323)

Devin Foleya59f5a8adc12
diff --git a/server/src/__tests__/cloud-tenant-transient-db-retry.test.ts b/server/src/__tests__/cloud-tenant-transient-db-retry.test.ts
index 70ada7397d..94b9c5a8ef 100644
--- a/server/src/__tests__/cloud-tenant-transient-db-retry.test.ts
+++ b/server/src/__tests__/cloud-tenant-transient-db-retry.test.ts
@@ -33,7 +33,7 @@ describe("isTransientDbConnectionError", () => {
});
describe("retryOnTransientDbConnectionError", () => {
- it("retries exactly once after a transient closed connection", async () => {
+ it("retries after a transient closed connection", async () => {
let calls = 0;
const result = await retryOnTransientDbConnectionError(async () => {
calls += 1;
@@ -44,6 +44,19 @@ describe("retryOnTransientDbConnectionError", () => {
expect(calls).toBe(2);
});
+ it("survives a pool-wide recycle where the first replay draws another dead socket", async () => {
+ // A suspending pooled endpoint kills every pooled socket at once, so
+ // the first replay can fail identically to the original attempt.
+ let calls = 0;
+ const result = await retryOnTransientDbConnectionError(async () => {
+ calls += 1;
+ if (calls <= 2) throw driverClosedError("CONNECTION_CLOSED");
+ return "ok";
+ });
+ expect(result).toBe("ok");
+ expect(calls).toBe(3);
+ });
+
it("propagates a non-transient failure without retrying", async () => {
let calls = 0;
await expect(
@@ -55,7 +68,7 @@ describe("retryOnTransientDbConnectionError", () => {
expect(calls).toBe(1);
});
- it("propagates the second failure when the retry also dies", async () => {
+ it("propagates the failure once the replay budget is spent", async () => {
let calls = 0;
await expect(
retryOnTransientDbConnectionError(async () => {
@@ -63,6 +76,6 @@ describe("retryOnTransientDbConnectionError", () => {
throw driverClosedError("CONNECTION_CLOSED");
}),
).rejects.toThrow("Failed query");
- expect(calls).toBe(2);
+ expect(calls).toBe(3);
});
});
diff --git a/server/src/__tests__/error-handler.test.ts b/server/src/__tests__/error-handler.test.ts
index fd6af917db..52bf838559 100644
--- a/server/src/__tests__/error-handler.test.ts
+++ b/server/src/__tests__/error-handler.test.ts
@@ -64,6 +64,24 @@ describe("errorHandler", () => {
expect(res.__errorContext?.error?.message).toBe("boom");
});
+ it("ends aborted client requests without reporting a crash", () => {
+ // A closed tab or dropped network surfaces as `Error: aborted` with
+ // ECONNRESET; there is no server fault and nobody left to answer.
+ const req = makeReq();
+ const res = { ...makeRes(), end: vi.fn(), headersSent: false } as any;
+ (res.status as ReturnType<typeof vi.fn>).mockReturnValue(res);
+ const next = vi.fn() as unknown as NextFunction;
+ const err = Object.assign(new Error("aborted"), { code: "ECONNRESET" });
+
+ errorHandler(err, req, res, next);
+
+ expect(res.status).toHaveBeenCalledWith(499);
+ expect(res.end).toHaveBeenCalled();
+ expect(res.json).not.toHaveBeenCalled();
+ expect(captureExceptionMock).not.toHaveBeenCalled();
+ expect(telemetryMocks.trackErrorHandlerCrash).not.toHaveBeenCalled();
+ });
+
it("exposes raw 500 messages for trusted Cloud tenant imports", () => {
const req = {
...makeReq(),
diff --git a/server/src/__tests__/startup-refusals.test.ts b/server/src/__tests__/startup-refusals.test.ts
index 763b91ac32..619c12f39e 100644
--- a/server/src/__tests__/startup-refusals.test.ts
+++ b/server/src/__tests__/startup-refusals.test.ts
@@ -16,13 +16,16 @@ describe("migrationRefusalError", () => {
expect(error.message).toContain("Refusing to start");
});
- it("keeps pending migrations on a migrated database as a plain, always-reported error", () => {
+ it("classifies pending migrations on a migrated database as a supervised-transient refusal", () => {
+ // Managed fleet rolls deliver the new app image before the migration
+ // runner, so a briefly-behind schema is the routine mid-upgrade phase
+ // under a supervisor — suppressed there, still reported self-hosted.
const error = migrationRefusalError(
{ appliedMigrations: ["0000_init.sql"], tableCount: 41 },
message,
);
- expect(error).toBeInstanceOf(Error);
- expect(error).not.toBeInstanceOf(StartupRefusalError);
+ expect(error).toBeInstanceOf(StartupRefusalError);
+ expect((error as StartupRefusalError).kind).toBe("schema-migration-pending");
});
it("treats an empty journal beside existing tables as drift, not a fresh database", () => {
diff --git a/server/src/middleware/auth.ts b/server/src/middleware/auth.ts
index 5d06c5dcd4..bbf146c068 100644
--- a/server/src/middleware/auth.ts
+++ b/server/src/middleware/auth.ts
@@ -557,16 +557,22 @@ export function isTransientDbConnectionError(error: unknown): boolean {
}
/**
- * Runs `run` and retries it exactly once when it fails on a transient
- * closed-connection error. Callers must pass an idempotent operation.
- * Exported for tests.
+ * Runs `run` and retries it up to twice when it fails on a transient
+ * closed-connection error. Two replays, not one: when a pooled endpoint
+ * suspends or recycles, EVERY pooled socket is dead at once, so the first
+ * replay can draw another stale socket from the pool and fail identically
+ * (observed 2026-09-12: retried actor resolution still surfacing
+ * CONNECTION_CLOSED). The short pause gives the driver time to notice and
+ * re-dial. Callers must pass an idempotent operation. Exported for tests.
*/
export async function retryOnTransientDbConnectionError<T>(run: () => Promise<T>): Promise<T> {
- try {
- return await run();
- } catch (error) {
- if (!isTransientDbConnectionError(error)) throw error;
- return run();
+ for (let attempt = 0; ; attempt += 1) {
+ try {
+ return await run();
+ } catch (error) {
+ if (attempt >= 2 || !isTransientDbConnectionError(error)) throw error;
+ await new Promise((resolve) => setTimeout(resolve, 50 * (attempt + 1)));
+ }
}
}
diff --git a/server/src/middleware/error-handler.ts b/server/src/middleware/error-handler.ts
index e759f9a964..c82a9211b4 100644
--- a/server/src/middleware/error-handler.ts
+++ b/server/src/middleware/error-handler.ts
@@ -237,6 +237,20 @@ export function errorHandler(
}
const rootError = err instanceof Error ? err : new Error(String(err));
+
+ // The client tore down the connection mid-request (closed tab, dropped
+ // mobile network, cancelled upload): Node surfaces it as `Error: aborted`
+ // with ECONNRESET. There is no server fault to report and nobody left to
+ // answer, so skip the error sinks and just close out the response.
+ if (
+ rootError.message === "aborted" &&
+ (rootError as NodeJS.ErrnoException).code === "ECONNRESET"
+ ) {
+ if (!res.headersSent) res.status(499);
+ res.end();
+ return;
+ }
+
const reportableError = sanitizeSecretSensitiveError(req, rootError);
attachErrorContext(
req,
diff --git a/server/src/startup-refusals.ts b/server/src/startup-refusals.ts
index 08c3153ffd..38816ddae2 100644
--- a/server/src/startup-refusals.ts
+++ b/server/src/startup-refusals.ts
@@ -22,6 +22,7 @@
export type StartupRefusalKind =
| "schema-not-yet-migrated"
+ | "schema-migration-pending"
| "database-contract-unmet";
/**
@@ -53,9 +54,23 @@ export function migrationRefusalError(
message: string,
): Error {
const neverMigrated = state.appliedMigrations.length === 0 && state.tableCount === 0;
- return neverMigrated
- ? new StartupRefusalError("schema-not-yet-migrated", message)
- : new Error(message);
+ if (neverMigrated) return new StartupRefusalError("schema-not-yet-migrated", message);
+ // A database with applied HISTORY and newer pending files is normal
+ // mid-upgrade under a supervisor: managed fleet rolls deliver the new
+ // app image before the migration runner, so every upgraded stack
+ // briefly boots ahead of its schema and crash-loops until the
+ // supervisor migrates and restarts it (observed: ~11 events per
+ // container, hundreds per fleet roll). It still refuses, logs, and
+ // exits nonzero; only the Sentry capture is skipped — and only when
+ // `PAPERCLIP_CLOUD_API_ORIGIN` marks the deployment as supervised
+ // (`shouldReportStartupFailure`). Self-hosted deployments keep
+ // reporting.
+ if (state.appliedMigrations.length > 0) {
+ return new StartupRefusalError("schema-migration-pending", message);
+ }
+ // An empty or wiped migration journal beside real tables is genuine
+ // drift with no supervisor remedy on the way; it must keep reporting.
+ return new Error(message);
}
/**