-
Notifications
You must be signed in to change notification settings - Fork 295
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
Shared kernelconnection wrapper #10141
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import type { Kernel } from '@jupyterlab/services'; | ||
import { IDisposable } from '../../platform/common/types'; | ||
import { BaseKernelConnectionWrapper } from '../jupyter/baseKernelConnectionWrapper'; | ||
|
||
export class KernelConnectionWrapper extends BaseKernelConnectionWrapper { | ||
/** | ||
* Use `kernelConnection` to access the value as its not a constant (can change over time). | ||
* E.g. when restarting kernels or the like. | ||
*/ | ||
private _kernelConnection!: Kernel.IKernelConnection; | ||
protected get possibleKernelConnection(): undefined | Kernel.IKernelConnection { | ||
return this._kernelConnection; | ||
} | ||
public get kernel() { | ||
return this._kernelConnection; | ||
} | ||
|
||
constructor(kernel: Kernel.IKernelConnection, disposables: IDisposable[]) { | ||
super(kernel, disposables); | ||
this._kernelConnection = kernel; | ||
} | ||
public changeKernel(kernel: Kernel.IKernelConnection) { | ||
if (this.kernel === kernel) { | ||
return; | ||
} | ||
this.stopHandlingKernelMessages(this.possibleKernelConnection!); | ||
this._kernelConnection = kernel; | ||
this.startHandleKernelMessages(kernel); | ||
} | ||
async shutdown(): Promise<void> { | ||
await this._kernelConnection.shutdown(); | ||
} | ||
dispose(): void { | ||
this._kernelConnection.dispose(); | ||
} | ||
async interrupt(): Promise<void> { | ||
await this._kernelConnection.interrupt(); | ||
} | ||
async restart(): Promise<void> { | ||
await this._kernelConnection.restart(); | ||
} | ||
protected override startHandleKernelMessages(kernelConnection: Kernel.IKernelConnection) { | ||
super.startHandleKernelMessages(kernelConnection); | ||
this._kernelConnection = kernelConnection; | ||
kernelConnection.connectionStatusChanged.connect(this.onConnectionStatusChanged, this); | ||
kernelConnection.statusChanged.connect(this.onStatusChanged, this); | ||
kernelConnection.disposed.connect(this.onDisposed, this); | ||
} | ||
protected override stopHandlingKernelMessages(kernelConnection: Kernel.IKernelConnection): void { | ||
super.stopHandlingKernelMessages(kernelConnection); | ||
kernelConnection.connectionStatusChanged.disconnect(this.onConnectionStatusChanged, this); | ||
kernelConnection.statusChanged.disconnect(this.onStatusChanged, this); | ||
kernelConnection.disposed.disconnect(this.onDisposed, this); | ||
} | ||
private onDisposed(connection: Kernel.IKernelConnection) { | ||
if (connection === this.possibleKernelConnection) { | ||
this.disposed.emit(); | ||
} | ||
} | ||
private onStatusChanged(connection: Kernel.IKernelConnection, args: Kernel.Status) { | ||
if (connection === this.possibleKernelConnection) { | ||
this.statusChanged.emit(args); | ||
} | ||
} | ||
private onConnectionStatusChanged(connection: Kernel.IKernelConnection, args: Kernel.ConnectionStatus) { | ||
if (connection === this.possibleKernelConnection) { | ||
this.connectionStatusChanged.emit(args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import { | |
DebugAdapter, | ||
DebugProtocolMessage, | ||
DebugSession, | ||
Disposable, | ||
Event, | ||
EventEmitter, | ||
NotebookCell, | ||
|
@@ -33,7 +34,6 @@ import { | |
IDebugInfoResponse | ||
} from './types'; | ||
import { sendTelemetryEvent } from '../../telemetry'; | ||
import { IDisposable } from '../../platform/common/types'; | ||
import { traceError, traceInfo, traceInfoIfCI, traceVerbose } from '../../platform/logging'; | ||
import { | ||
assertIsDebugConfig, | ||
|
@@ -42,6 +42,7 @@ import { | |
getMessageSourceAndHookIt | ||
} from '../../notebooks/debugger/helper'; | ||
import { ResourceMap } from '../../platform/vscode-path/map'; | ||
import { IDisposable } from '../../platform/common/types'; | ||
|
||
/** | ||
* For info on the custom requests implemented by jupyter see: | ||
|
@@ -89,18 +90,9 @@ export abstract class KernelDebugAdapterBase implements DebugAdapter, IKernelDeb | |
this.debugCell = notebookDocument.cellAt(configuration.__cellIndex!); | ||
} | ||
|
||
this.jupyterSession.kernel?.iopubMessage.connect(this.onIOPubMessage, this); | ||
this.disposables.push( | ||
this.jupyterSession.onIOPubMessage(async (msg: KernelMessage.IIOPubMessage) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const anyMsg = msg as any; | ||
traceInfoIfCI(`Debug IO Pub message: ${JSON.stringify(msg)}`); | ||
if (anyMsg.header.msg_type === 'debug_event') { | ||
this.trace('event', JSON.stringify(msg)); | ||
if (!(await this.delegate?.willSendEvent(anyMsg))) { | ||
this.sendMessage.fire(msg.content); | ||
} | ||
} | ||
}) | ||
new Disposable(() => this.jupyterSession.kernel?.iopubMessage.disconnect(this.onIOPubMessage, this)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we can hook up to the underlying Jupyter API events. |
||
); | ||
|
||
if (this.kernel) { | ||
|
@@ -157,6 +149,17 @@ export abstract class KernelDebugAdapterBase implements DebugAdapter, IKernelDeb | |
traceVerbose(`[Debug] ${tag}: ${msg}`); | ||
} | ||
|
||
async onIOPubMessage(_: unknown, msg: KernelMessage.IIOPubMessage) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const anyMsg = msg as any; | ||
traceInfoIfCI(`Debug IO Pub message: ${JSON.stringify(msg)}`); | ||
if (anyMsg.header.msg_type === 'debug_event') { | ||
this.trace('event', JSON.stringify(msg)); | ||
if (!(await this.delegate?.willSendEvent(anyMsg))) { | ||
this.sendMessage.fire(msg.content); | ||
} | ||
} | ||
} | ||
async handleMessage(message: DebugProtocol.ProtocolMessage) { | ||
try { | ||
traceInfoIfCI(`KernelDebugAdapter::handleMessage ${JSON.stringify(message, undefined, ' ')}`); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a way to hook up to the
iopub
andanymessage
events in the underlyiing Jupyter kernel connection.This property
onIOPubMessage
is a custom event we wire up. If I were to introduceanyMessage
event then i'd have to handle kernel restarts all over again, tomorrow if we wanted to handle other events, then all over gain etc...Also, as we expose the underlying jupyter kernel (Kernel.IKenrelConnection) everywhere it makes sense to just hook to those events.
Hence created a kernel wrapper so that we have a single instance, of the Kernel.KernleConnection on which we can monitor the events (basically moved onIOPubMessage & similar events into the kernel.IKernelConnection and have that keep track of all the event handlers even when kernels restart)