You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow initializationOptions and fillInitializeParams to be async - #1958
Extensions can now do async work to compute what the client sends in the initialize request. Today initializationOptions can be a function, but its result is sent as is, so a returned promise reaches the server as {}. fillInitializeParams on the client and on features returns void, so a feature can't wait for anything either, although initialize itself is already async.
Changes
LanguageClientOptions.initializationOptions accepts a function that returns a promise. The client awaits the result before it sends initialize. As before, the function is called each time the client initializes the server, so a restart computes the options again. If the function throws or the promise rejects, start() rejects with that error and the client is in the StartFailed state, the same as a synchronous throw today. As today, this happens before the initialize request, so initializationFailedHandler is not called and the server process is not stopped.
StaticFeature.fillInitializeParams and DynamicFeature.fillInitializeParams may return a promise. BaseLanguageClient.fillInitializeParams now returns void | Promise<void>, and initialize awaits it.
Design
Features are still called in registration order. If a feature returns a promise, the remaining features run after it resolves, so a feature can still rely on what an earlier one wrote into the params. If a feature rejects, the features after it are not called and start() rejects.
When no feature returns a promise, fillInitializeParams fills the params synchronously and returns undefined, exactly as before. This keeps existing subclasses that override fillInitializeParams with a void return type compiling and working: an override that calls super.fillInitializeParams(params) only needs to return its result if it wants to support async features. The Node LanguageClient override now does that and still fills in processId after all features ran, also when one of them is async. Code that calls a feature's fillInitializeParams directly now sees void | Promise<void> as its return type.
The issue also mentions client capabilities. I left fillClientCapabilities synchronous, since every feature implements it and changing it would touch many features. A feature that needs async work for its capabilities can now do it in fillInitializeParams by changing params.capabilities.
Tests
A new Asynchronous initialize params suite in client-node-tests/src/integration.test.ts with a small initializeParamsServer that returns the initialize params it received:
options returned by an async function reach the server, and a sync function and a plain value still work;
the async options function is called again on restart ({ count: 1 }, then { count: 2 });
an async feature and a sync feature registered after it fill the params in order (['async', 'sync']), and the Node client fills in process.pid only after both features ran;
with only sync features, fillInitializeParams returns undefined and the params are filled synchronously;
a rejecting options function and a rejecting feature make start() reject with their error, leave the client in StartFailed, skip later features and cause no unhandled rejection.
Five of the new tests fail when they run against the client source on main (the server receives {} as the initialization options, the async feature's entry is missing, and start() resolves instead of rejecting). The sync function, plain value and synchronous fill tests pass on both.
Checks run on Windows: npm run compile and npm run lint from the root, and npm test in client-node-tests with VS Code insiders: 245 passing on this branch, 238 passing on main. One of four branch runs had a 2s timeout in the existing delayOpenNotifications test "didClose and didOpen keep their order when the language mode of a document changes"; the other three runs passed it. The JSON-RPC, protocol and server suites and the browser tests were not run, since this change only touches the client.
The initializationOptions function can now return a promise, which the
client awaits before it sends the initialize request. Features may
return a promise from fillInitializeParams. They are still called in
registration order, and the params are filled synchronously when no
feature returns a promise, so existing overrides keep working.
Closesmicrosoft#1283
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A critical startup race leaves pending preparation active after connection cleanup, allowing a stale initialization attempt to use a disposed connection and potentially stop the replacement server.
This PR lets language client extensions compute initialization options and parameters asynchronously before sending the initialize request, addressing #1283 while preserving synchronous behavior.
Changes:
Await initialization option factories and feature hooks in registration order.
Preserve synchronous parameter filling and populate the Node process ID after features finish.
Add integration coverage for async results, restarts, ordering, synchronous compatibility, and rejection handling.
File
Description
client/src/node/main.ts
Populate the process ID after asynchronous feature hooks finish.
client/src/common/features.ts
Allow and document promise-returning initialization hooks.
client/src/common/client.ts
Await options and sequence asynchronous parameter preparation.
If the server exited while the initialization options or a feature
filling the initialize params were still pending, the close handling
restarted the client but the first start kept waiting. Once the pending
value resolved, it sent the initialize request on the closed connection
and its failure handling stopped the restarted server.
The start now fails as soon as the connection closes while it waits,
and the remaining features are not called for the stale start.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Queued features can still run after connection closure when ErrorHandler.closed() remains pending; closure must be tracked independently and covered by a deferred-handler regression test.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Skip queued features immediately when connection closes
client/src/common/client.ts:2174
ErrorHandler.closed() can return a pending promise. handleConnectionClosed() disposes the connection but retains _connection until that promise resolves, so this check still passes if the async feature finishes meanwhile: later features run even after whileConnected() has rejected start(). Track closure independently of connection identity and skip queued features immediately on close; add a regression case with a deferred closed() handler.
The check compared the connection with the current one, but the close
handling keeps the connection until the error handler's closed() settles.
If that is a pending promise and the async feature finished meanwhile,
the remaining features still ran for the failed start. Track the close
of the connection itself instead.
Fixed in f1aebec, the case where ErrorHandler.closed() is still pending. Queued features now stop on the connection's own closed promise instead of comparing the connection, so they are skipped even while the close handler waits. A new test with a deferred closed() handler covers it.
The reason will be displayed to describe this comment to others. Learn more.
🟢 Approval recommended
No active review issues are reported for the asynchronous initialization-preparation changes.
No open issues 🎉
Review effort: Balanced
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1283
Extensions can now do async work to compute what the client sends in the
initializerequest. TodayinitializationOptionscan be a function, but its result is sent as is, so a returned promise reaches the server as{}.fillInitializeParamson the client and on features returnsvoid, so a feature can't wait for anything either, althoughinitializeitself is already async.Changes
LanguageClientOptions.initializationOptionsaccepts a function that returns a promise. The client awaits the result before it sendsinitialize. As before, the function is called each time the client initializes the server, so a restart computes the options again. If the function throws or the promise rejects,start()rejects with that error and the client is in theStartFailedstate, the same as a synchronous throw today. As today, this happens before theinitializerequest, soinitializationFailedHandleris not called and the server process is not stopped.StaticFeature.fillInitializeParamsandDynamicFeature.fillInitializeParamsmay return a promise.BaseLanguageClient.fillInitializeParamsnow returnsvoid | Promise<void>, andinitializeawaits it.Design
start()rejects.fillInitializeParamsfills the params synchronously and returnsundefined, exactly as before. This keeps existing subclasses that overridefillInitializeParamswith avoidreturn type compiling and working: an override that callssuper.fillInitializeParams(params)only needs to return its result if it wants to support async features. The NodeLanguageClientoverride now does that and still fills inprocessIdafter all features ran, also when one of them is async. Code that calls a feature'sfillInitializeParamsdirectly now seesvoid | Promise<void>as its return type.fillClientCapabilitiessynchronous, since every feature implements it and changing it would touch many features. A feature that needs async work for its capabilities can now do it infillInitializeParamsby changingparams.capabilities.Tests
A new
Asynchronous initialize paramssuite inclient-node-tests/src/integration.test.tswith a smallinitializeParamsServerthat returns the initialize params it received:{ count: 1 }, then{ count: 2 });['async', 'sync']), and the Node client fills inprocess.pidonly after both features ran;fillInitializeParamsreturnsundefinedand the params are filled synchronously;start()reject with their error, leave the client inStartFailed, skip later features and cause no unhandled rejection.Five of the new tests fail when they run against the client source on
main(the server receives{}as the initialization options, the async feature's entry is missing, andstart()resolves instead of rejecting). The sync function, plain value and synchronous fill tests pass on both.Checks run on Windows:
npm run compileandnpm run lintfrom the root, andnpm testinclient-node-testswith VS Code insiders: 245 passing on this branch, 238 passing onmain. One of four branch runs had a 2s timeout in the existingdelayOpenNotificationstest "didClose and didOpen keep their order when the language mode of a document changes"; the other three runs passed it. The JSON-RPC, protocol and server suites and the browser tests were not run, since this change only touches the client.