Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(cli): bound get-sources where it runs, and stop losing good enumerations
EtienneLescot3961c631cc38
diff --git a/electron/cli/cliMain.ts b/electron/cli/cliMain.tsindex cd1289e14..c84c5a48a 100644--- a/electron/cli/cliMain.ts+++ b/electron/cli/cliMain.ts@@ -439,10 +439,15 @@ export function runCli(command: CliCommand): void {command.jsonOutPath &&result.sources) {+ // Same as writeProjectFile above: `record --project out/x` creates+ // `out/`, so `sources -o out/x.json` has to as well. Without it a+ // fully successful enumeration is lost on every channel at once --+ // the ENOENT lands in the catch below, which clears result.success+ // and so skips printSources and the `done` payload alike.+ await fs.mkdir(path.dirname(command.jsonOutPath), { recursive: true });await fs.writeFile(command.jsonOutPath,- `${JSON.stringify(result.sources, null, 2)}-`,+ `${JSON.stringify(result.sources, null, 2)}\n`,"utf8",);}diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.tsindex 958a7031e..b6916e355 100644--- a/electron/ipc/handlers.ts+++ b/electron/ipc/handlers.ts@@ -106,6 +106,31 @@ const ALLOWED_IMPORT_VIDEO_EXTENSIONS = new Set([const PREVIEW_AUDIO_DIR = path.join(app.getPath("userData"), "preview-audio");const nativeMacCaptureEvents = new EventEmitter();+// Enumeration walks every display and window and grabs a thumbnail of each, so it+// is allowed to be slow on a loaded machine. It is not allowed to be unbounded.+// Deliberately above the CLI runner's own 20s bound, so the more specific message+// there still wins for `openscreen sources`; this is the backstop for everything+// else that calls get-sources.+const GET_SOURCES_TIMEOUT_MS = 30_000;++/**+ * Reject if `work` has not settled within `ms`.+ *+ * The abandoned promise keeps running — there is no way to cancel a+ * desktopCapturer call — so this bounds the *wait*, not the work. That is the+ * whole available remedy: an unbounded await leaves a caller with no error and no+ * way out, which is strictly worse than a late failure it can report.+ */+function withDeadline<T>(work: Promise<T>, ms: number, message: string): Promise<T> {+ let timer: ReturnType<typeof setTimeout> | undefined;+ return Promise.race([+ work,+ new Promise<never>((_resolve, reject) => {+ timer = setTimeout(() => reject(new Error(message)), ms);+ }),+ ]).finally(() => clearTimeout(timer)) as Promise<T>;+}+// Paths the user approved via file picker or project load (i.e. outside the default dirs).const approvedPaths = new Set<string>();@@ -1683,7 +1708,20 @@ export function registerIpcHandlers(}ipcMain.handle("get-sources", async (_, opts) => {- const sources = await desktopCapturer.getSources(opts);+ // desktopCapturer.getSources can never settle where the GL stack cannot be+ // reached -- a container, a CI runner, a host whose ANGLE fails to+ // initialise. Bounded here rather than per-caller because every caller has+ // the same exposure and none of them can cancel this call: the CLI runners+ // (`sources`, `record`) and the GUI pickers (SourceSelector, RecStage) all+ // await it, and a renderer-side race would only stop *waiting* while this+ // keeps running and its reply goes to nobody. Rejecting is what turns an+ // indefinite spinner into the pickers' existing error branch.+ const sources = await withDeadline(+ desktopCapturer.getSources(opts),+ GET_SOURCES_TIMEOUT_MS,+ `Desktop source enumeration did not return within ${GET_SOURCES_TIMEOUT_MS}ms. ` ++ "This usually means the display or GPU stack cannot be reached — check that a display server is available.",+ );lastEnumeratedSources = new Map(sources.map((source) => [source.id, source]));return sources.map((source) => ({id: source.id,diff --git a/src/cli/CliSourcesRunner.tsx b/src/cli/CliSourcesRunner.tsxindex b08a47022..c1d988292 100644--- a/src/cli/CliSourcesRunner.tsx+++ b/src/cli/CliSourcesRunner.tsx@@ -50,12 +50,22 @@ async function enumerateMicrophones(): Promise<{(device) => device.kind === "audioinput",);- let inputs = await withTimeout<MediaDeviceInfo[]>(+ // null, not []: an empty list is an affirmative claim that this host has no+ // microphones, and with it `microphoneLabelsUnavailable` computes to false+ // below — the exact opposite of what giving up should report, and+ // indistinguishable from a genuinely mic-less machine for anyone reading the+ // -o file. The sentinel keeps "we stopped asking" separate from "there are+ // none", and reports it the way a denied permission already reports it.+ const initialInputs = await withTimeout<MediaDeviceInfo[] | null>(listInputs(),- [],+ null,"device enumeration",MICROPHONE_TIMEOUT_MS,);+ if (initialInputs === null) {+ return { microphones: [], microphoneLabelsUnavailable: true };+ }+ let inputs = initialInputs;// Labels are blank until a getUserMedia grant exists; a short-lived probe// stream unlocks them without leaving anything recording.@@ -71,8 +81,15 @@ async function enumerateMicrophones(): Promise<{.catch(() => false); // Permission denied — report devices without labels.if (await withTimeout(probe, false, "microphone permission probe", MICROPHONE_TIMEOUT_MS)) {+ // The .catch restores what the try/catch around this call used to give:+ // withTimeout is a Promise.race, so it adopts a rejection and its+ // `fallback` never applies on that path. Without it a rejecting+ // enumerateDevices() — a detached context, or the MediaDevices connection+ // dropping after the grant — fails the whole command and discards the+ // displays and windows already enumerated above, which are what `sources`+ // is for. Labels are a nicety; keep the pre-probe list and carry on.inputs = await withTimeout(- listInputs(),+ listInputs().catch(() => inputs),inputs,"device re-enumeration",MICROPHONE_TIMEOUT_MS,