Skip to content

Commit cdd3e6c

Browse files
committed
fix(recording): keep a portal cancel that arrives with ready, before the wait
1 parent 719f97c commit cdd3e6c

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎electron/native-bridge/capture/linuxNativeCaptureSession.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,34 @@ describe("LinuxNativeCaptureSession", () => {
423423
expect(isPortalCancelled(new Error("The screen capture request was cancelled."))).toBe(false);
424424
});
425425

426+
it("keeps a cancel that arrived with `ready`, before anyone waited", async () => {
427+
const session = newSession(true);
428+
const started = session.start();
429+
await Promise.resolve();
430+
// One chunk: the reader dispatches both lines before `start()` returns.
431+
helper.stdout.write(
432+
[
433+
{ event: "ready", timestampMs: 1_000 },
434+
{
435+
event: "error",
436+
timestampMs: 1_001,
437+
code: "portal-cancelled",
438+
message: "The screen capture request was cancelled.",
439+
},
440+
]
441+
.map(
442+
(event) => `${JSON.stringify({ schemaVersion: 1, ...event })}
443+
`,
444+
)
445+
.join(""),
446+
);
447+
await started;
448+
helper.emit("exit", 1, null);
449+
450+
const error = await session.waitUntilSourceSelected().catch((reason: unknown) => reason);
451+
expect(isPortalCancelled(error)).toBe(true);
452+
});
453+
426454
it("does not take a portal failure for a cancel", async () => {
427455
const session = newSession(true);
428456
await startReady(session);

‎electron/native-bridge/capture/linuxNativeCaptureSession.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ export class LinuxNativeCaptureSession {
138138
private sourceSelectedAtMs = 0;
139139
private sourceSelectedResolve: (() => void) | null = null;
140140
private sourceSelectedReject: ((error: Error) => void) | null = null;
141+
/** The helper's error before any source was selected, for a wait registered after it. */
142+
private sourceSelectionError: Error | null = null;
141143
private armed = false;
142144

143145
private stopped: LinuxCaptureResult | null = null;
@@ -282,6 +284,11 @@ export class LinuxNativeCaptureSession {
282284
if (this.sourceSelected) {
283285
return Promise.resolve();
284286
}
287+
// `ready` and the error can share one stdout chunk, both read before the caller
288+
// gets to wait: the cancel must still reach it, not the exit that follows.
289+
if (this.sourceSelectionError) {
290+
return Promise.reject(this.sourceSelectionError);
291+
}
285292
if (!this.process) {
286293
return Promise.reject(new Error("The Linux capture helper is not running."));
287294
}
@@ -728,6 +735,7 @@ export class LinuxNativeCaptureSession {
728735
// The code rides along so a dismissed picker is not read as a failure.
729736
const error = Object.assign(new Error(payload.message), { code: payload.code });
730737
this.rejectReady(error);
738+
if (!this.sourceSelected) this.sourceSelectionError ??= error;
731739
this.sourceSelectedReject?.(error);
732740
this.sourceSelectedReject = null;
733741
this.sourceSelectedResolve = null;

0 commit comments

Comments
 (0)