Skip to content

Commit d6c42f6

Browse files
committed
FIX Prevent segfaults in CLI runner
After many hours of debugging, I minimized the problem down to this: emscripten-core/emscripten#22052 Thanks to @henryiii for reporting. See thread here for my comments while I was debugging: scikit-hep/boost-histogram#938
1 parent 7cf233e commit d6c42f6

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

src/core/error_handling.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ EM_JS(JsVal, restore_stderr, (void), {
119119
/**
120120
* Wrap the exception in a JavaScript PythonError object.
121121
*
122-
* The return value of this function is always a valid hiwire ID to an error
123-
* object. It never returns NULL.
122+
* The return value of this function is always a JavaScript error object. It
123+
* never returns null.
124124
*
125125
* We are cautious about leaking the Python stack frame, so we don't increment
126126
* the reference count on the exception object, we just store a pointer to it.
127-
* Later we can check if this pointer is equal to sys.last_exc and if so
128-
* restore the exception (see restore_sys_last_exception).
127+
* Later we can check if this pointer is equal to sys.last_exc and if so restore
128+
* the exception (see restore_sys_last_exception).
129129
*
130130
* WARNING: dereferencing the error pointer stored on the PythonError is a
131131
* use-after-free bug.

src/js/load-package.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,9 @@ export async function loadPackage(
555555
Array.from(toLoad.values()).map(({ installPromise }) => installPromise),
556556
);
557557

558+
// Warning: this sounds like it might not do anything important, but it
559+
// fills in the GOT. There can be segfaults if we leave it out.
560+
// See https://github.com/emscripten-core/emscripten/issues/22052
558561
Module.reportUndefinedSymbols();
559562
if (loadedPackageData.size > 0) {
560563
const successNames = Array.from(loadedPackageData, (pkg) => pkg.name)

src/templates/python

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ async function main() {
142142
console.error(e);
143143
}
144144
}
145+
// Warning: this sounds like it might not do anything important, but it
146+
// fills in the GOT. There can be segfaults if we leave it out.
147+
// See https://github.com/emscripten-core/emscripten/issues/22052
148+
py._module.reportUndefinedSymbols();
145149
146150
py.runPython(
147151
`
@@ -176,14 +180,10 @@ async function main() {
176180
try {
177181
errcode = py._module._run_main();
178182
} catch (e) {
179-
// If someone called exit, just exit with the right return code.
180183
if(e.constructor.name === "ExitStatus"){
181184
process.exit(e.status);
182185
}
183-
// Otherwise if there is some sort of error, include the Python
184-
// tracebook in addition to the JavaScript traceback
185-
py._module._dump_traceback();
186-
throw e;
186+
py._api.fatal_error(e);
187187
}
188188
if (errcode) {
189189
process.exit(errcode);

0 commit comments

Comments
 (0)