Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions client-node-tests/src/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,205 @@ suite('Server tests', () => {
}
}).timeout(10000);

function createRetryClient(server: string, initializationFailedHandler?: lsclient.InitializationFailedHandler): lsclient.LanguageClient {
// The client owns its output channel: it logs the server's exit after start() and
// stop() settle, and recreates a channel it already disposed on stop.
return new lsclient.LanguageClient('initialization-retry', 'Initialization retry', {
module: path.join(__dirname, './servers', server),
transport: lsclient.TransportKind.ipc,
}, {
initializationFailedHandler,
errorHandler: {
error: () => ({ action: lsclient.ErrorAction.Continue }),
closed: () => ({ action: lsclient.CloseAction.DoNotRestart, handled: true }),
}
});
}

async function withUnhandledRejections(body: (unhandled: unknown[]) => Promise<void>): Promise<void> {
const unhandled: unknown[] = [];
const onUnhandled = (reason: unknown) => unhandled.push(reason);
process.on('unhandledRejection', onUnhandled);
try {
await body(unhandled);
} finally {
process.off('unhandledRejection', onUnhandled);
}
}

test('Start resolves once a retried initialization succeeds', async () => {
const handler = sinon.stub().returns(true);
const client = createRetryClient('retryInitializeServer.js', handler);
try {
await withUnhandledRejections(async (unhandled) => {
await client.start();
assert.strictEqual(handler.callCount, 1);
assert.strictEqual(client.state, lsclient.State.Running);
assert.strictEqual(client.isRunning(), true);
// A second start shares the settled start promise.
await client.start();
await client.stop();
assert.strictEqual(client.state, lsclient.State.Stopped);
assert.deepStrictEqual(unhandled, []);
});
} finally {
await client.dispose();
client.diagnostics?.dispose();
}
}).timeout(10000);

test('Start rejects when a retried initialization fails again', async () => {
const handler = sinon.stub();
handler.onFirstCall().returns(true);
handler.returns(false);
// The server fails the retry with a distinct error, so the final error is observable.
const client = createRetryClient('retryThenFailInitializeServer.js', handler);
const stopped = new Promise<void>(resolve => {
const listener = client.onDidChangeState(event => {
if (event.newState === lsclient.State.Stopped) {
listener.dispose();
resolve();
}
});
});
const stop = sinon.spy(client, 'stop');
try {
await withUnhandledRejections(async (unhandled) => {
await assert.rejects(client.start(), /Initialization failed after retry/);
assert.strictEqual(handler.callCount, 2);
assert.match(String(handler.secondCall.args[0]), /Initialization failed after retry/);
assert.strictEqual(client.state, lsclient.State.StartFailed);
assert.strictEqual(stop.callCount, 1);
await stopped;
assert.notStrictEqual(client.state, lsclient.State.Running);
assert.deepStrictEqual(unhandled, []);
});
} finally {
stop.restore();
await client.dispose();
client.diagnostics?.dispose();
}
}).timeout(10000);

test('Start waits for the retry choice of a retryable initialization error', async () => {
const client = createRetryClient('retryInitializeServer.js');
const showErrorMessage = sinon.stub(vscode.window, 'showErrorMessage').resolves({ title: 'Retry', id: 'retry' } as any);
try {
await withUnhandledRejections(async (unhandled) => {
await client.start();
assert.deepStrictEqual(showErrorMessage.getCalls().map(call => call.args), [['Initialization refused', { title: 'Retry', id: 'retry' }]]);
assert.strictEqual(client.state, lsclient.State.Running);
await client.stop();
assert.deepStrictEqual(unhandled, []);
});
} finally {
showErrorMessage.restore();
await client.dispose();
client.diagnostics?.dispose();
}
}).timeout(10000);

