mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
src: add re-entrancy guard to TriggerUncaughtException
Add a thread_local re-entrancy guard to TriggerUncaughtException() that detects when the JS-level exception handler (process._fatalException) 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: https://github.com/nodejs/node/issues/64326 Signed-off-by: Temuulen Undrakhbayar <zerone.offical@gmail.com> PR-URL: https://github.com/nodejs/node/pull/64327 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
1 parent
dfe0617e71
commit
2fd50ae915
1 file changed
+23
@@ -191,6 +191,9 @@ 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};
|
||||
// This is thread-local because it only guards re-entrancy within the current
|
||||
// thread's uncaught-exception path; no cross-thread synchronization is needed.
|
||||
static thread_local bool is_in_uncaught_exception = false;
|
||||
MaybeLocal<StackTrace> GetCurrentStackTrace(Isolate* isolate, int frame_count) {
|
||||
if (isolate == nullptr) {
|
||||
return MaybeLocal<StackTrace>();
|
||||
@@ -1302,6 +1305,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
|
||||
|
||||
Reference in new issue
Block a user