Skip to content

Commit 1ad5c33

Browse files
committed
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]>
1 parent 30b7367 commit 1ad5c33

File tree

4 files changed

+118
-23
lines changed

4 files changed

+118
-23
lines changed

src/goLanguageServer.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import {
6565
getWorkspaceFolderPath,
6666
removeDuplicateDiagnostics
6767
} from './util';
68+
import { Mutex } from './utils/mutex';
6869
import { getToolFromToolPath } from './utils/pathUtils';
6970

7071
export interface LanguageServerConfig {
@@ -89,9 +90,9 @@ let languageServerDisposable: vscode.Disposable;
8990
let latestConfig: LanguageServerConfig;
9091
export let serverOutputChannel: vscode.OutputChannel;
9192
export let languageServerIsRunning = false;
92-
// TODO: combine languageServerIsRunning & languageServerStartInProgress
93-
// as one languageServerStatus variable.
94-
let languageServerStartInProgress = false;
93+
94+
const languageServerStartMutex = new Mutex();
95+
9596
let serverTraceChannel: vscode.OutputChannel;
9697
let crashCount = 0;
9798

@@ -122,11 +123,6 @@ export async function startLanguageServerWithFallback(ctx: vscode.ExtensionConte
122123
}
123124
}
124125

125-
if (!activation && languageServerStartInProgress) {
126-
console.log('language server restart is already in progress...');
127-
return;
128-
}
129-
130126
const goConfig = getGoConfig();
131127
const cfg = buildLanguageServerConfig(goConfig);
132128

@@ -146,21 +142,21 @@ export async function startLanguageServerWithFallback(ctx: vscode.ExtensionConte
146142
}
147143
}
148144
}
145+
const unlock = await languageServerStartMutex.lock();
146+
try {
147+
const started = await startLanguageServer(ctx, cfg);
149148

150-
languageServerStartInProgress = true;
151-
152-
const started = await startLanguageServer(ctx, cfg);
153-
154-
// If the server has been disabled, or failed to start,
155-
// fall back to the default providers, while making sure not to
156-
// re-register any providers.
157-
if (!started && defaultLanguageProviders.length === 0) {
158-
registerDefaultProviders(ctx);
149+
// If the server has been disabled, or failed to start,
150+
// fall back to the default providers, while making sure not to
151+
// re-register any providers.
152+
if (!started && defaultLanguageProviders.length === 0) {
153+
registerDefaultProviders(ctx);
154+
}
155+
languageServerIsRunning = started;
156+
updateLanguageServerIconGoStatusBar(started, goConfig['useLanguageServer'] === true);
157+
} finally {
158+
unlock();
159159
}
160-
161-
languageServerIsRunning = started;
162-
updateLanguageServerIconGoStatusBar(started, goConfig['useLanguageServer'] === true);
163-
languageServerStartInProgress = false;
164160
}
165161

166162
// scheduleGoplsSuggestions sets timeouts for the various gopls-specific

src/goStatus.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,8 @@ export function updateLanguageServerIconGoStatusBar(started: boolean, enabled: b
129129
let text = goEnvStatusbarItem.text;
130130
let icon = '';
131131
if (text.endsWith(languageServerIcon)) {
132-
icon = languageServerIcon;
133132
text = text.substring(0, text.length - languageServerIcon.length);
134133
} else if (text.endsWith(languageServerErrorIcon)) {
135-
icon = languageServerErrorIcon;
136134
text = text.substring(0, text.length - languageServerErrorIcon.length);
137135
}
138136

src/utils/mutex.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*---------------------------------------------------------
2+
* Copyright 2021 The Go Authors. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE in the project root for license information.
4+
*--------------------------------------------------------*/
5+
6+
'use strict';
7+
8+
/* Mutex provides mutex feature by building a promise chain.
9+
10+
const m = new Mutex();
11+
12+
const unlock = await m.lock();
13+
try {
14+
// critical section
15+
} finally {
16+
unlock();
17+
}
18+
*/
19+
export class Mutex {
20+
private mutex = Promise.resolve();
21+
22+
public lock(): PromiseLike<() => void> {
23+
// Based on https://spin.atomicobject.com/2018/09/10/javascript-concurrency/
24+
25+
let x: (unlock: () => void) => void;
26+
27+
// add to the promise chain of this mutex.
28+
// When all the prior promises in the chain are resolved,
29+
// x, which will be the resolve callback of promise B,
30+
// will run and cause to unblock the waiter of promise B.
31+
this.mutex = this.mutex.then(() => {
32+
return new Promise(x); // promise A
33+
});
34+
35+
return new Promise((resolve) => { // promise B
36+
x = resolve;
37+
});
38+
// the returned Promise will resolve when all the previous
39+
// promises chained in this.mutex resolve.
40+
}
41+
}

test/unit/mutex.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*---------------------------------------------------------
2+
* Copyright 2021 The Go Authors. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE in the project root for license information.
4+
*--------------------------------------------------------*/
5+
6+
import * as assert from 'assert';
7+
import { Mutex } from '../../src/utils/mutex';
8+
9+
suite('Mutex Tests', () => {
10+
test('works for basic concurrent access', async () => {
11+
const m = new Mutex();
12+
13+
let cnt = 0;
14+
const worker = async (delay: number, count: number) => {
15+
for (let i = 0; i < count; i++) {
16+
const unlock = await m.lock();
17+
try {
18+
const cntCopy = cnt;
19+
await sleep(delay);
20+
cnt = cntCopy + 1;
21+
} finally {
22+
unlock();
23+
}
24+
}
25+
};
26+
27+
await Promise.all([worker(3, 5), worker(1, 10)]);
28+
assert.strictEqual(cnt, 15);
29+
});
30+
31+
test('works when lock holders throw errors', async () => {
32+
const m = new Mutex();
33+
34+
let cnt = 0;
35+
const worker = async (delay: number) => {
36+
const unlock = await m.lock();
37+
try {
38+
const cntCopy = cnt;
39+
await sleep(delay);
40+
cnt = cntCopy + 1;
41+
throw new Error('ooops');
42+
} finally {
43+
unlock();
44+
}
45+
};
46+
47+
const safeWorker = async (delay: number) => {
48+
try {
49+
await worker(delay);
50+
} catch (e) {
51+
// swallow the exception
52+
}
53+
};
54+
55+
await Promise.all([safeWorker(3), safeWorker(2), safeWorker(1), safeWorker(0)]);
56+
assert.strictEqual(cnt, 4);
57+
});
58+
});
59+
60+
function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); }

0 commit comments

Comments
 (0)