test('Start rejects when the retry choice of a retryable initialization error is dismissed', async () => {
const client = createRetryClient('retryInitializeServer.js');
const showErrorMessage = sinon.stub(vscode.window, 'showErrorMessage').resolves(undefined);
const stopped = new Promise<void>(resolve => {
const listener = client.onDidChangeState(event => {
if (event.newState === lsclient.State.Stopped) {
listener.dispose();
resolve();
}
});
});
const stop = sinon.spy(client, 'stop');
try {
await withUnhandledRejections(async (unhandled) => {
await assert.rejects(client.start(), /Initialization refused/);
// The retry choice is offered once; the failed start is then reported as well.
assert.deepStrictEqual(showErrorMessage.firstCall.args, ['Initialization refused', { title: 'Retry', id: 'retry' }]);
assert.strictEqual(showErrorMessage.getCalls().filter(call => call.args[0] === 'Initialization refused').length, 1);
assert.strictEqual(client.state, lsclient.State.StartFailed);
assert.strictEqual(stop.callCount, 1);
await stopped;
assert.deepStrictEqual(unhandled, []);
});
} finally {
stop.restore();
showErrorMessage.restore();
await client.dispose();
client.diagnostics?.dispose();
}
}).timeout(10000);

test('Start rejects with the final error when the retry chosen for an initialization error fails', async () => {
const client = createRetryClient('retryThenFailInitializeServer.js');
const showErrorMessage = sinon.stub(vscode.window, 'showErrorMessage');
showErrorMessage.withArgs('Initialization refused').resolves({ title: 'Retry', id: 'retry' } as any);
showErrorMessage.resolves(undefined);
const stopped = new Promise<void>(resolve => {
const listener = client.onDidChangeState(event => {
if (event.newState === lsclient.State.Stopped) {
listener.dispose();
resolve();
}
});
});
const stop = sinon.spy(client, 'stop');
try {
await withUnhandledRejections(async (unhandled) => {
await assert.rejects(client.start(), /Initialization failed after retry/);
// Retry is offered for the first error only; the final error can't be retried.
assert.deepStrictEqual(showErrorMessage.withArgs('Initialization refused').getCalls().map(call => call.args), [['Initialization refused', { title: 'Retry', id: 'retry' }]]);
assert.strictEqual(showErrorMessage.withArgs('Initialization failed after retry', sinon.match.object).callCount, 0);
assert.strictEqual(client.state, lsclient.State.StartFailed);
// Only the failed retry stops the client, which terminates the server process.
assert.strictEqual(stop.callCount, 1);
await stopped;
assert.strictEqual(stop.callCount, 1);
assert.deepStrictEqual(unhandled, []);
});
} finally {
stop.restore();
showErrorMessage.restore();
await client.dispose();
client.diagnostics?.dispose();
}
}).timeout(10000);

test('Start rejects when the connection closes while the retry choice is shown', async () => {
const client = createRetryClient('exitOnInitializeRetryServer.js');
// Keep the retry choice open until the test answers it.
let answer!: (item: vscode.MessageItem | undefined) => void;
const choice = new Promise<vscode.MessageItem | undefined>(resolve => answer = resolve);
const showErrorMessage = sinon.stub(vscode.window, 'showErrorMessage');
showErrorMessage.withArgs('Initialization refused').returns(choice as any);
showErrorMessage.resolves(undefined);
const initialize = sinon.spy(client as any, 'initialize');
// The client must not stop after the close: the close handling already did. A failed
// start can't be stopped either, so the stub also keeps dispose() from rejecting.
const stop = sinon.stub(client, 'stop').resolves();
try {
await withUnhandledRejections(async (unhandled) => {
await assert.rejects(client.start(), /Connection to server got closed before initialization was retried/);
assert.strictEqual(showErrorMessage.withArgs('Initialization refused').callCount, 1);
assert.strictEqual(client.state, lsclient.State.StartFailed);
// Answering the stale choice afterwards changes nothing.
answer({ title: 'Retry', id: 'retry' } as vscode.MessageItem);
await new Promise(resolve => setTimeout(resolve, 100));
assert.strictEqual(client.state, lsclient.State.StartFailed);
assert.strictEqual(stop.callCount, 0);
assert.strictEqual(initialize.callCount, 1);
assert.deepStrictEqual(unhandled, []);
});
} finally {
answer(undefined);
initialize.restore();
showErrorMessage.restore();
await client.dispose();
stop.restore();
client.diagnostics?.dispose();
}
}).timeout(10000);

