@@ -32,6 +32,7 @@ interface LocalAppConfig {
32
32
}
33
33
34
34
interface Lock {
35
+ pid ?: number
35
36
value : string
36
37
deadline : number
37
38
}
@@ -57,13 +58,16 @@ export async function activate(context: vscode.ExtensionContext) {
57
58
}
58
59
59
60
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
+ }
62
66
function releaseStaleLocks ( ) : void {
63
67
for ( const key of context . globalState . keys ( ) ) {
64
68
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 ) ) {
67
71
const lockName = key . substr ( lockPrefix . length ) ;
68
72
log ( `cancel stale lock: ${ lockName } ` ) ;
69
73
context . globalState . update ( key , undefined ) ;
@@ -83,7 +87,7 @@ export async function activate(context: vscode.ExtensionContext) {
83
87
currentLock = context . globalState . get < Lock > ( lockKey ) ;
84
88
if ( ! currentLock ) {
85
89
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 } ) ;
87
91
}
88
92
// TODO(ak) env.globaState.onDidChange instead, see https://github.com/microsoft/vscode/issues/131182
89
93
await new Promise ( resolve => setTimeout ( resolve , updateTimeout ) ) ;
@@ -110,7 +114,8 @@ export async function activate(context: vscode.ExtensionContext) {
110
114
}
111
115
}
112
116
113
- const releaseStaleLocksTimer = setInterval ( ( ) => releaseStaleLocks ( ) , fastLockTimeout ) ;
117
+ releaseStaleLocks ( ) ;
118
+ const releaseStaleLocksTimer = setInterval ( ( ) => releaseStaleLocks ( ) , checkStaleInterval ) ;
114
119
context . subscriptions . push ( new vscode . Disposable ( ( ) => clearInterval ( releaseStaleLocksTimer ) ) ) ;
115
120
116
121
function checkRunning ( pid : number ) : true | Error {
@@ -344,7 +349,7 @@ export async function activate(context: vscode.ExtensionContext) {
344
349
const installationKey = 'installation/' + gitpodAuthority ;
345
350
const config = await withLock ( gitpodAuthority , token =>
346
351
ensureLocalApp ( gitpodHost , configKey , installationKey , token )
347
- , slowLockTimeout , token ) ;
352
+ , installLockTimeout , token ) ;
348
353
throwIfCancelled ( token ) ;
349
354
while ( true ) {
350
355
const client = new LocalAppClient ( 'http://localhost:' + config . apiPort , { transport : NodeHttpTransport ( ) } ) ;
0 commit comments