Skip to content

fix: End LS request when canceled

Paul Slaughter requested to merge ps-handle-abort into main

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

  1. 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);
     }
   }

  1. Run the Run Extension task in VSCode for gitlab-vscode-extension and open Debug Console in a place where you can see it.

  2. 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
  3. While that is running, move to another line and try to trigger the completions/ endpoint by entering code like console.lo

  4. In the Debug Console, if there's a big gap between the generations/ prompt being CANCELED and STOP 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
Screenshot_2023-12-13_at_12.22.26_AM Screenshot_2023-12-13_at_12.20.53_AM
Edited by Paul Slaughter

Merge request reports

Loading