Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(electron): stop EPIPE/ECONNRESET from crashing the main process

Etienne Lescotd68ab84ae558
diff --git a/electron/main-process-errors.test.ts b/electron/main-process-errors.test.ts
new file mode 100644
index 000000000..6573ecbf3
--- /dev/null
+++ b/electron/main-process-errors.test.ts
@@ -0,0 +1,37 @@
+import { describe, expect, it } from "vitest";
+import { shouldSwallowMainProcessError } from "./main-process-errors";
+
+describe("shouldSwallowMainProcessError", () => {
+ it("swallows EPIPE on an Error object", () => {
+ const err = Object.assign(new Error("write EPIPE"), { code: "EPIPE" });
+ expect(shouldSwallowMainProcessError(err)).toBe(true);
+ });
+
+ it("swallows ECONNRESET on an Error object", () => {
+ const err = Object.assign(new Error("read ECONNRESET"), { code: "ECONNRESET" });
+ expect(shouldSwallowMainProcessError(err)).toBe(true);
+ });
+
+ it("swallows ERR_STREAM_DESTROYED on an Error object", () => {
+ const err = Object.assign(new Error("ERR_STREAM_DESTROYED"), {
+ code: "ERR_STREAM_DESTROYED",
+ });
+ expect(shouldSwallowMainProcessError(err)).toBe(true);
+ });
+
+ it("does not swallow a plain Error without an errno code", () => {
+ expect(shouldSwallowMainProcessError(new Error("boom"))).toBe(false);
+ });
+
+ it("does not swallow an Error with an unrelated code", () => {
+ const err = Object.assign(new Error("EBADFOO"), { code: "EBADFOO" });
+ expect(shouldSwallowMainProcessError(err)).toBe(false);
+ });
+
+ it("does not swallow non-Error values", () => {
+ expect(shouldSwallowMainProcessError("EPIPE")).toBe(false);
+ expect(shouldSwallowMainProcessError(null)).toBe(false);
+ expect(shouldSwallowMainProcessError(undefined)).toBe(false);
+ expect(shouldSwallowMainProcessError(42)).toBe(false);
+ });
+});
diff --git a/electron/main-process-errors.ts b/electron/main-process-errors.ts
new file mode 100644
index 000000000..f12b40053
--- /dev/null
+++ b/electron/main-process-errors.ts
@@ -0,0 +1,32 @@
+// ponytail: Electron 28+ terminates on uncaughtException/unhandledRejection.
+// Renderer reload / DevTools detach routinely produces EPIPE on in-flight
+// IPC replies — churn, not a bug. Swallow the expected codes; re-throw the
+// rest so genuine failures still kill the app.
+
+const SWALLOWED_ERROR_CODES = new Set(["EPIPE", "ECONNRESET", "ERR_STREAM_DESTROYED"]);
+
+export function shouldSwallowMainProcessError(error: unknown): boolean {
+ if (!(error instanceof Error)) return false;
+ const code = (error as NodeJS.ErrnoException).code;
+ return code !== undefined && SWALLOWED_ERROR_CODES.has(code);
+}
+
+export function installMainProcessErrorGuards(): void {
+ process.on("uncaughtException", (error) => {
+ if (shouldSwallowMainProcessError(error)) {
+ const e = error as NodeJS.ErrnoException;
+ console.warn("[main] swallowed uncaughtException:", e.code, e.message);
+ return;
+ }
+ console.error("[main] uncaughtException:", error);
+ throw error;
+ });
+ process.on("unhandledRejection", (reason) => {
+ if (shouldSwallowMainProcessError(reason)) {
+ console.warn("[main] swallowed unhandledRejection:", (reason as NodeJS.ErrnoException).code);
+ return;
+ }
+ console.error("[main] unhandledRejection:", reason);
+ throw reason instanceof Error ? reason : new Error(String(reason));
+ });
+}
diff --git a/electron/main.ts b/electron/main.ts
index 7d8c2a748..d0935b8c2 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -20,6 +20,7 @@ import {
} from "./globalShortcut";
import { mainT, setMainLocale } from "./i18n";
import { getSelectedDesktopSource, registerIpcHandlers } from "./ipc/handlers";
+import { installMainProcessErrorGuards } from "./main-process-errors";
import { acquireStableInstanceLock } from "./singleInstanceLock";
import {
createCountdownOverlayWindow,
@@ -55,6 +56,8 @@ if (process.platform === "linux") {
}
}
+installMainProcessErrorGuards();
+
export const RECORDINGS_DIR = path.join(app.getPath("userData"), "recordings");
async function ensureRecordingsDir() {