From 2ac9a8ce9797f62f8278ed5175fed5764c45bc93 Mon Sep 17 00:00:00 2001 From: Aaron Munger Date: Thu, 4 Apr 2024 08:48:19 -0700 Subject: [PATCH] log errors that occur in bg threads --- src/standalone/api/kernels/backgroundExecution.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/standalone/api/kernels/backgroundExecution.ts b/src/standalone/api/kernels/backgroundExecution.ts index 97b9a6cae18..bf0b12757c2 100644 --- a/src/standalone/api/kernels/backgroundExecution.ts +++ b/src/standalone/api/kernels/backgroundExecution.ts @@ -22,12 +22,14 @@ export async function execCodeInBackgroundThread( const api = createKernelApiForExtension(JVSC_EXTENSION_ID, kernel, { accessAllowed: true }); const mime = `application/vnd.vscode.bg.execution.${counter}`; const mimeFinalResult = `application/vnd.vscode.bg.execution.${counter}.result`; + const mimeErrorResult = `application/vnd.vscode.bg.execution.${counter}.error`; let displayId = ''; const codeToSend = ` def __jupyter_exec_background__(): from IPython.display import display from threading import Thread + from traceback import format_exc # First send a dummy response to get the display id. # Later we'll send the real response with the actual data. @@ -41,8 +43,8 @@ def __jupyter_exec_background__(): def bg_main(): try: output.update({"${mimeFinalResult}": do_implementation()}, raw=True) - except: - pass + except Exception as e: + output.update({"${mimeErrorResult}": format_exc()}, raw=True) Thread(target=bg_main, daemon=True).start() @@ -85,6 +87,11 @@ del __jupyter_exec_background__ if (token.isCancellationRequested) { return; } + const error = output.items.find((item) => item.mime === mimeErrorResult); + if (error) { + traceWarning('Error in background execution:\n', new TextDecoder().decode(error.data)); + return; + } const metadata = getNotebookCellOutputMetadata(output); if (!metadata?.transient?.display_id) { continue;