Skip to content
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

Intercept stopped event when auto-stepping through code in other files/cells #7248

Merged
merged 2 commits into from
Aug 24, 2021
Merged
Changes from 1 commit
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
58 changes: 40 additions & 18 deletions src/client/debugger/jupyter/kernelDebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,19 @@ export class KernelDebugAdapter implements DebugAdapter, IKernelDebugAdapter, ID
}

this.disposables.push(
this.jupyterSession.onIOPubMessage((msg: KernelMessage.IIOPubMessage) => {
this.jupyterSession.onIOPubMessage(async (msg: KernelMessage.IIOPubMessage) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const anyMsg = msg as any;

this.trace('event', JSON.stringify(msg));

if (anyMsg.header.msg_type === 'debug_event') {
if (anyMsg.content.event === 'stopped') {
this.threadId = anyMsg.content.body.threadId;
// We want to get the variables for the variable view every time we stop
// This call starts that
this.stackTrace();
if (await this.handleStoppedEvent()) {
this.trace('intercepted', JSON.stringify(anyMsg.content));
return;
}
}
this.sendMessage.fire(msg.content);
}
Expand Down Expand Up @@ -170,8 +173,30 @@ export class KernelDebugAdapter implements DebugAdapter, IKernelDebugAdapter, ID
this.disposables
)
);
}

private async handleStoppedEvent(): Promise<boolean> {
if (await this.shouldStepIn()) {
this.runByLineContinue();
return true;
}

return false;
}

this.disposables.push(this.onDidSendMessage((msg) => this.trace('to client', JSON.stringify(msg))));
private async shouldStepIn(): Promise<boolean> {
// If we're in run by line and are stopped at another path, continue
if (this.configuration.__mode !== KernelDebugMode.RunByLine) {
return false;
}

// Call stackTrace to determine whether to forward the stop event to the client, and also to
// start the process of updating the variables view.
const stResponse = await this.stackTrace({ startFrame: 0, levels: 1 });

const sf = stResponse.stackFrames[0];
const cell = this.notebookDocument.cellAt(this.configuration.__cellIndex!);
return !!sf.source && sf.source.path !== cell.document.uri.toString();
}

private trace(tag: string, msg: string) {
Expand Down Expand Up @@ -236,8 +261,15 @@ export class KernelDebugAdapter implements DebugAdapter, IKernelDebugAdapter, ID
});
}

private stackTrace(): void {
void this.session.customRequest('stackTrace', { threadId: this.threadId });
private stackTrace(args?: {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please can we rename this to getStacktrace, I saw this earlier and it tripped me, didn't know if it was a Method that got data or a funky method (like a property) that just returned existing existing data or an existing promise

startFrame?: number;
levels?: number;
}): Promise<DebugProtocol.StackTraceResponse['body']> {
return this.session.customRequest('stackTrace', {
threadId: this.threadId,
startFrame: args?.startFrame,
levels: args?.levels
}) as Promise<DebugProtocol.StackTraceResponse['body']>;
}

private scopes(frameId: number): void {
Expand Down Expand Up @@ -346,17 +378,6 @@ export class KernelDebugAdapter implements DebugAdapter, IKernelDebugAdapter, ID
if ((message as DebugProtocol.StackTraceResponse).command === 'stackTrace') {
(message as DebugProtocol.StackTraceResponse).body.stackFrames.forEach((sf) => {
this.scopes(sf.id);

// If we're in run by line and are stopped at another path, continue
if (
this.configuration.__mode === KernelDebugMode.RunByLine &&
this.configuration.__cellIndex !== undefined
) {
const cell = this.notebookDocument.cellAt(this.configuration.__cellIndex);
if (sf.source && sf.source.path !== cell.document.uri.toString()) {
this.runByLineContinue();
}
}
});
}

Expand All @@ -367,6 +388,7 @@ export class KernelDebugAdapter implements DebugAdapter, IKernelDebugAdapter, ID
});
}

this.trace('response', JSON.stringify(message));
this.sendMessage.fire(message);
}

Expand Down