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(cli): give each CLI run its own Chromium session data

EtienneLescot294b984b145c
diff --git a/electron/cli/cliMain.ts b/electron/cli/cliMain.ts
index 1aeffd079..2a02cb285 100644
--- a/electron/cli/cliMain.ts
+++ b/electron/cli/cliMain.ts
@@ -17,6 +17,7 @@ import { ASSET_BASE_URL_ARG } from "../windows";
import { CLI_USAGE, type CliCommand } from "./args";
import { runInfoCommand, runPackCommand } from "./projectCommands";
import { createRunnerHost } from "./runnerHost";
+import { isolateCliSessionData } from "./sessionData";
import { openStdin } from "./stdin";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -244,6 +245,8 @@ export function runCli(command: CliCommand): void {
return;
}
+ isolateCliSessionData(app);
+
const output = createOutput(command.json === true);
// stdout belongs to the CLI protocol (NDJSON / progress); reroute the app's
diff --git a/electron/cli/sessionData.test.ts b/electron/cli/sessionData.test.ts
new file mode 100644
index 000000000..b23c91dfa
--- /dev/null
+++ b/electron/cli/sessionData.test.ts
@@ -0,0 +1,24 @@
+import path from "node:path";
+import { describe, expect, it } from "vitest";
+import { cliSessionDataDir, staleCliSessionDirs } from "./sessionData";
+
+describe("cliSessionDataDir", () => {
+ it("gives each process its own folder under userData, apart from the GUI's profile", () => {
+ const userData = path.join("home", "openscreen");
+ expect(cliSessionDataDir(userData, 4242)).toBe(path.join(userData, "cli-sessions", "4242"));
+ expect(cliSessionDataDir(userData, 4242)).not.toBe(cliSessionDataDir(userData, 4243));
+ });
+});
+
+describe("staleCliSessionDirs", () => {
+ it("keeps its own folder and those of live runs, and returns those of exited runs", () => {
+ const alive = new Set([200]);
+ expect(staleCliSessionDirs(["100", "200", "300"], 100, (pid) => alive.has(pid))).toEqual([
+ "300",
+ ]);
+ });
+
+ it("never touches a folder whose name is not a pid", () => {
+ expect(staleCliSessionDirs(["notes", "12a", ".DS_Store"], 1, () => false)).toEqual([]);
+ });
+});
diff --git a/electron/cli/sessionData.ts b/electron/cli/sessionData.ts
new file mode 100644
index 000000000..821e8ef65
--- /dev/null
+++ b/electron/cli/sessionData.ts
@@ -0,0 +1,63 @@
+// A CLI run keeps the app's userData (projects, recordings, settings, models) but
+// must not open the GUI's Chromium profile: its Local Storage, caches and GPU cache
+// are single-process, so whichever of the two starts first holds them and the other
+// silently runs on in-memory storage (#1137). Each CLI process gets its own
+// sessionData folder, so concurrent CLI runs do not fight over one either.
+
+import fs from "node:fs";
+import path from "node:path";
+import type { App } from "electron";
+
+const CLI_SESSIONS_DIR = "cli-sessions";
+
+export function cliSessionDataDir(userDataDir: string, pid: number): string {
+ return path.join(userDataDir, CLI_SESSIONS_DIR, String(pid));
+}
+
+/** The folders, among `names`, left behind by CLI processes that are no longer running. */
+export function staleCliSessionDirs(
+ names: string[],
+ ownPid: number,
+ isAlive: (pid: number) => boolean,
+): string[] {
+ return names.filter(
+ (name) => /^\d+$/.test(name) && Number(name) !== ownPid && !isAlive(Number(name)),
+ );
+}
+
+function isProcessAlive(pid: number): boolean {
+ try {
+ process.kill(pid, 0);
+ return true;
+ } catch (error) {
+ // EPERM: the process exists, it is just not ours to signal.
+ return (error as NodeJS.ErrnoException).code === "EPERM";
+ }
+}
+
+/**
+ * Point this process's Chromium session storage at its own folder, then delete the
+ * folders of CLI runs that have exited. Must run before `app` is ready.
+ */
+export function isolateCliSessionData(app: Pick<App, "getPath" | "setPath">): void {
+ const root = path.join(app.getPath("userData"), CLI_SESSIONS_DIR);
+ const own = cliSessionDataDir(app.getPath("userData"), process.pid);
+ try {
+ fs.mkdirSync(own, { recursive: true });
+ app.setPath("sessionData", own);
+ } catch (error) {
+ // A throw here, at module load, would surface as Electron's error dialog. The
+ // shared profile is the lesser harm.
+ console.warn("[cli] could not isolate the Chromium session data:", error);
+ return;
+ }
+
+ // A crashed run cannot clean up after itself, and a live one cannot delete what its
+ // own Chromium still holds open, so every run sweeps for the runs before it.
+ const stale = staleCliSessionDirs(fs.readdirSync(root), process.pid, isProcessAlive);
+ for (const name of stale) {
+ fs.promises.rm(path.join(root, name), { recursive: true, force: true }).catch(() => {
+ // Still locked or already gone; the next run tries again.
+ });
+ }
+}
diff --git a/electron/main.ts b/electron/main.ts
index 13b8a8ad7..7dac8e528 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -192,7 +192,8 @@ function showMainWindow() {
}
// CLI runs skip the single-instance lock so `openscreen export/record` works
-// while the GUI app is open (they share nothing but the recordings directory).
+// while the GUI app is open. They share its userData, but not its Chromium
+// profile: runCli gives each run its own sessionData (see cli/sessionData.ts).
const hasSingleInstanceLock = cliCommand ? false : app.requestSingleInstanceLock();
if (cliCommand) {