-
Notifications
You must be signed in to change notification settings - Fork 29.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Electron: investigate to use requestSingleInstanceLock()
API
#97626
Comments
Electron's The general shape of the callback is determined at The Chromium implementation is process-dependent. The following links are for the Windows implementation: The part that transfers the The callback that takes in those same Bonus: I was getting confused by what all the bind functions were doing. It's explained well at https://source.chromium.org/chromium/chromium/src/+/main:base/bind.h |
Update: Electron has a copy of those files here: https://github.com/electron/electron/tree/main/chromium_src/chrome/browser Removing chromium label. |
Adding relevant upstream issues,
electron/electron#18434
|
I'm wondering how one of the callbacks should be handled if we're implementing first->second instance data passing on the Chromium side. app.on('second-instance', (event, commandLine, workingDirectory, additionalData, ackCallback) => {
console.log('Received data from first instance');
console.log(JSON.stringify(additionalData));
console.log('Sending data back to the second instance before timeout expires');
ackCallback(returnObj);
}); I added a new parameter |
The response callback implementation depends on how the underlying platform communication is handled, first lets consider the Posix implementation.
For a scenario to send data from the first process to the second, currently the Lets consider the windows implementation
|
Refs: electron/electron#31460 |
Fixed a bug upstream for the posix impl so I'm hoping to get that merged first before 31460. |
Currently fixing a bug where ConnectNamedPipe sometimes hangs, because the other process doesn't actually go through a code path that connects to that waiting pipe. |
Adding @bpasero comments about the benefits from using the API:
|
I looked again into how we use our launch service for when a second instance launches and feel we can probably explore adopting
The first one should not be needed anymore once we switch over, it is a workaround for allowing a second instance to bring a first instance into focus. I am sure, Chrome supports that in their implementation (and I think @rzhao271 confirmed this). The second one can easily be worked around I would argue. We could simply send a temporary file path from second to first instance asking to write to the file for status info and then read from it. We do a similar trick when we adopted the As such, I think we could do an exploration for adopting
Thoughts? Update:
But I feel this is comparable to the task of writing diagnostics into a file and letting the second instance simply read from it. |
Super hacky solution that actually already seems to work 🚀 const result = app.requestSingleInstanceLock({
args: JSON.stringify(environmentMainService.args),
env: JSON.stringify(process.env)
});
if (!result) {
instantiationService.invokeFunction(this.quit);
return;
} added right as first line into: vscode/src/vs/code/electron-main/main.ts Line 116 in 0edaded
And then app.on('second-instance', (event: Event, argv: string[], workingDirectory, additionalData: any) => {
this.start(JSON.parse(additionalData.args), JSON.parse(additionalData.env));
}); added into the constructor of
|
requestSingleInstanceLock()
API
Interesting, looks like I found a blocker: with In other words, it looks like the lock is partitioned also by user and not just process, but in our solution we are able to detect this case and show a warning dialog that another instance is running as admin. Since we do not enforce a separate user data dir and extensions dir (which maybe Chrome does?), we cannot really support this scenario. Update: on Windows it is even worse, the second instance tries to talk to the first instance but fails silently. |
Good catch, maybe the API can provide an option to allow this default behavior or abort and surface as error to users.
|
Are we planning to pursue this in upcoming milestones, if not I suggest we close this one out. |
We closed this issue because we don't plan to address it in the foreseeable future. If you disagree and feel that this issue is crucial: we are happy to listen and to reconsider. If you wonder what we are up to, please see our roadmap and issue reporting guidelines. Thanks for your understanding, and happy coding! |
Currently VSCode is implementing it's own solution for preventing a second instance from starting. Electron provides API to do the same but there are differences: https://github.com/electron/electron/blob/7-1-x/docs/api/app.md#apprequestsingleinstancelock
Things we need:
code --status
[1][1]
code --status
will print information about the active window in the first instance to the console of the second instance and thus requires access to the workspace, including remote scenarios.The text was updated successfully, but these errors were encountered: