Skip to content

Commit f173d1f

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 f173d1f

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

Tools/wasm/emscripten/node_entry.mjs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ 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);
3840
delete Module.ENV.PATH;
3941
},
42+
onExit(x) {
43+
console.log("onExit", ...arguments);
44+
},
4045
// Ensure that sys.executable, sys._base_executable, etc point to python.sh
4146
// not to this file. To properly handle symlinks, python.sh needs to compute
4247
// its own path.
@@ -45,4 +50,12 @@ const settings = {
4550
arguments: process.argv.slice(thisProgramIndex + 1),
4651
};
4752

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

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)