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): bound the audio probe that hangs `openscreen sources`

EtienneLescot57731b7139d5
diff --git a/src/cli/CliSourcesRunner.tsx b/src/cli/CliSourcesRunner.tsx
index a1e464e23..354476d21 100644
--- a/src/cli/CliSourcesRunner.tsx
+++ b/src/cli/CliSourcesRunner.tsx
@@ -5,6 +5,39 @@
import { useEffect, useRef, useState } from "react";
import type { CliSourcesResult } from "@/lib/cliContracts";
+// getUserMedia and enumerateDevices both sit on the host's audio backend, and
+// neither takes a timeout. Where that backend is absent or wedged -- a
+// container, a CI runner, a server -- they can simply never settle, and the
+// catch below is no defence: it fires on rejection, not on silence. `openscreen
+// sources` hung indefinitely on roughly half of its headless runs for this
+// reason, which is worse than failing outright, because a script waits forever
+// rather than seeing an error.
+//
+// Displays and windows are what the command is for; microphone labels are a
+// nicety on top. So the audio path is best-effort and bounded, and giving up
+// reports exactly what a denied permission already reported.
+const MICROPHONE_TIMEOUT_MS = 5_000;
+
+async function withTimeout<T>(work: Promise<T>, fallback: T, what: string): Promise<T> {
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ try {
+ return await Promise.race([
+ work,
+ new Promise<T>((resolve) => {
+ timer = setTimeout(() => {
+ // Surfaced on stderr as `[renderer] …` by the CLI's console bridge.
+ console.warn(
+ `${what} did not settle in ${MICROPHONE_TIMEOUT_MS}ms; continuing without it`,
+ );
+ resolve(fallback);
+ }, MICROPHONE_TIMEOUT_MS);
+ }),
+ ]);
+ } finally {
+ clearTimeout(timer);
+ }
+}
+
async function enumerateMicrophones(): Promise<{
microphones: { label: string }[];
microphoneLabelsUnavailable: boolean;
@@ -14,19 +47,23 @@ async function enumerateMicrophones(): Promise<{
(device) => device.kind === "audioinput",
);
- let inputs = await listInputs();
+ let inputs = await withTimeout<MediaDeviceInfo[]>(listInputs(), [], "device enumeration");
// Labels are blank until a getUserMedia grant exists; a short-lived probe
// stream unlocks them without leaving anything recording.
if (inputs.length > 0 && inputs.every((device) => !device.label)) {
- let probeStream: MediaStream | null = null;
- try {
- probeStream = await navigator.mediaDevices.getUserMedia({ audio: true });
- inputs = await listInputs();
- } catch {
- // Permission denied — report devices without labels.
- } finally {
- probeStream?.getTracks().forEach((track) => track.stop());
+ const probe = navigator.mediaDevices
+ .getUserMedia({ audio: true })
+ .then((stream) => {
+ // Released here rather than in a finally, so that a stream arriving
+ // after we have stopped waiting still does not hold the device open.
+ stream.getTracks().forEach((track) => track.stop());
+ return true;
+ })
+ .catch(() => false); // Permission denied — report devices without labels.
+
+ if (await withTimeout(probe, false, "microphone permission probe")) {
+ inputs = await withTimeout(listInputs(), inputs, "device re-enumeration");
}
}