Skip to content

Commit

Permalink
prevent debugpy from getting stuck (#7612)
Browse files Browse the repository at this point in the history
* -prevent debugpy from getting stuck
by running code
-throttle all debugging functions
-if we can't continue in RBL, disconnect
-have run and debug button respect
'notebook.consolidatedRunButton' setting

* undo debug cell button changes

* -remove throttle
-check if we're already debugging
-change code that we execute to get unfrozen

* simplify into a function

* return if notebook is in process of starting
a debugging session

* oops

* Check if its already debugging

* imrpove readability

* fix context keys in case session is going,
and context keys shows its not

* change context keys after awaiting the debugger to
start

* more context key management

* remove isDebugging conditions

* fix failing test

* wait more

* try to fix tests

* tests
  • Loading branch information
David Kutugata authored Sep 24, 2021
1 parent 45564e0 commit c6d0357
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 40 deletions.
9 changes: 5 additions & 4 deletions src/client/debugger/jupyter/debugControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ export class RunByLineController implements IDebuggingDelegate {
public continue(): void {
if (typeof this.lastPausedThreadId !== 'number') {
traceVerbose(`No paused thread, can't do RBL`);
this.stop();
return;
}

void this.debugAdapter.stepIn(this.lastPausedThreadId);
}

public stop(): void {
// When debugpy gets stuck, running a cell fixes it and allows us to start another debugging session
void this.kernel.executeHidden('pass');
this.debugAdapter.disconnect();
}

Expand Down Expand Up @@ -173,10 +176,8 @@ async function cellDebugSetup(
): Promise<void> {
// remove this if when https://github.com/microsoft/debugpy/issues/706 is fixed and ipykernel ships it
// executing this code restarts debugpy and fixes https://github.com/microsoft/vscode-jupyter/issues/7251
if (kernel) {
const code = 'import debugpy\ndebugpy.debug_this_thread()';
await kernel.executeHidden(code);
}
const code = 'import debugpy\ndebugpy.debug_this_thread()';
await kernel.executeHidden(code);

await debugAdapter.dumpCell(debugCell.index);
}
105 changes: 69 additions & 36 deletions src/client/debugger/jupyter/debuggingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
ProgressLocation,
DebugAdapterDescriptor,
Event,
EventEmitter
EventEmitter,
NotebookEditor
} from 'vscode';
import * as path from 'path';
import { IKernel, IKernelProvider } from '../../datascience/jupyter/kernels/types';
Expand Down Expand Up @@ -51,6 +52,7 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
private notebookToDebugger = new Map<NotebookDocument, Debugger>();
private notebookToDebugAdapter = new Map<NotebookDocument, KernelDebugAdapter>();
private notebookToRunByLineController = new Map<NotebookDocument, RunByLineController>();
private notebookInProgress = new Set<NotebookDocument>();
private cache = new Map<PythonEnvironment, boolean>();
private readonly disposables: IDisposable[] = [];
private _doneDebugging = new EventEmitter<void>();
Expand Down Expand Up @@ -81,9 +83,9 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
workspace.onDidCloseNotebookDocument(async (document) => {
const dbg = this.notebookToDebugger.get(document);
if (dbg) {
await dbg.stop();
this.updateToolbar(false);
this.updateCellToolbar(false);
await dbg.stop();
}
}),

Expand All @@ -94,16 +96,7 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb

this.commandManager.registerCommand(DSCommands.DebugNotebook, async () => {
const editor = this.vscNotebook.activeNotebookEditor;
if (editor) {
if (await this.checkForIpykernel6(editor.document)) {
this.updateToolbar(true);
void this.startDebugging(editor.document);
} else {
void this.installIpykernel6();
}
} else {
void this.appShell.showErrorMessage(DataScience.noNotebookToDebug());
}
await this.tryToStartDebugging(KernelDebugMode.Everything, editor);
}),

this.commandManager.registerCommand(DSCommands.RunByLine, async (cell: NotebookCell | undefined) => {
Expand All @@ -120,17 +113,7 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
return;
}

if (editor) {
if (await this.checkForIpykernel6(editor.document, DataScience.startingRunByLine())) {
this.updateToolbar(true);
this.updateCellToolbar(true);
await this.startDebuggingCell(editor.document, KernelDebugMode.RunByLine, cell);
} else {
void this.installIpykernel6();
}
} else {
void this.appShell.showErrorMessage(DataScience.noNotebookToDebug());
}
await this.tryToStartDebugging(KernelDebugMode.RunByLine, editor, cell);
}),

