Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(desktop): keep a broken stdout or stderr pipe from crashing the app (#10566)

pseudoyudfcc633dda2e
diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts
index 668f77d270..8fcc37b559 100644
--- a/apps/desktop/src/main/index.ts
+++ b/apps/desktop/src/main/index.ts
@@ -44,6 +44,7 @@ import {
type RendererRecoveryWindow,
} from "./renderer-recovery";
import { createBestEffortDevLog } from "./dev-log";
+import { ignoreBrokenStdio } from "./stdio-guard";
import { appendMissingPathDirs } from "./path-fallback";
import {
writeFreezeBreadcrumb,
@@ -153,6 +154,9 @@ if (process.platform !== "win32") {
]);
}
+// Before anything can log: release builds write to stdout/stderr too.
+ignoreBrokenStdio();
+
const PROTOCOL = "multica";
const devLog = is.dev ? createBestEffortDevLog() : undefined;
diff --git a/apps/desktop/src/main/stdio-guard.test.ts b/apps/desktop/src/main/stdio-guard.test.ts
new file mode 100644
index 0000000000..005f5b4ac0
--- /dev/null
+++ b/apps/desktop/src/main/stdio-guard.test.ts
@@ -0,0 +1,16 @@
+// @vitest-environment node
+import { EventEmitter } from "node:events";
+import { describe, expect, it } from "vitest";
+import { ignoreBrokenStdio } from "./stdio-guard";
+
+describe("ignoreBrokenStdio", () => {
+ it("keeps a failed stdout or stderr write from becoming an uncaught exception", () => {
+ const stdout = new EventEmitter();
+ const stderr = new EventEmitter();
+ ignoreBrokenStdio([stdout, stderr]);
+ const error = Object.assign(new Error("write EPIPE"), { code: "EPIPE" });
+
+ expect(() => stdout.emit("error", error)).not.toThrow();
+ expect(() => stderr.emit("error", error)).not.toThrow();
+ });
+});
diff --git a/apps/desktop/src/main/stdio-guard.ts b/apps/desktop/src/main/stdio-guard.ts
new file mode 100644
index 0000000000..adc55ba0f5
--- /dev/null
+++ b/apps/desktop/src/main/stdio-guard.ts
@@ -0,0 +1,17 @@
+type StdioStream = {
+ on: (event: "error", listener: (error: Error) => void) => unknown;
+};
+
+/**
+ * Logging must never take the app down. A desktop launch often has no one
+ * reading its stdout/stderr (a closed terminal, an exited launcher, a session
+ * pipe whose reader is gone), so a write fails with EPIPE/EIO. The failure
+ * arrives as a stream `error`, and without a listener it becomes an uncaught
+ * exception in the main process. That covers our own `console.*` calls and
+ * dependencies that log through `console`, such as electron-updater.
+ */
+export function ignoreBrokenStdio(
+ streams: readonly StdioStream[] = [process.stdout, process.stderr],
+): void {
+ for (const stream of streams) stream.on("error", () => undefined);
+}