Skip to content

Commit 19a197c

Browse files
committed
gh-127503: Improve tracebacks on Emscripten when there is a trap
This PR solves two problems: 1. No Python traceback shown on segfault/trap 2. The JavaScript source line is shown The JavaScript source line is super long and completely unenlightening, whereas the Python traceback is very helpful.
1 parent 98fa4a4 commit 19a197c

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Tools/wasm/emscripten/node_entry.mjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const thisProgramIndex = process.argv.findIndex((x) =>
3232

3333
const settings = {
3434
preRun(Module) {
35+
// Globally expose API object so we can access it if we raise on startup.
36+
globalThis.Module = Module;
3537
mountDirectories(Module);
3638
Module.FS.chdir(process.cwd());
3739
Object.assign(Module.ENV, process.env);
@@ -45,4 +47,12 @@ const settings = {
4547
arguments: process.argv.slice(thisProgramIndex + 1),
4648
};
4749

48-
await EmscriptenModule(settings);
50+
try {
51+
await EmscriptenModule(settings);
52+
} catch(e) {
53+
// Show JavaScript exception and traceback
54+
console.warn(e);
55+
// Show Python exception and traceback
56+
Module.__Py_DumpTraceback(2, Module._PyGILState_GetThisThreadState());
57+
process.exit(1);
58+
}

configure

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2370,7 +2370,7 @@ AS_CASE([$ac_sys_system],
23702370
dnl Include file system support
23712371
AS_VAR_APPEND([LINKFORSHARED], [" -sFORCE_FILESYSTEM -lidbfs.js -lnodefs.js -lproxyfs.js -lworkerfs.js"])
23722372
AS_VAR_APPEND([LINKFORSHARED], [" -sEXPORTED_RUNTIME_METHODS=FS,callMain,ENV"])
2373-
AS_VAR_APPEND([LINKFORSHARED], [" -sEXPORTED_FUNCTIONS=_main,_Py_Version,__PyRuntime,__PyEM_EMSCRIPTEN_COUNT_ARGS_OFFSET"])
2373+
AS_VAR_APPEND([LINKFORSHARED], [" -sEXPORTED_FUNCTIONS=_main,_Py_Version,__PyRuntime,__PyEM_EMSCRIPTEN_COUNT_ARGS_OFFSET,_PyGILState_GetThisThreadState,__Py_DumpTraceback"])
23742374
AS_VAR_APPEND([LINKFORSHARED], [" -sSTACK_SIZE=5MB"])
23752375
23762376
AS_VAR_IF([enable_wasm_dynamic_linking], [yes], [

0 commit comments

Comments
 (0)