From 3e34b7615ce39f0780409a75c17e3c5d9326ef2a Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Wed, 17 May 2023 21:36:48 +1000 Subject: [PATCH 1/2] Simplify code in JupyterConnection --- src/kernels/jupyter/connection/jupyterConnection.ts | 2 +- src/kernels/jupyter/jupyterUtils.ts | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/kernels/jupyter/connection/jupyterConnection.ts b/src/kernels/jupyter/connection/jupyterConnection.ts index 2d024a2ff23..d1320f2e089 100644 --- a/src/kernels/jupyter/connection/jupyterConnection.ts +++ b/src/kernels/jupyter/connection/jupyterConnection.ts @@ -77,7 +77,7 @@ export class JupyterConnection implements IExtensionSyncActivationService { private async createConnectionInfoFromUri(uri: string) { // Prepare our map of server URIs await this.updateServerUri(uri); - return createRemoteConnectionInfo(uri, this.getServerUri.bind(this)); + return createRemoteConnectionInfo(uri, this.getServerUri(uri)); } private async validateRemoteConnection(connection: IJupyterConnection): Promise { diff --git a/src/kernels/jupyter/jupyterUtils.ts b/src/kernels/jupyter/jupyterUtils.ts index 1012952c1cc..ee09c8b6f00 100644 --- a/src/kernels/jupyter/jupyterUtils.ts +++ b/src/kernels/jupyter/jupyterUtils.ts @@ -91,7 +91,10 @@ export async function handleExpiredCertsError( export function createRemoteConnectionInfo( uri: string, - getJupyterServerUri: (uri: string) => { server: IJupyterServerUri; serverId: string } | undefined + info?: { + server: IJupyterServerUri; + serverId: string; + } ): IJupyterConnection { let url: URL; try { @@ -101,7 +104,6 @@ export function createRemoteConnectionInfo( throw err; } - const info = getJupyterServerUri(uri); const serverUri = info?.server; const serverId = info?.serverId || ''; From aeab73d8e1ce3e857af8388060546e30125b3b0b Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Wed, 17 May 2023 21:55:39 +1000 Subject: [PATCH 2/2] Fixes --- .../jupyter/connection/jupyterConnection.ts | 15 ++++------ src/kernels/jupyter/jupyterUtils.ts | 9 ++---- src/standalone/api/api.ts | 30 ++++--------------- 3 files changed, 13 insertions(+), 41 deletions(-) diff --git a/src/kernels/jupyter/connection/jupyterConnection.ts b/src/kernels/jupyter/connection/jupyterConnection.ts index d1320f2e089..339cc545e62 100644 --- a/src/kernels/jupyter/connection/jupyterConnection.ts +++ b/src/kernels/jupyter/connection/jupyterConnection.ts @@ -77,7 +77,10 @@ export class JupyterConnection implements IExtensionSyncActivationService { private async createConnectionInfoFromUri(uri: string) { // Prepare our map of server URIs await this.updateServerUri(uri); - return createRemoteConnectionInfo(uri, this.getServerUri(uri)); + + const idAndHandle = extractJupyterServerHandleAndId(uri); + const server = this.uriToJupyterServerUri.get(uri); + return createRemoteConnectionInfo(uri, server, idAndHandle?.id); } private async validateRemoteConnection(connection: IJupyterConnection): Promise { @@ -96,7 +99,7 @@ export class JupyterConnection implements IExtensionSyncActivationService { } } - public async updateServerUri(uri: string): Promise { + private async updateServerUri(uri: string): Promise { const idAndHandle = extractJupyterServerHandleAndId(uri); if (idAndHandle) { try { @@ -124,12 +127,4 @@ export class JupyterConnection implements IExtensionSyncActivationService { } } } - - private getServerUri(uri: string): { server: IJupyterServerUri; serverId: string } | undefined { - const idAndHandle = extractJupyterServerHandleAndId(uri); - if (idAndHandle) { - const server = this.uriToJupyterServerUri.get(uri); - return server ? { server, serverId: idAndHandle.id } : undefined; - } - } } diff --git a/src/kernels/jupyter/jupyterUtils.ts b/src/kernels/jupyter/jupyterUtils.ts index ee09c8b6f00..955f33bede1 100644 --- a/src/kernels/jupyter/jupyterUtils.ts +++ b/src/kernels/jupyter/jupyterUtils.ts @@ -91,10 +91,8 @@ export async function handleExpiredCertsError( export function createRemoteConnectionInfo( uri: string, - info?: { - server: IJupyterServerUri; - serverId: string; - } + serverUri?: IJupyterServerUri, + serverId?: string ): IJupyterConnection { let url: URL; try { @@ -104,8 +102,7 @@ export function createRemoteConnectionInfo( throw err; } - const serverUri = info?.server; - const serverId = info?.serverId || ''; + serverId = serverId || ''; const baseUrl = serverUri ? serverUri.baseUrl diff --git a/src/standalone/api/api.ts b/src/standalone/api/api.ts index c884e5b6eb0..c72ccbd0d40 100644 --- a/src/standalone/api/api.ts +++ b/src/standalone/api/api.ts @@ -2,7 +2,6 @@ // Licensed under the MIT License. import { ExtensionMode, NotebookController, NotebookDocument, Uri, commands, window, workspace } from 'vscode'; -import { JupyterConnection } from '../../kernels/jupyter/connection/jupyterConnection'; import { computeServerId, generateUriFromRemoteProvider } from '../../kernels/jupyter/jupyterUtils'; import { JupyterServerSelector } from '../../kernels/jupyter/connection/serverSelector'; import { @@ -20,6 +19,7 @@ import { traceError } from '../../platform/logging'; import { IControllerRegistration } from '../../notebooks/controllers/types'; import { sendTelemetryEvent } from '../../telemetry'; import { noop } from '../../platform/common/utils/misc'; +import { isRemoteConnection } from '../../kernels/types'; export const IExportedKernelServiceFactory = Symbol('IExportedKernelServiceFactory'); export interface IExportedKernelServiceFactory { @@ -86,13 +86,8 @@ function waitForNotebookControllersCreationForServer( return new Promise((resolve) => { controllerRegistration.onDidChange((e) => { for (let controller of e.added) { - if ( - controller.connection.kind === 'connectToLiveRemoteKernel' || - controller.connection.kind === 'startUsingRemoteKernelSpec' - ) { - if (controller.connection.serverId === serverId) { - resolve(); - } + if (isRemoteConnection(controller.connection) && controller.connection.serverId === serverId) { + resolve(); } } }); @@ -165,7 +160,6 @@ export function buildApi( addRemoteJupyterServer: async (providerId: string, handle: JupyterServerUriHandle) => { sendApiUsageTelemetry(extensions, 'addRemoteJupyterServer'); await new Promise(async (resolve) => { - const connection = serviceContainer.get(JupyterConnection); const selector = serviceContainer.get(JupyterServerSelector); const uri = generateUriFromRemoteProvider(providerId, handle); const serverId = await computeServerId(uri); @@ -176,23 +170,9 @@ export function buildApi( controllerRegistration ); - await connection.updateServerUri(uri); await selector.setJupyterURIToRemote(uri); - - if ( - controllerRegistration.all.find( - (metadata) => - (metadata.kind === 'connectToLiveRemoteKernel' || - metadata.kind === 'startUsingRemoteKernelSpec') && - metadata.serverId === serverId - ) !== undefined - ) { - resolve(); - return; - } else { - await controllerCreatedPromise; - resolve(); - } + await controllerCreatedPromise; + resolve(); }); }, openNotebook: async (uri: Uri, kernelId: string) => {