-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release] src/goLanguageServer: protect language restart with mutex
We have used the languageServerStartInProgress flag to prevent issuing another language start request while the previous language start up process is in progress and is blocked. This prevented the race of multiple language start calls. However, this can cause important language client restart request to be skipped. Consider this scenario: - user modifies the setting to enable gopls and that triggers a call to startLanguageServerWithFallback. While this is waiting on startLanguageServer that will run with cfg.enabled = true. - user modifies the stting to disable gopls, and that triggers another call to startLanguageServerWithFallback. - the second startLanguageServerWithFallback will skip startLanguageServer because languageServerStartInProgress is true. - As a result, we will fail to disable the language server. This change fixes the issue by using a new Mutex to protect startLanguageServer. With the change, the second call won't skip startLanguageServer, but will be resumed when the previous startLanguageServer call is finished. This change also fixes the bug in src/goStatus that produced incorrect language server status icon when language server is disabled. Fixes #1132 Change-Id: I4435d41b843032ff8f675ea95aac002d9ba79b4b Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/288352 Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]> Trust: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]> (cherry picked from commit 1ad5c33) Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/289234
- Loading branch information
Showing
4 changed files
with
118 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright 2021 The Go Authors. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for license information. | ||
*--------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
/* Mutex provides mutex feature by building a promise chain. | ||
const m = new Mutex(); | ||
const unlock = await m.lock(); | ||
try { | ||
// critical section | ||
} finally { | ||
unlock(); | ||
} | ||
*/ | ||
export class Mutex { | ||
private mutex = Promise.resolve(); | ||
|
||
public lock(): PromiseLike<() => void> { | ||
// Based on https://spin.atomicobject.com/2018/09/10/javascript-concurrency/ | ||
|
||
let x: (unlock: () => void) => void; | ||
|
||
// add to the promise chain of this mutex. | ||
// When all the prior promises in the chain are resolved, | ||
// x, which will be the resolve callback of promise B, | ||
// will run and cause to unblock the waiter of promise B. | ||
this.mutex = this.mutex.then(() => { | ||
return new Promise(x); // promise A | ||
}); | ||
|
||
return new Promise((resolve) => { // promise B | ||
x = resolve; | ||
}); | ||
// the returned Promise will resolve when all the previous | ||
// promises chained in this.mutex resolve. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright 2021 The Go Authors. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for license information. | ||
*--------------------------------------------------------*/ | ||
|
||
import * as assert from 'assert'; | ||
import { Mutex } from '../../src/utils/mutex'; | ||
|
||
suite('Mutex Tests', () => { | ||
test('works for basic concurrent access', async () => { | ||
const m = new Mutex(); | ||
|
||
let cnt = 0; | ||
const worker = async (delay: number, count: number) => { | ||
for (let i = 0; i < count; i++) { | ||
const unlock = await m.lock(); | ||
try { | ||
const cntCopy = cnt; | ||
await sleep(delay); | ||
cnt = cntCopy + 1; | ||
} finally { | ||
unlock(); | ||
} | ||
} | ||
}; | ||
|
||
await Promise.all([worker(3, 5), worker(1, 10)]); | ||
assert.strictEqual(cnt, 15); | ||
}); | ||
|
||
test('works when lock holders throw errors', async () => { | ||
const m = new Mutex(); | ||
|
||
let cnt = 0; | ||
const worker = async (delay: number) => { | ||
const unlock = await m.lock(); | ||
try { | ||
const cntCopy = cnt; | ||
await sleep(delay); | ||
cnt = cntCopy + 1; | ||
throw new Error('ooops'); | ||
} finally { | ||
unlock(); | ||
} | ||
}; | ||
|
||
const safeWorker = async (delay: number) => { | ||
try { | ||
await worker(delay); | ||
} catch (e) { | ||
// swallow the exception | ||
} | ||
}; | ||
|
||
await Promise.all([safeWorker(3), safeWorker(2), safeWorker(1), safeWorker(0)]); | ||
assert.strictEqual(cnt, 4); | ||
}); | ||
}); | ||
|
||
function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); } |