Skip to content

Commit 53cabf0

Browse files
akosyakovjeanp413
authored andcommitted
[desktop] release lock if holding process is not running anymore
1 parent 5af3acb commit 53cabf0

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

extensions/gitpod-remote/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "%displayName%",
44
"description": "%description%",
55
"publisher": "gitpod",
6-
"version": "0.0.24",
6+
"version": "0.0.25",
77
"license": "MIT",
88
"preview": true,
99
"icon": "resources/gitpod.png",
@@ -229,4 +229,4 @@
229229
"gitpod-shared": "0.0.1",
230230
"vscode-nls": "^5.0.0"
231231
}
232-
}
232+
}

extensions/gitpod/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "%displayName%",
44
"description": "%description%",
55
"publisher": "gitpod",
6-
"version": "0.0.24",
6+
"version": "0.0.25",
77
"license": "MIT",
88
"preview": true,
99
"icon": "resources/gitpod.png",

extensions/gitpod/src/extension.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ interface LocalAppConfig {
3232
}
3333

3434
interface Lock {
35+
pid?: number
3536
value: string
3637
deadline: number
3738
}
@@ -57,13 +58,16 @@ export async function activate(context: vscode.ExtensionContext) {
5758
}
5859

5960
const lockPrefix = 'lock/';
60-
const fastLockTimeout = 30000;
61-
const slowLockTimeout = 300000;
61+
const checkStaleInterval = 30000;
62+
const installLockTimeout = 300000;
63+
function isLock(lock: any): lock is Lock {
64+
return !!lock && typeof lock === 'object';
65+
}
6266
function releaseStaleLocks(): void {
6367
for (const key of context.globalState.keys()) {
6468
if (key.startsWith(lockPrefix)) {
65-
const lock = context.globalState.get<Lock>(key);
66-
if (typeof lock !== 'object' || Date.now() >= lock.deadline) {
69+
const lock = context.globalState.get(key);
70+
if (!isLock(lock) || Date.now() >= lock.deadline || (typeof lock.pid === 'number' && checkRunning(lock.pid) !== true)) {
6771
const lockName = key.substr(lockPrefix.length);
6872
log(`cancel stale lock: ${lockName}`);
6973
context.globalState.update(key, undefined);
@@ -83,7 +87,7 @@ export async function activate(context: vscode.ExtensionContext) {
8387
currentLock = context.globalState.get<Lock>(lockKey);
8488
if (!currentLock) {
8589
deadline = Date.now() + timeout + updateTimeout * 2;
86-
await context.globalState.update(lockKey, <Lock>{ value, deadline });
90+
await context.globalState.update(lockKey, <Lock>{ value, deadline, pid: process.pid });
8791
}
8892
// TODO(ak) env.globaState.onDidChange instead, see https://github.com/microsoft/vscode/issues/131182
8993
await new Promise(resolve => setTimeout(resolve, updateTimeout));
@@ -110,7 +114,8 @@ export async function activate(context: vscode.ExtensionContext) {
110114
}
111115
}
112116

113-
const releaseStaleLocksTimer = setInterval(() => releaseStaleLocks(), fastLockTimeout);
117+
releaseStaleLocks();
118+
const releaseStaleLocksTimer = setInterval(() => releaseStaleLocks(), checkStaleInterval);
114119
context.subscriptions.push(new vscode.Disposable(() => clearInterval(releaseStaleLocksTimer)));
115120

116121
function checkRunning(pid: number): true | Error {
@@ -344,7 +349,7 @@ export async function activate(context: vscode.ExtensionContext) {
344349
const installationKey = 'installation/' + gitpodAuthority;
345350
const config = await withLock(gitpodAuthority, token =>
346351
ensureLocalApp(gitpodHost, configKey, installationKey, token)
347-
, slowLockTimeout, token);
352+
, installLockTimeout, token);
348353
throwIfCancelled(token);
349354
while (true) {
350355
const client = new LocalAppClient('http://localhost:' + config.apiPort, { transport: NodeHttpTransport() });

0 commit comments

Comments
 (0)