Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(desktop): return after app.exit when the single-instance lock is lost (#21060)

Arvin Xu2ba7e57292f2
diff --git a/apps/desktop/src/main/core/App.ts b/apps/desktop/src/main/core/App.ts
index 170561ece98..603d636306b 100644
--- a/apps/desktop/src/main/core/App.ts
+++ b/apps/desktop/src/main/core/App.ts
@@ -266,7 +266,14 @@ export class App {
const isSingle = app.requestSingleInstanceLock();
if (!isSingle) {
logger.info('Another instance is already running, exiting');
+ // `app.exit(0)` only *initiates* shutdown — without returning, the rest
+ // of this async bootstrap kept running: the duplicate instance went on
+ // to start the IPC server, gateway connection and shell services, so a
+ // machine could briefly run two live device processes for one user and
+ // both answered gateway tool calls (observed as the same runCommand
+ // executing twice with two independent process trees). Stop here.
app.exit(0);
+ return;
}
this.initDevBranding();
diff --git a/apps/desktop/src/main/core/__tests__/App.test.ts b/apps/desktop/src/main/core/__tests__/App.test.ts
index eb50d735586..3986ba998be 100644
--- a/apps/desktop/src/main/core/__tests__/App.test.ts
+++ b/apps/desktop/src/main/core/__tests__/App.test.ts
@@ -365,6 +365,25 @@ describe('App', () => {
});
});
+ describe('single instance lock', () => {
+ it('stops bootstrapping side effects when another instance holds the lock', async () => {
+ vi.mocked(electronApp.requestSingleInstanceLock).mockReturnValue(false);
+ appInstance = new App();
+ const makeAppReady = vi
+ .spyOn(appInstance as any, 'makeAppReady')
+ .mockImplementation(async () => {});
+
+ await appInstance.bootstrap();
+
+ expect(electronApp.exit).toHaveBeenCalledWith(0);
+ // The duplicate instance must not get as far as creating windows,
+ // starting the gateway connection or the shell services: two live
+ // device processes for one machine both answer gateway tool calls.
+ expect(makeAppReady).not.toHaveBeenCalled();
+ expect(appInstance.browserManager.initializeBrowsers).not.toHaveBeenCalled();
+ });
+ });
+
describe('handleWindowAllClosed', () => {
const originalPlatform = process.platform;