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(capture-linux): latch capture-started so an early first frame is not lost

askalf0ec5f2ca2461
diff --git a/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts b/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts
index d450a8a87..97896479c 100644
--- a/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts
+++ b/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts
@@ -295,4 +295,82 @@ describe("LinuxNativeCaptureSession", () => {
expect(session.grantedSourceKind).toBe("window");
});
+
+ /**
+ * The latch `waitUntilSourceSelected` has and this one did not.
+ *
+ * `start-native-linux-recording` arms the helper and only THEN awaits this,
+ * so `capture-started` can already have been read — the helper emits it as
+ * soon as the first frame stages, and a stdout chunk carrying it is parsed
+ * synchronously. Without a latch the answer is dropped on the floor and the
+ * returned promise has nothing left to resolve it: the recording is running,
+ * the file is filling, and the app waits for it forever.
+ */
+ it("resolves the capture wait immediately once the first frame already landed", async () => {
+ const session = newSession(true);
+ await startReady(session);
+
+ session.arm();
+ helper.emitEvent({
+ event: "capture-started",
+ timestampMs: 1_200,
+ path: "/tmp/recording.mp4",
+ width: 800,
+ height: 600,
+ fps: 30,
+ });
+ await flushStdout();
+
+ // Callers must not have to race the event to observe it.
+ await expect(session.waitUntilCapturing()).resolves.toBeUndefined();
+ });
+
+ /**
+ * The same loss through the ONE path that makes it more than theoretical:
+ * both events in a single stdout chunk. `NdjsonLineReader` hands them to the
+ * session back to back within one `data` callback, so no caller — however
+ * promptly it awaits — can install its handler in between.
+ */
+ it("resolves the capture wait when arming and the first frame share a stdout chunk", async () => {
+ const session = newSession(true);
+ await startReady(session);
+
+ session.arm();
+ helper.stdout.write(
+ `${JSON.stringify({
+ schemaVersion: 1,
+ event: "source-selected",
+ timestampMs: 1_100,
+ nodeId: 42,
+ sourceKind: "window",
+ })}\n${JSON.stringify({
+ schemaVersion: 1,
+ event: "capture-started",
+ timestampMs: 1_200,
+ path: "/tmp/recording.mp4",
+ width: 800,
+ height: 600,
+ fps: 30,
+ })}\n`,
+ );
+ await flushStdout();
+
+ await expect(session.waitUntilCapturing()).resolves.toBeUndefined();
+ });
+
+ /**
+ * The latch must not swallow a failure that arrived first: a helper that died
+ * before any frame has nothing to report, and the caller has to learn that
+ * rather than be told the capture is running.
+ */
+ it("still rejects the capture wait when the helper died before any frame", async () => {
+ const session = newSession(true);
+ await startReady(session);
+
+ session.arm();
+ helper.emit("exit", 1, null);
+ await flushStdout();
+
+ await expect(session.waitUntilCapturing()).rejects.toThrow();
+ });
});
diff --git a/electron/native-bridge/capture/linuxNativeCaptureSession.ts b/electron/native-bridge/capture/linuxNativeCaptureSession.ts
index f1582ee59..afe7a694f 100644
--- a/electron/native-bridge/capture/linuxNativeCaptureSession.ts
+++ b/electron/native-bridge/capture/linuxNativeCaptureSession.ts
@@ -76,6 +76,7 @@ export class LinuxNativeCaptureSession {
private readyReject: ((error: Error) => void) | null = null;
private readyTimer: NodeJS.Timeout | null = null;
+ private capturing = false;
private startedResolve: (() => void) | null = null;
private startedReject: ((error: Error) => void) | null = null;
@@ -226,6 +227,9 @@ export class LinuxNativeCaptureSession {
* answered the portal picker. No timeout, on purpose: see the class doc.
*/
waitUntilCapturing(): Promise<void> {
+ if (this.capturing) {
+ return Promise.resolve();
+ }
if (!this.process) {
return Promise.reject(new Error("The Linux capture helper is not running."));
}
@@ -421,6 +425,7 @@ export class LinuxNativeCaptureSession {
// before the portal picker. The recording's zero is HERE, so the
// telemetry is re-based onto it and anything from during the
// picker is dropped rather than left pinned to the start.
+ this.capturing = true;
this.cursor.rebase(payload.timestampMs);
console.info(
"[capture-linux] capture started",