mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
fix(electron): refuse captures handed off to an already-running app (#10989)
* fix(electron): refuse captures handed off to an already-running app When another instance holds the app's single-instance lock, Electron forwards the launch and its arguments to that instance. The active hook now stops the launched app at the refused requestSingleInstanceLock call and reports the refusal in its snapshot; the provider fails the capture with application_already_running before driving it. * fix(electron): stop pending actions on a late lock refusal and keep the final snapshot * docs(electron): describe late single-instance refusals during actions * test(electron): join the late-refusal capture even when the test fails
This commit is contained in:
1 parent
7134053d27
commit
483f53fdef
11 files changed
+356
-85
No files matched your search
@@ -1195,6 +1195,47 @@ const patchWebContents = (webContents, windowId) => {
|
||||
boundaries.patchWindowOpenHandler(webContents, contentsId);
|
||||
};
|
||||
|
||||
// When another instance holds the app's single-instance lock, Electron has
|
||||
// already handed this launch's arguments to that instance, and the launched
|
||||
// process is no longer the capture target. Stop the application's calling path
|
||||
// and keep the process alive so the provider reads the refusal, fails the
|
||||
// capture, and terminates the process it owns.
|
||||
let singleInstanceLockRefused = false;
|
||||
// Resolves when the lock is refused so the provider can stop actions that
|
||||
// wait for a window the stopped path would have opened. The snapshot's
|
||||
// `single_instance_lock_refused` remains the fact the provider reports.
|
||||
let announceSingleInstanceLockRefusal = () => undefined;
|
||||
globalThis.__reaElectronSingleInstanceLockRefusal = new Promise((resolve) => {
|
||||
announceSingleInstanceLockRefusal = () => resolve(true);
|
||||
});
|
||||
const singleInstanceLockRefusal = new Error(
|
||||
"Another instance holds this application's single-instance lock",
|
||||
);
|
||||
const patchSingleInstanceLock = (app) => {
|
||||
const request = app?.requestSingleInstanceLock;
|
||||
if (typeof request !== "function") return;
|
||||
app.requestSingleInstanceLock = function requestSingleInstanceLock() {
|
||||
const held = applyFunction(request, this, arguments);
|
||||
if (held !== false) return held;
|
||||
if (!singleInstanceLockRefused) {
|
||||
singleInstanceLockRefused = true;
|
||||
recordRuntime("app-lifecycle", "requestSingleInstanceLock", "blocked", {
|
||||
target: "app",
|
||||
process_type: "main",
|
||||
error: true,
|
||||
});
|
||||
// The stopped path surfaces as an uncaught exception; without a
|
||||
// listener, Electron would show a modal error dialog or exit first.
|
||||
applyFunction(nodeEventEmitterMethods.on ?? process.on, process, [
|
||||
"uncaughtException",
|
||||
() => undefined,
|
||||
]);
|
||||
announceSingleInstanceLockRefusal();
|
||||
}
|
||||
throw singleInstanceLockRefusal;
|
||||
};
|
||||
};
|
||||
|
||||
const patchWindows = (app) => {
|
||||
if (app === null || app === undefined) return;
|
||||
app.on("browser-window-created", (_event, window) => {
|
||||
@@ -1318,6 +1359,7 @@ try {
|
||||
boundaries.patchIpcRenderer(electron.ipcRenderer);
|
||||
boundaries.patchContextBridge(electron.contextBridge);
|
||||
patchWindows(electron.app);
|
||||
patchSingleInstanceLock(electron.app);
|
||||
boundaries.patchUtilityProcess(electron.utilityProcess);
|
||||
boundaries.patchShell(electron.shell);
|
||||
boundaries.patchSessionManager(electron.session, electron.app);
|
||||
@@ -1388,4 +1430,5 @@ globalThis.__reaElectronActiveSnapshot = () => ({
|
||||
observed_ipc: observedIpc,
|
||||
observed_runtime: observedRuntime,
|
||||
hook_error: globalThis.__reaElectronActiveHookError === true,
|
||||
single_instance_lock_refused: singleInstanceLockRefused,
|
||||
});
|
||||
Reference in new issue
Block a user