Skip to content

Commit

Permalink
main: add a lockfile, and set the user data dir while debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Jul 9, 2021
1 parent 9044b2c commit 126218b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,3 +16,4 @@ test-results/
yarn-error.log
vscode.lsif
vscode.db
/.profile-oss
2 changes: 1 addition & 1 deletion .vscode/launch.json
Expand Up @@ -219,7 +219,7 @@
"cascadeTerminateToConfigurations": [
"Attach to Extension Host"
],
"userDataDir": false,
"userDataDir": "${workspaceFolder}/.profile-oss",
"pauseForSourceMap": false,
"outFiles": [
"${workspaceFolder}/out/**/*.js"
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Expand Up @@ -4,6 +4,7 @@
"files.exclude": {
".git": true,
".build": true,
".profile-oss": true,
"**/.DS_Store": true,
"build/**/*.js": {
"when": "$(basename).ts"
Expand Down
16 changes: 16 additions & 0 deletions src/vs/code/electron-main/main.ts
Expand Up @@ -59,6 +59,7 @@ import { cwd } from 'vs/base/common/process';
import { IProtocolMainService } from 'vs/platform/protocol/electron-main/protocol';
import { ProtocolMainService } from 'vs/platform/protocol/electron-main/protocolMainService';
import { Promises } from 'vs/base/common/async';
import { toDisposable } from 'vs/base/common/lifecycle';

/**
* The main VS Code entry point.
Expand Down Expand Up @@ -339,6 +340,9 @@ class CodeMain {
throw new ExpectedError('Sent env to running instance. Terminating...');
}

const lockFile = await this.createLockfile(environmentMainService);
once(lifecycleMainService.onWillShutdown)(() => lockFile.dispose());

// Print --status usage info
if (environmentMainService.args.status) {
logService.warn('Warning: The --status argument can only be used if Code is already running. Please run it again after Code has started.');
Expand Down Expand Up @@ -419,6 +423,18 @@ class CodeMain {
lifecycleMainService.kill(exitCode);
}

private async createLockfile(env: IEnvironmentMainService) {
await FSPromises.writeFile(env.mainLockfile, String(process.pid));

return toDisposable(() => {
try {
unlinkSync(env.mainLockfile);
} catch {
// ignored
}
});
}

//#region Command line arguments utilities

private resolveArgs(): NativeParsedArgs {
Expand Down
Expand Up @@ -30,6 +30,7 @@ export interface IEnvironmentMainService extends INativeEnvironmentService {

// --- IPC
mainIPCHandle: string;
mainLockfile: string;

// --- config
sandbox: boolean;
Expand All @@ -52,6 +53,9 @@ export class EnvironmentMainService extends NativeEnvironmentService implements
@memoize
get mainIPCHandle(): string { return createStaticIPCHandle(this.userDataPath, 'main', this.productService.version); }

@memoize
get mainLockfile(): string { return join(this.userDataPath, 'code.lock'); }

@memoize
get sandbox(): boolean { return !!this.args['__sandbox']; }

Expand Down

5 comments on commit 126218b

@deepak1556
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @bpasero

We are adding a new lock file to distinguish if an instance of the application is already running, usually we get this for free if we were to use https://github.com/electron/electron/blob/main/docs/api/app.md#apprequestsingleinstancelock but since we maintain our own singleton logic this file is helpful for js-debug use case.

@bpasero
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not good, filed #128366

@bpasero
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw I don't really see how the debug extension would benefit from the requestSingleInstanceLock because the extension is not running with Electron API acess.

@deepak1556
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the extension but if the vscode core used requestSingleInstanceLock to create singleton instance then the user data dir will contain SingletonLock file which the debugger already checks in case of chrome, the api in electron is derived from chrome implementation so most of the logic is similar like creating a lock file.

@bpasero
Copy link
Member

@bpasero bpasero commented on 126218b Aug 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, didn't know. Well for other reasons, we cannot use that Electron API...

Please sign in to comment.