From b6c98ddaafb718012228ea3065e8d072896347d2 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Thu, 31 Oct 2019 10:46:53 -0700 Subject: [PATCH 1/2] Cache the promise --- .../datascience/jupyter/jupyterCommandFinder.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/client/datascience/jupyter/jupyterCommandFinder.ts b/src/client/datascience/jupyter/jupyterCommandFinder.ts index 0a3b10da7412..c93f0c688713 100644 --- a/src/client/datascience/jupyter/jupyterCommandFinder.ts +++ b/src/client/datascience/jupyter/jupyterCommandFinder.ts @@ -51,7 +51,7 @@ type ProgressNotification = Progress<{ message?: string | undefined; increment?: export class JupyterCommandFinder { private readonly processServicePromise: Promise; private jupyterPath?: string; - private readonly commands = new Map(); + private readonly commands = new Map>(); constructor( private readonly interpreterService: IInterpreterService, private readonly executionFactory: IPythonExecutionFactory, @@ -100,13 +100,10 @@ export class JupyterCommandFinder { // Only log telemetry if not already found (meaning the first time) const timer = new StopWatch(); - try { - const result = await this.findBestCommandImpl(command, cancelToken); - this.commands.set(command, result); - return result; - } finally { - sendTelemetryEvent(Telemetry.FindJupyterCommand, timer.elapsedTime, { command }); - } + const promise = this.findBestCommandImpl(command, cancelToken) + .finally(() => sendTelemetryEvent(Telemetry.FindJupyterCommand, timer.elapsedTime, { command })); + this.commands.set(command, promise); + return promise; } /** From b69f5fc6287e041691b6e0956accfba4d649fd6d Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Thu, 31 Oct 2019 11:49:37 -0700 Subject: [PATCH 2/2] Fix broken test --- .../jupyter/jupyterCommandFinder.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/client/datascience/jupyter/jupyterCommandFinder.ts b/src/client/datascience/jupyter/jupyterCommandFinder.ts index c93f0c688713..5e1f6d77e7c0 100644 --- a/src/client/datascience/jupyter/jupyterCommandFinder.ts +++ b/src/client/datascience/jupyter/jupyterCommandFinder.ts @@ -101,7 +101,26 @@ export class JupyterCommandFinder { // Only log telemetry if not already found (meaning the first time) const timer = new StopWatch(); const promise = this.findBestCommandImpl(command, cancelToken) - .finally(() => sendTelemetryEvent(Telemetry.FindJupyterCommand, timer.elapsedTime, { command })); + .finally(() => sendTelemetryEvent(Telemetry.FindJupyterCommand, timer.elapsedTime, { command })); + + if (cancelToken) { + let promiseCompleted = false; + promise.finally(() => promiseCompleted = true).ignoreErrors(); + + // If the promise is not pending, then remove the item from cache. + // As the promise would not complete correctly, as its been cancelled. + if (cancelToken.isCancellationRequested && !promiseCompleted) { + this.commands.delete(command); + } + cancelToken.onCancellationRequested(() => { + // If the promise is not pending, then remove the item from cache. + // As the promise would not complete correctly, as its been cancelled. + if (!promiseCompleted) { + this.commands.delete(command); + } + }); + } + this.commands.set(command, promise); return promise; }