fix: End LS request when canceled
Description
This MR is another whack at gitlab-org/editor-extensions/gitlab-lsp!170 (merged). The original approach canceled the request from the LS side, but there are caching concerns which need to be considered.
For now, let's just handle the canceling in the extension side.
Screenshots
- Apply this patch to your local
gitlab-vscode-extension
to see some logging of requests and times:
diff --git a/src/common/language_server/language_client_middleware.ts b/src/common/language_server/language_client_middleware.ts
index a10475e7..65fb556d 100644
--- a/src/common/language_server/language_client_middleware.ts
+++ b/src/common/language_server/language_client_middleware.ts
@@ -33,6 +33,25 @@ export class LanguageClientMiddleware implements InlineCompletionMiddleware, Com
return [];
}
+ const debugLineText = document.getText(
+ new vscode.Range(
+ new vscode.Position(position.line, 0),
+ new vscode.Position(position.line, position.character),
+ ),
+ );
+
+ const startTime = performance.now();
+ // eslint-disable-next-line no-console
+ console.log(`[language_client_middleware] "${debugLineText}" start loading`);
+
+ token.onCancellationRequested(() => {
+ // eslint-disable-next-line no-console
+ console.log(
+ `[language_client_middleware] "${debugLineText}" CANCELED (${
+ performance.now() - startTime
+ })!`,
+ );
+ });
this.#stateManager.setLoading(true);
// Short circuit after both cancellation and time have passed
@@ -44,6 +63,12 @@ export class LanguageClientMiddleware implements InlineCompletionMiddleware, Com
try {
return await Promise.race([shortCircuit, next(document, position, context, token)]);
} finally {
+ // eslint-disable-next-line no-console
+ console.log(
+ `[language_client_middleware] "${debugLineText}" STOP LOADING (${
+ performance.now() - startTime
+ })`,
+ );
this.#stateManager.setLoading(false);
}
}
-
Run the Run Extension task in VSCode for
gitlab-vscode-extension
and openDebug Console
in a place where you can see it. -
In the VSCode window that pops up, try to hit the
generations/
endpoint by entering just a prompting comment// A function that starts an http server that returns the weather
-
While that is running, move to another line and try to trigger the
completions/
endpoint by entering code likeconsole.lo
-
In the
Debug Console
, if there's a big gap between thegenerations/
prompt beingCANCELED
andSTOP LOADING
, then we're not canceling our request properly and the LSP is hanging onto a long request for no reason. If there's no gap, then we're handling cancellation correctly. See the MR screenshots for example logs.
Before | After |
---|---|