Skip to content

Wait for ready on raw session start and respect timeout on kernel change #11768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2 Fixes/11752.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When switching to an invalid kernel (one that is registered but cannot start) in raw mode respect the launch timeout that is passed in.
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -494,5 +494,6 @@
"DataScience.libraryRequiredToLaunchJupyterKernelNotInstalledInterpreter": "{0} requires {1} to be installed.",
"DataScience.runByLine": "Run by line",
"DataScience.continueRunByLine": "Stop",
"DataScience.couldNotInstallLibrary": "Could not install {0}. If pip is not available, please use the package manager of your choice to manually install this library into your Python environment."
"DataScience.couldNotInstallLibrary": "Could not install {0}. If pip is not available, please use the package manager of your choice to manually install this library into your Python environment.",
"DataScience.rawKernelSessionFailed": "Unable to start session for kernel {0}. Select another kernel to launch with."
}
4 changes: 4 additions & 0 deletions src/client/common/utils/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,10 @@ export namespace DataScience {
export const kernelStarted = localize('DataScience.kernelStarted', 'Started kernel {0}.');
export const runByLine = localize('DataScience.runByLine', 'Run by line');
export const continueRunByLine = localize('DataScience.continueRunByLine', 'Stop');
export const rawKernelSessionFailed = localize(
'DataScience.rawKernelSessionFailed',
'Unable to start session for kernel {0}. Select another kernel to launch with.'
);
}

export namespace DebugConfigStrings {
Expand Down
5 changes: 4 additions & 1 deletion src/client/datascience/jupyter/kernels/kernelSwitcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Common, DataScience } from '../../../common/utils/localize';
import { StopWatch } from '../../../common/utils/stopWatch';
import { JupyterSessionStartError } from '../../baseJupyterSession';
import { Commands, Settings } from '../../constants';
import { RawKernelSessionStartError } from '../../raw-kernel/rawJupyterSession';
import {
IJupyterConnection,
IJupyterKernelSpec,
Expand Down Expand Up @@ -125,7 +126,9 @@ export class KernelSwitcher {
} catch (ex) {
if (
isLocalConnection &&
(ex instanceof JupyterSessionStartError || ex instanceof JupyterInvalidKernelError)
(ex instanceof JupyterSessionStartError ||
ex instanceof JupyterInvalidKernelError ||
ex instanceof RawKernelSessionStartError)
) {
// Looks like we were unable to start a session for the local connection.
// Possibly something wrong with the kernel.
Expand Down
22 changes: 19 additions & 3 deletions src/client/datascience/raw-kernel/rawJupyterSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ import { ReportableAction } from '../progress/types';
import { RawSession } from '../raw-kernel/rawSession';
import { IJupyterKernelSpec, ISessionWithSocket } from '../types';

// Error thrown when we are unable to start a raw kernel session
export class RawKernelSessionStartError extends Error {
constructor(kernelTitle: string) {
super(localize.DataScience.rawKernelSessionFailed().format(kernelTitle));
}
}

/*
RawJupyterSession is the implementation of IJupyterSession that instead of
connecting to JupyterLab services it instead connects to a kernel directly
Expand Down Expand Up @@ -91,7 +98,7 @@ export class RawJupyterSession extends BaseJupyterSession {
} else if (newSession === null) {
sendTelemetryEvent(Telemetry.RawKernelSessionStartTimeout);
traceError('Raw session failed to start in given timeout');
throw new Error(localize.DataScience.sessionDisposed());
throw new RawKernelSessionStartError(kernelSpec.display_name || kernelSpec.name);
} else {
sendTelemetryEvent(Telemetry.RawKernelSessionStartSuccess);
traceInfo('Raw session started and connected');
Expand Down Expand Up @@ -121,7 +128,7 @@ export class RawJupyterSession extends BaseJupyterSession {

public async createNewKernelSession(
kernel: IJupyterKernelSpec | LiveKernelModel,
_timeoutMS: number,
timeoutMS: number,
interpreter?: PythonInterpreter
): Promise<ISessionWithSocket> {
if (!kernel || 'session' in kernel) {
Expand All @@ -131,7 +138,13 @@ export class RawJupyterSession extends BaseJupyterSession {

this.outputChannel.appendLine(localize.DataScience.kernelStarted().format(kernel.display_name || kernel.name));

return this.startRawSession(kernel, interpreter);
const newSession = await waitForPromise(this.startRawSession(kernel, interpreter), timeoutMS);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to respect the restart timeout that was getting passed in here.


if (!newSession) {
throw new RawKernelSessionStartError(kernel.display_name || kernel.name);
}

return newSession;
}

protected shutdownSession(
Expand Down Expand Up @@ -209,6 +222,9 @@ export class RawJupyterSession extends BaseJupyterSession {
// Create our raw session, it will own the process lifetime
const result = new RawSession(process);

// When our kernel connects and gets a status message it triggers the ready promise
await result.kernel.ready;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually the more interesting part of the fix. When we were having those initial windows launch fixes I examined this promise, but it didn't actually help with the case where the ports were not open yet. But we do need it here. Ready waits for that initial kernel_status message to be returned. So without this here we might have a bad kernel that can't actually start (regardless of if the process exits or not) but we would leak outside of the start timeout check. So the start or switch timeout check would not trigger, but we'd be stuck with a hung kernel.


// So that we don't have problems with ipywidgets, always register the default ipywidgets comm target.
// Restart sessions and retries might make this hard to do correctly otherwise.
result.kernel.registerCommTarget(Identifiers.DefaultCommTarget, noop);
Expand Down