test('Stop fails if server crashes after shutdown request', async () => {
const serverOptions: lsclient.ServerOptions = {
module: path.join(__dirname, './servers/crashOnShutdownServer.js'),
Expand Down
12 changes: 12 additions & 0 deletions client-node-tests/src/servers/exitOnInitializeRetryServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createConnection, InitializeError, ResponseError } from 'vscode-languageserver/node';
const connection = createConnection();
connection.onInitialize(() => {
// Ask the client to retry, then exit while the client asks the user whether to retry.
setTimeout(() => process.exit(0), 100);
return new ResponseError<InitializeError>(1, 'Initialization refused', { retry: true });
});
connection.listen();
16 changes: 16 additions & 0 deletions client-node-tests/src/servers/retryInitializeServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createConnection, InitializeError, ResponseError } from 'vscode-languageserver/node';
const connection = createConnection();
let refused = false;
connection.onInitialize(() => {
// Refuse the first initialization and ask the client to retry, then accept the retry.
if (!refused) {
refused = true;
return new ResponseError<InitializeError>(1, 'Initialization refused', { retry: true });
}
return { capabilities: {} };
});
connection.listen();
17 changes: 17 additions & 0 deletions client-node-tests/src/servers/retryThenFailInitializeServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createConnection, InitializeError, ResponseError } from 'vscode-languageserver/node';
const connection = createConnection();
let refused = false;
connection.onInitialize(() => {
// Refuse the first initialization and ask the client to retry, then fail the retry
// with a distinct error that can't be retried.
if (!refused) {
refused = true;
return new ResponseError<InitializeError>(1, 'Initialization refused', { retry: true });
}
return new ResponseError<InitializeError>(1, 'Initialization failed after retry', { retry: false });
});
connection.listen();
41 changes: 29 additions & 12 deletions client/src/common/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,11 @@ export abstract class BaseLanguageClient implements FeatureClient<Middleware, La
await this.initialize(connection);
resolve();
} catch (error) {
this.$state = ClientState.StartFailed;
// If the connection closed during the start, the close handling may already have
// moved the client on, for example to a restart. Only a current start fails it.
if (this._onStart === promise) {
this.$state = ClientState.StartFailed;
}
this.error(`${this._name} client: couldn't create connection to server.`, error, 'force');
reject(error);
}
Expand Down Expand Up @@ -1550,27 +1554,32 @@ export abstract class BaseLanguageClient implements FeatureClient<Middleware, La

return result;
} catch (error: any) {
// A retry settles the start of the client, so its outcome is returned rather
// than run in the background after start() has already rejected.
if (this._clientOptions.initializationFailedHandler) {
if (this._clientOptions.initializationFailedHandler(error)) {
void this.initialize(connection);
} else {
this.stopAfterFailedInitialize();
return this.initialize(connection);
}
} else if (error instanceof ResponseError && error.data && error.data.retry) {
void Window.showErrorMessage(error.message, { title: 'Retry', id: 'retry' }).then(item => {
if (item && item.id === 'retry') {
void this.initialize(connection);
} else {
this.stopAfterFailedInitialize();
}
});
// The connection can close while the message is shown. The close handling then
// already cleaned up or restarted the client, so settle the start without
// waiting for the user and ignore the stale answer.
const item = await Promise.race([
Window.showErrorMessage(error.message, { title: 'Retry', id: 'retry' }),
connection.closed.then((): never => {
throw new Error(`Connection to server got closed before initialization was retried.`);
})
]);
if (item && item.id === 'retry') {
return this.initialize(connection);
Comment thread
trivikr marked this conversation as resolved.
Resolved
}
} else {
if (error && error.message) {
void Window.showErrorMessage(error.message);
}
this.error('Server initialization failed.', error);
this.stopAfterFailedInitialize();
}
this.stopAfterFailedInitialize();
throw error;
}
}
Expand Down Expand Up @@ -2451,6 +2460,11 @@ export class LanguageClient extends BaseLanguageClient {

interface Connection {

/**
* Resolves once the connection got closed, also for callers that ask after the fact.
*/
readonly closed: Promise<void>;

listen(): void;

sendRequest<R, PR, E, RO>(type: ProtocolRequestType0<R, PR, E, RO>, token?: CancellationToken): Promise<R>;
Expand Down Expand Up @@ -2544,8 +2558,11 @@ function createConnection(input: MessageReader, output: MessageWriter, errorHand
const connection = createProtocolConnection(input, output, logger, options);
connection.onError((data) => { errorHandler(data[0], data[1], data[2]); });
connection.onClose(closeHandler);
const closed = new Promise<void>(resolve => connection.onClose(() => resolve()));
const result: Connection = {

closed,

listen: (): void => connection.listen(),

sendRequest: connection.sendRequest,
Expand Down
Loading