Skip to content

Commit

Permalink
Remove exitStatus patch (pyodide#5389)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane authored Jan 30, 2025
1 parent 6c78e4a commit 6a1a74a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 37 deletions.
1 change: 1 addition & 0 deletions Makefile.envs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export MAIN_MODULE_LDFLAGS= $(LDFLAGS_BASE) \
-s NODEJS_CATCH_EXIT=0 \
-s NODEJS_CATCH_REJECTION=0 \
-s EXPORTED_FUNCTIONS='$(EXPORTS)'\
-s EXIT_RUNTIME \
\
-lpython$(PYMAJOR).$(PYMINOR)$(CPYTHON_ABI_FLAGS) \
-lffi \
Expand Down
34 changes: 0 additions & 34 deletions emsdk/patches/0006-Record-the-exitStatus-when-exit-is-called.patch

This file was deleted.

12 changes: 11 additions & 1 deletion src/core/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include <emscripten.h>
#include <emscripten/eventloop.h>

#include <stdbool.h>

#define FAIL_IF_STATUS_EXCEPTION(status) \
Expand Down Expand Up @@ -63,7 +65,9 @@ main(int argc, char** argv)
// no status code to check.
PyImport_AppendInittab("_pyodide_core", PyInit__pyodide_core);
initialize_python(argc, argv);
emscripten_exit_with_live_runtime();
// Normally the runtime would exit when main() returns, don't let that
// happen.
emscripten_runtime_keepalive_push();
return 0;
}

Expand All @@ -74,6 +78,12 @@ EMSCRIPTEN_KEEPALIVE int
run_main()
{
int exitcode;
// run_python may call exit() if `-h` or `-V` have been passed. If we stop it
// from exiting, we'll segfault. So pop the keep alive, so that exit() will
// call onExit and shut down the runtime. We notice this in pyodide.ts and
// throw a ExitStatus error.
emscripten_runtime_keepalive_pop();
pymain_run_python(&exitcode);
emscripten_runtime_keepalive_push();
return exitcode;
}
5 changes: 5 additions & 0 deletions src/js/emscripten-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface EmscriptenSettings {
readonly preRun: readonly PreRunFunc[];
readonly print?: (a: string) => void;
readonly printErr?: (a: string) => void;
readonly onExit?: (code: number) => void;
readonly arguments: readonly string[];
readonly instantiateWasm?: (
imports: { [key: string]: any },
Expand All @@ -29,6 +30,7 @@ export interface EmscriptenSettings {

noInitialRun?: boolean;
INITIAL_MEMORY?: number;
exitCode?: number;
}

/**
Expand All @@ -44,6 +46,9 @@ export function createSettings(config: ConfigType): EmscriptenSettings {
preRun: getFileSystemInitializationFuncs(config),
print: config.stdout,
printErr: config.stderr,
onExit(code) {
settings.exitCode = code;
},
arguments: config.args,
API: { config } as API,
// Emscripten calls locateFile exactly one time with argument
Expand Down
4 changes: 2 additions & 2 deletions src/js/pyodide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ export async function loadPyodide(
// `-s EXPORT_NAME="'_createPyodideModule'"`
const Module = await _createPyodideModule(emscriptenSettings);
// Handle early exit
if (Module.exitCode !== undefined) {
throw new Module.ExitStatus(Module.exitCode);
if (emscriptenSettings.exitCode !== undefined) {
throw new Module.ExitStatus(emscriptenSettings.exitCode);
}
if (options.pyproxyToStringRepr) {
API.setPyProxyToStringMethod(true);
Expand Down

0 comments on commit 6a1a74a

Please sign in to comment.