this.commandManager.registerCommand(DSCommands.RunByLineNext, (cell: NotebookCell | undefined) => {
Expand All @@ -146,6 +129,10 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
return;
}

if (this.notebookInProgress.has(cell.notebook)) {
return;
}

const controller = this.notebookToRunByLineController.get(cell.notebook);
if (controller && controller.debugCell.document.uri.toString() === cell.document.uri.toString()) {
controller.continue();
Expand All @@ -157,7 +144,9 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
if (editor) {
const controller = this.notebookToRunByLineController.get(editor.document);
if (controller) {
sendTelemetryEvent(DebuggingTelemetry.endedSession, undefined, { reason: 'withKeybinding' });
sendTelemetryEvent(DebuggingTelemetry.endedSession, undefined, {
reason: 'withKeybinding'
});
controller.stop();
}
}
Expand All @@ -177,16 +166,7 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
return;
}

if (editor) {
if (await this.checkForIpykernel6(editor.document)) {
this.updateToolbar(true);
void this.startDebuggingCell(editor.document, KernelDebugMode.Cell, cell);
} else {
void this.installIpykernel6();
}
} else {
void this.appShell.showErrorMessage(DataScience.noNotebookToDebug());
}
await this.tryToStartDebugging(KernelDebugMode.Cell, editor, cell);
})
);
}
Expand Down Expand Up @@ -232,6 +212,59 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
this.runByLineInProgress.set(runningByLine).ignoreErrors();
}

private async tryToStartDebugging(mode: KernelDebugMode, editor?: NotebookEditor, cell?: NotebookCell) {
if (!editor) {
void this.appShell.showErrorMessage(DataScience.noNotebookToDebug());
return;
}

if (this.notebookInProgress.has(editor.document)) {
return;
}

if (this.isDebugging(editor.document)) {
this.updateToolbar(true);
if (mode === KernelDebugMode.RunByLine) {
this.updateCellToolbar(true);
}
return;
}

try {
this.notebookInProgress.add(editor.document);
if (
await this.checkForIpykernel6(
editor.document,
mode === KernelDebugMode.RunByLine ? DataScience.startingRunByLine() : undefined
)
) {
switch (mode) {
case KernelDebugMode.Everything:
await this.startDebugging(editor.document);
this.updateToolbar(true);
break;
case KernelDebugMode.Cell:
if (cell) {
await this.startDebuggingCell(editor.document, KernelDebugMode.Cell, cell);
this.updateToolbar(true);
}
break;
case KernelDebugMode.RunByLine:
if (cell) {
await this.startDebuggingCell(editor.document, KernelDebugMode.RunByLine, cell);
this.updateToolbar(true);
this.updateCellToolbar(true);
}
break;
}
} else {
void this.installIpykernel6();
}
} finally {
this.notebookInProgress.delete(editor.document);
}
}

private async startDebuggingCell(
doc: NotebookDocument,
mode: KernelDebugMode.Cell | KernelDebugMode.RunByLine,
Expand Down Expand Up @@ -286,13 +319,13 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
}

private async endSession(session: DebugSession) {
void this.updateToolbar(false);
void this.updateCellToolbar(false);
this._doneDebugging.fire();
for (const [doc, dbg] of this.notebookToDebugger.entries()) {
if (dbg && session.id === (await dbg.session).id) {
this.notebookToDebugger.delete(doc);
this.notebookToDebugAdapter.delete(doc);
this.updateToolbar(false);
this.updateCellToolbar(false);
break;
}
}
Expand Down

0 comments on commit c6d0357

Please sign in to comment.