Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
src/node_errors: add re-entrancy guard to prevent infinite recursion …
…in TriggerUncaughtException

Add a thread_local re-entrancy guard to TriggerUncaughtException()
that detects when the JS-level exception handler (process._fatalException)
itself triggers another exception through the inspector protocol.

This prevents the infinite loop:
  TriggerUncaughtException -> InspectorConsoleCall ->
  TriggerUncaughtException -> InspectorConsoleCall -> ...

When re-entrancy is detected, the function prints a diagnostic with the
formatted exception to stderr using FormatCaughtException (which avoids
the inspector path entirely), then aborts the process.

Fixes: #64326
Signed-off-by: Temuulen Undrakhbayar <zerone.offical@gmail.com>
  • Loading branch information
themuuln committed Jul 15, 2026
commit f2c1ab5679ee5e2d0182f5f3dc96911ea9ee0c72
21 changes: 21 additions & 0 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ static std::string GetErrorSource(Isolate* isolate,

static std::atomic<bool> is_in_oom{false};
static thread_local std::atomic<bool> is_retrieving_js_stacktrace{false};
static thread_local bool is_in_uncaught_exception = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be std::atomic<bool> like is_retrieving_js_stacktrace? If not, a short comment explaining why would be good.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough — added a comment explaining why. Since this flag only guards re-entrancy on the current thread, thread_local handles the isolation and no atomic is needed.

MaybeLocal<StackTrace> GetCurrentStackTrace(Isolate* isolate, int frame_count) {
if (isolate == nullptr) {
return MaybeLocal<StackTrace>();
Expand Down Expand Up @@ -1278,6 +1279,26 @@ void TriggerUncaughtException(Isolate* isolate,
CHECK(isolate->InContext());
Local<Context> context = isolate->GetCurrentContext();
Environment* env = Environment::GetCurrent(context);
// Re-entrancy guard: prevent infinite recursion when the JS-level
// exception handler (process._fatalException) itself triggers another
// exception through the inspector protocol, causing V8's pending message
// reporting to call TriggerUncaughtException reentrantly.
if (is_in_uncaught_exception) {
PrintToStderrAndFlush(
"FATAL ERROR: Re-entrant uncaught exception detected.\n"
"The exception handler threw an error while processing a\n"
"previous uncaught exception, likely due to an inspector\n"
"protocol issue. Aborting to prevent infinite recursion.\n" +
FormatCaughtException(isolate, context, error, message));
ABORT();
}
// RAII guard to ensure the flag is cleared when TriggerUncaughtException
// returns normally. Note: ABORT() and env->Exit() do not run this
// destructor, which is intentional — the process is terminating.
struct UncaughtExceptionGuard {
UncaughtExceptionGuard() { is_in_uncaught_exception = true; }
~UncaughtExceptionGuard() { is_in_uncaught_exception = false; }
} uncaught_exception_guard;
if (env == nullptr) {
// This means that the exception happens before Environment is assigned
// to the context e.g. when there is a SyntaxError in a per-context
Expand Down