Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(server): ignore console writes after the terminal closes
Elian Doran6f1370368613
diff --git a/apps/server/src/services/process_errors.spec.ts b/apps/server/src/services/process_errors.spec.tsindex 92a9eab483..280489cc00 100644--- a/apps/server/src/services/process_errors.spec.ts+++ b/apps/server/src/services/process_errors.spec.ts@@ -144,3 +144,26 @@ describe("reportProcessError", () => {expect(getLogMock.error.mock.calls[2][0]).toContain("will not be reported");});});++describe("reportStdioError", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ vi.spyOn(console, "error").mockImplementation(() => {});+ });++ it("ignores a closed terminal or pipe, and reports any other write failure", async () => {+ const { markAppReady, reportStdioError } = await freshModule();+ markAppReady();++ reportStdioError(Object.assign(new Error("write EIO"), { code: "EIO" }));+ reportStdioError(Object.assign(new Error("write EPIPE"), { code: "EPIPE" }));++ expect(console.error).not.toHaveBeenCalled();+ expect(getLogMock.error).not.toHaveBeenCalled();+ expect(sendMessageToAllClientsMock).not.toHaveBeenCalled();++ reportStdioError(Object.assign(new Error("write ENOSPC"), { code: "ENOSPC" }));++ expect(sendMessageToAllClientsMock).toHaveBeenCalledWith(expect.objectContaining({ message: "write ENOSPC" }));+ });+});diff --git a/apps/server/src/services/process_errors.ts b/apps/server/src/services/process_errors.tsindex aa9884fe70..f42ce7151b 100644--- a/apps/server/src/services/process_errors.ts+++ b/apps/server/src/services/process_errors.ts@@ -55,6 +55,26 @@ let appReady = false;export function installProcessErrorHandlers() {process.on("uncaughtException", (error) => reportProcessError(UNCAUGHT_EXCEPTION, error));process.on("unhandledRejection", (reason) => reportProcessError(UNHANDLED_REJECTION, reason));+ process.stdout.on("error", reportStdioError);+ process.stderr.on("error", reportStdioError);+}++/**+ * Handles a failed write to stdout or stderr, which would otherwise surface as an uncaught exception+ * whose report is written to the same failed stream.+ *+ * `EIO` (the terminal was closed while the process kept running) and `EPIPE` (the reader of a pipe+ * exited) mean nothing reads the console any more, so the error is dropped; the log file still receives+ * every entry.+ *+ * Exported for tests — production code reaches it through {@link installProcessErrorHandlers}.+ */+export function reportStdioError(error: NodeJS.ErrnoException) {+ if (error.code === "EIO" || error.code === "EPIPE") {+ return;+ }++ reportProcessError(UNCAUGHT_EXCEPTION, error);}/**