From dafd4efabb9ad55465e43d7ce081a78dc6c77177 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Fri, 2 Feb 2024 17:07:33 +0000 Subject: [PATCH] wip: try and dispose --- packages/myst-cli/src/session/session.ts | 2 + packages/myst-execute/src/manager.ts | 56 +++++++++++++----------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/packages/myst-cli/src/session/session.ts b/packages/myst-cli/src/session/session.ts index 6e823c0d9..9af75496b 100644 --- a/packages/myst-cli/src/session/session.ts +++ b/packages/myst-cli/src/session/session.ts @@ -196,6 +196,8 @@ export class Session implements ISession { } } + + this._disposeJupyterSessionManager = partialServerSettings.dispose; const serverSettings = ServerConnection.makeSettings(partialServerSettings); const kernelManager = new KernelManager({ serverSettings }); const manager = new SessionManager({ kernelManager, serverSettings }); diff --git a/packages/myst-execute/src/manager.ts b/packages/myst-execute/src/manager.ts index 7f00fbb68..f323b66d0 100644 --- a/packages/myst-execute/src/manager.ts +++ b/packages/myst-execute/src/manager.ts @@ -5,6 +5,7 @@ import * as readline from 'node:readline'; import type { Logger } from 'myst-cli-utils'; import chalk from 'chalk'; + export type JupyterServerSettings = Partial & { dispose?: () => void; }; @@ -56,46 +57,51 @@ export async function findExistingJupyterServer(): Promise { log.info(`🚀 ${chalk.yellowBright('Starting new Jupyter server')}`); const pythonPath = which.sync('python'); const proc = spawn(pythonPath, ['-m', 'jupyter_server', '--ServerApp.root_dir', contentPath]); - const promise = new Promise((resolve, reject) => { - proc.stderr.on('data', (buf) => { - const data = buf.toString(); - // Wait for server to declare itself up - const match = data.match(/([^\s]*?)\?token=([^\s]*)/); - if (match === null) { - return; - } - // Pull out the match information - const [, addr, token] = match; + const settings = await new Promise((resolve, reject) => { + const reader = proc.stderr; + reader.on('data', (buf) => { + + const data = buf.toString(); + // Wait for server to declare itself up + const match = data.match(/([^\s]*?)\?token=([^\s]*)/); + if (match === null) { + return; + } + + // Pull out the match information + const [, addr, token] = match; - // Resolve the promise - resolve({ - baseUrl: addr, - token: token, - dispose: () => proc.kill('SIGINT'), - }); - // Unsubscribe from here-on-in - proc.stdout.removeAllListeners('data'); + // Resolve the promise + resolve({ + baseUrl: addr, + token: token, + }); + // Unsubscribe from here-on-in + reader.removeAllListeners('data'); }); - setTimeout(reject, 20_000); // Fail after 20 seconds of nothing happening + setTimeout(reject, 20_000).unref(); // Fail after 20 seconds of nothing happening }); + // Inform log - promise.then((settings) => { - const url = `${settings.baseUrl}?token=${settings.token}`; - log.info(`🪐 ${chalk.greenBright('Jupyter Server Started')}\n ${chalk.dim(url)}`); - }); - return promise; + const url = `${settings.baseUrl}?token=${settings.token}`; + log.info(`🪐 ${chalk.greenBright('Jupyter Server Started')}\n ${chalk.dim(url)}`); + + settings.dispose = () => proc.kill(); + + return settings; }