Runtime: only install the uncaughtException handler for standalone programs#2258
Draft
hhugo wants to merge 1 commit into
Draft
Runtime: only install the uncaughtException handler for standalone programs#2258hhugo wants to merge 1 commit into
hhugo wants to merge 1 commit into
Conversation
e569040 to
9d5acd2
Compare
…ograms The runtime registered a process-wide "uncaughtException" handler at module-load time that called process.exit(2) on any uncaught exception. This emulates native OCaml's fatal-error behaviour, but it ran even when the generated code was loaded as a library: requiring jsoo output in the Node REPL would override the host's error handling and abort the whole process on errors unrelated to the OCaml code. Only install the handler when the program is run directly (require.main === module), not when loaded via require or in the REPL. The same guard is applied to the wasm_of_ocaml runtime. Fixes #1277 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
9d5acd2 to
119284b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1277.
Problem
The runtime registers a process-wide
uncaughtExceptionhandler at module-load time (runtime/js/sys.js):This is meant to emulate native OCaml's fatal-error behaviour (print
Fatal error: exception …, exit with code 2). But it runs unconditionally, even when the generated code is loaded as a library. As reported in #1277, requiring jsoo output in the Node REPL then overrides the host's error handling: an error completely unrelated to the OCaml code (e.g. a typo at the prompt) trips the handler and aborts the whole process viaprocess.exit(2)instead of letting the REPL recover.Fix
Only install the handler when the program is run directly (
require.main === module, e.g.node a.js), not when it is loaded viarequire("./a.js")or in the REPL.module/requireare already in scope inside the runtime (the runtime usesrequire("node:fs")directly), so the check works through the IIFE wrapper. When the discriminator is unavailable (no CommonJSmodule, e.g. ESM/browser), the previous behaviour is preserved.This matches the direction suggested on the issue: keep the fatal-error logic for standalone programs only.
The same guard is applied to the wasm_of_ocaml runtime (
runtime/wasm/runtime.js).Behaviour after the fix
node a.js— handler installed; an async uncaught exception still printsFatal error: exception …and exits 2 (unchanged).require("./a.js")(library / REPL) — handler not installed; the host's own error handling applies, so unrelated errors no longer abort the process and the REPL recovers normally.Tests
Added a regression test in
compiler/tests-compiler/error.mlthat compiles a module, loads it from a driver viarequire, and assertsprocess.listenerCount("uncaughtException") === 0.