Skip to content

[Bug]: Uncaught illegal state exception and renderer crash with node immediates and closing child window #36858

Description

@markh-discord

Preflight Checklist

Electron Version

13.x, 17.x, 22.0.0

What operating system are you using?

Windows

Operating System Version

Windows 11 (22621.963)

What arch are you using?

x64

Last Known Working Electron version

Unknown

Expected Behavior

Closing a child window should not produce an 'Uncaught illegal state' exception. Furthermore, it shouldn't cause the renderer process to exit with code 7 (which can specifically occur if there is a node native immediate being used, see additional information below).

Actual Behavior

A race condition can occur when using setImmediate (or using a node module that sets up native immediates) in a node environment and closing a child window that has a separate node environment (but shares the underlying isolate and event loop).

Note: This seems to occur when the BrowserWindow webPreferences sandbox is set to false.

The behavior here is that the console will report Uncaught illegal state and in certain cases the renderer process will simply exit with code 7. The following was also reported to stderr:

illegal access
(Use `electron --trace-uncaught ...` to show where the exception was thrown)

Testcase Gist URL

https://gist.github.com/markh-discord/1bb303c57d1a20aeee95aeedbc403ab3

Additional Information

Note: This seems to occur only when the main BrowserWindow webPreferences sandbox is set to false.

The reproduce project in the gist may require a few attempts, but when clicking the button to open the window it will automatically close. Around 1-10 attempts may be needed to display the error in the console. To improve the simplicity of this I used Javascript setImmediate which seems to be more recoverable as it doesn't exit the process. In the steps below there is a node native immediate being setup by the node https module that does cause the renderer process to eventually exit.

Reproduce steps aren't precise since this is a race condition, however the following path seems to hit this:

  1. node Environment A is created and Environment::InitializeLibuv is run and sets up its Environment::CheckImmediate method in uv_check_start.
    a. This will be interesting later, but it's called via the event loop uv_run: https://github.com/libuv/libuv/blob/e972c6705f197e12d211b598fc514bbea051425d/src/win/core.c#L645
  2. Window is opened, a new node Environment (I'll call Environment B) is created:
    node::Environment* env =
    node_bindings_->CreateEnvironment(renderer_context, nullptr);

    a. This environment uses the existing NodeBindings::isolate_data_, which also shares the isolate and uv_loop_ from Environment A:
    if (!isolate_data_)
  3. node Environment A has a native immediate pushed from http post work being done by the node http module.
    a. This can occur in a few places via env()->SetImmediate(...), here's one as an example: https://github.com/nodejs/node/blob/e8db11c5b2169931238db6c02f6d7f80b3c6e759/src/crypto/crypto_tls.cc#L593
  4. Window is closed, causing Environment B to begin cleanup:
    a. node::FreeEnvironment is called, and sets up JS to be disallowed: https://github.com/nodejs/node/blob/2af83ea0f8a1594c0d0bf4bc4b3035234bcd895e/src/api/environment.cc#L420-L421
    b. node::CleanupHandles is eventually called, this starts up the event loop via uv_run: https://github.com/nodejs/node/blob/2af83ea0f8a1594c0d0bf4bc4b3035234bcd895e/src/env.cc#LL966C21-L966C21
    c. Per 1a above, the uv_check_invoke is called, and the Environment A's CheckImmediate is run
  5. At this point we're now running things from Environment A, in particular it will begin executing the native immediates that it has. Note that we're still in the same call stack and the isolate's state to disallow JS is still flagged.
  6. Since a native immediate was added in step 2 above, this begins running, eventually for the http stream work it tries to callback into Javascript via node::ReportWritesToJSStreamListener::OnStreamAfterReqFinished:
    a. https://github.com/nodejs/node/blob/2af83ea0f8a1594c0d0bf4bc4b3035234bcd895e/src/stream_base.cc#L632
  7. This then fails the check on if JS is allowed to be executed (setup on shared isolate from Environment B in step 3a):
    a. https://chromium.googlesource.com/v8/v8/+/roll/src/execution.cc
    if (!ThrowOnJavascriptExecution::IsAllowed(isolate)) {
      isolate->ThrowIllegalOperation();
    
  8. Call stack begins unwinding and eventually hits: errors::TriggerUncaughtException: https://github.com/nodejs/node/blob/2af83ea0f8a1594c0d0bf4bc4b3035234bcd895e/src/env.cc#L1097
  9. This in turn attempts to execute a fatal_exception_function.As<Function>()->Call: https://github.com/nodejs/node/blob/2af83ea0f8a1594c0d0bf4bc4b3035234bcd895e/src/node_errors.cc#L1126-L1127
    a. Since JS is still disallowed we get another v8::internal::Isolate::ThrowIllegalOperation triggered
  10. This unwinds and executes the TryCatchScope dtor setup in the above scope: https://github.com/nodejs/node/blob/2af83ea0f8a1594c0d0bf4bc4b3035234bcd895e/src/node_errors.cc#L1116-L1117
  11. The TryCatchScope::~TryCatchScope then builds the message to dumps and eventually causes the env_->Exit to be called: https://github.com/nodejs/node/blob/2af83ea0f8a1594c0d0bf4bc4b3035234bcd895e/src/node_errors.cc#L572
  12. This then runs the Environment::Exit and eventually will exit the process: https://github.com/nodejs/node/blob/2af83ea0f8a1594c0d0bf4bc4b3035234bcd895e/src/env.cc#L1548

Activity

  1. self-assigned this
    on Jan 12, 2023
  2. self-assigned this
    on Jan 26, 2023
  3. tsmaeder commented on Feb 20, 2023

    @tsmaeder

    This looks a lot like something we're hitting in eclipse-theia/theia#11642: when trying to close the second browser windows, we get the string "IllegalAccess" written to dev-tools javascript console with no stack trace. The call stack that writes out the message is:

    Details
    electron.exe!v8::internal::`anonymous namespace'::Invoke(v8::internal::Isolate * isolate, const v8::internal::`anonymous namespace'::InvokeParams & params) Line 320 (c:\Users\thomas\code\testdata\electron\src\v8\src\execution\execution.cc:320)
    electron.exe!v8::internal::Execution::Call(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::Object> callable, v8::internal::Handle<v8::internal::Object> receiver, int argc, v8::internal::Handle<v8::internal::Object> * argv) Line 474 (c:\Users\thomas\code\testdata\electron\src\v8\src\execution\execution.cc:474)
    electron.exe!v8::Function::Call(v8::Local<v8::Context> context, v8::Local<v8::Value> recv, int argc, v8::Local<v8::Value> * argv) Line 5090 (c:\Users\thomas\code\testdata\electron\src\v8\src\api\api.cc:5090)
    electron.exe!node::InternalMakeCallback(node::Environment * env, v8::Local<v8::Object> resource, v8::Local<v8::Object> recv, const v8::Local<v8::Function> callback, int argc, v8::Local<v8::Value> * argv, node::async_context asyncContext) Line 199 (c:\Users\thomas\code\testdata\electron\src\third_party\electron_node\src\api\callback.cc:199)
    electron.exe!node::MakeCallback(v8::Isolate * isolate, v8::Local<v8::Object> recv, v8::Local<v8::Function> callback, int argc, v8::Local<v8::Value> * argv, node::async_context asyncContext) Line 271 (c:\Users\thomas\code\testdata\electron\src\third_party\electron_node\src\api\callback.cc:271)
    electron.exe!node::Environment::CheckImmediate(uv_check_s * handle) Line 928 (c:\Users\thomas\code\testdata\electron\src\third_party\electron_node\src\env.cc:928)
    electron.exe!uv_check_invoke(uv_loop_s * loop) Line 121 (c:\Users\thomas\code\testdata\electron\src\third_party\electron_node\deps\uv\src\win\loop-watcher.c:121)
    electron.exe!uv_run(uv_loop_s * loop, <unnamed-tag> mode) Line 630 (c:\Users\thomas\code\testdata\electron\src\third_party\electron_node\deps\uv\src\win\core.c:630)
    electron.exe!node::Environment::CleanupHandles() Line 647 (c:\Users\thomas\code\testdata\electron\src\third_party\electron_node\src\env.cc:647)
    electron.exe!node::Environment::RunCleanup() Line 684 (c:\Users\thomas\code\testdata\electron\src\third_party\electron_node\src\env.cc:684)
    electron.exe!node::FreeEnvironment(node::Environment * env) Line 375 (c:\Users\thomas\code\testdata\electron\src\third_party\electron_node\src\api\environment.cc:375)
    electron.exe!electron::ElectronRendererClient::WillReleaseScriptContext(v8::Local<v8::Context> context, content::RenderFrame * render_frame) Line 161 (c:\Users\thomas\code\testdata\electron\src\electron\shell\renderer\electron_renderer_client.cc:161)
    electron.exe!electron::ElectronRenderFrameObserver::WillReleaseScriptContext(v8::Local<v8::Context> context, int world_id) Line 163 (c:\Users\thomas\code\testdata\electron\src\electron\shell\renderer\electron_render_frame_observer.cc:163)
    electron.exe!content::RenderFrameImpl::WillReleaseScriptContext(v8::Local<v8::Context> context, int world_id) Line 4460 (c:\Users\thomas\code\testdata\electron\src\content\renderer\render_frame_impl.cc:4460)
    electron.exe!blink::LocalFrameClientImpl::WillReleaseScriptContext(class v8::Local<class v8::Context>,int) (Unknown Source:0)
    electron.exe!blink::LocalWindowProxy::DisposeContext(enum blink::WindowProxy::Lifecycle,enum blink::WindowProxy::FrameReuseStatus) (Unknown Source:0)
    electron.exe!blink::WindowProxy::ClearForClose(void) (Unknown Source:0)
    electron.exe!blink::WindowProxyManager::ClearForClose(void) (Unknown Source:0)
    electron.exe!blink::Frame::Detach(enum blink::FrameDetachType) (Unknown Source:0)
    electron.exe!blink::Page::WillBeDestroyed(void) (Unknown Source:0)
    electron.exe!blink::WebViewImpl::Close(void) (Unknown Source:0)
    electron.exe!content::RenderViewImpl::Destroy() Line 236 (c:\Users\thomas\code\testdata\electron\src\content\renderer\render_view_impl.cc:236)
    electron.exe!base::internal::FunctorTraits<void (content::RenderViewImpl::*)(),void>::Invoke<void (content::RenderViewImpl::*)(),content::RenderViewImpl *>(void(content::RenderViewImpl::*)() method, content::RenderViewImpl * && receiver_ptr) Line 509 (c:\Users\thomas\code\testdata\electron\src\base\bind_internal.h:509)
    electron.exe!base::internal::InvokeHelper<0,void>::MakeItSo<void (content::RenderViewImpl::*)(),content::RenderViewImpl *>(void(content::RenderViewImpl::*)() && functor, content::RenderViewImpl * && args) Line 648 (c:\Users\thomas\code\testdata\electron\src\base\bind_internal.h:648)
    electron.exe!base::internal::Invoker<base::internal::BindState<void (content::RenderViewImpl::*)(),base::internal::UnretainedWrapper<content::RenderViewImpl>>,void ()>::RunImpl<void (content::RenderViewImpl::*)(),std::__1::tuple<base::internal::UnretainedWrapper<content::RenderViewImpl>>,0>(void(content::RenderViewImpl::*)() && functor, std::__1::tuple<base::internal::UnretainedWrapper<content::RenderViewImpl>> && bound, std::__1::integer_sequence<unsigned long long,0>) Line 721 (c:\Users\thomas\code\testdata\electron\src\base\bind_internal.h:721)
    electron.exe!base::internal::Invoker<base::internal::BindState<void (content::RenderViewImpl::*)(),base::internal::UnretainedWrapper<content::RenderViewImpl>>,void ()>::RunOnce(base::internal::BindStateBase * base) Line 690 (c:\Users\thomas\code\testdata\electron\src\base\bind_internal.h:690)
    electron.exe!base::OnceCallback<void ()>::Run() Line 99 (c:\Users\thomas\code\testdata\electron\src\base\callback.h:99)
    electron.exe!base::TaskAnnotator::RunTask(const char * trace_event_name, base::PendingTask * pending_task) Line 180 (c:\Users\thomas\code\testdata\electron\src\base\task\common\task_annotator.cc:180)
    electron.exe!base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWorkImpl(base::sequence_manager::LazyNow * continuation_lazy_now) Line 361 (c:\Users\thomas\code\testdata\electron\src\base\task\sequence_manager\thread_controller_with_message_pump_impl.cc:361)
    electron.exe!base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWork() Line 260 (c:\Users\thomas\code\testdata\electron\src\base\task\sequence_manager\thread_controller_with_message_pump_impl.cc:260)
    electron.exe!base::MessagePumpDefault::Run(base::MessagePump::Delegate * delegate) Line 40 (c:\Users\thomas\code\testdata\electron\src\base\message_loop\message_pump_default.cc:40)
    electron.exe!base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::Run(bool application_tasks_allowed, base::TimeDelta timeout) Line 470 (c:\Users\thomas\code\testdata\electron\src\base\task\sequence_manager\thread_controller_with_message_pump_impl.cc:470)
    electron.exe!base::RunLoop::Run(const base::Location & location) Line 134 (c:\Users\thomas\code\testdata\electron\src\base\run_loop.cc:134)
    electron.exe!content::RendererMain(const content::MainFunctionParams & parameters) Line 266 (c:\Users\thomas\code\testdata\electron\src\content\renderer\renderer_main.cc:266)
    electron.exe!content::RunOtherNamedProcessTypeMain(const std::__1::basic_string<char,std::__1::char_traits<char>,std::__1::allocator<char>> & process_type, const content::MainFunctionParams & main_function_params, content::ContentMainDelegate * delegate) Line 637 (c:\Users\thomas\code\testdata\electron\src\content\app\content_main_runner_impl.cc:637)
    electron.exe!content::ContentMainRunnerImpl::Run(bool start_minimal_browser) Line 973 (c:\Users\thomas\code\testdata\electron\src\content\app\content_main_runner_impl.cc:973)
    electron.exe!content::RunContentProcess(const content::ContentMainParams & params, content::ContentMainRunner * content_main_runner) Line 394 (c:\Users\thomas\code\testdata\electron\src\content\app\content_main.cc:394)
    electron.exe!content::ContentMain(const content::ContentMainParams & params) Line 422 (c:\Users\thomas\code\testdata\electron\src\content\app\content_main.cc:422)
    electron.exe!wWinMain(HINSTANCE__ * instance, HINSTANCE__ *, wchar_t * cmd, int) Line 289 (c:\Users\thomas\code\testdata\electron\src\electron\shell\app\electron_main.cc:289)
    electron.exe!invoke_main() Line 123 (d:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:123)
    electron.exe!__scrt_common_main_seh() Line 288 (d:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288)
    electron.exe!__scrt_common_main() Line 331 (d:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:331)
    electron.exe!wWinMainCRTStartup(void * __formal) Line 17 (d:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_wwinmain.cpp:17)
    kernel32.dll!00007ffc39ee26bd() (Unknown Source:0)
    ntdll.dll!00007ffc3bf4dfb8() (Unknown Source:0)
    
  4. tsmaeder commented on Feb 20, 2023

    @tsmaeder

    I believe the version I'm using to test is v15.3.5

  5. moved this to Unsorted Items in 24-x-yon Feb 27, 2023
  6. added
    has-repro-gist
    on Mar 1, 2023
  7. moved this from Unsorted Items to Does Not Block Stable in 24-x-yon Mar 1, 2023
  8. 39 remaining items

  9. electron-issue-triage commented on Dec 5, 2025

    @electron-issue-triage

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  10. gpetrov commented on Dec 5, 2025

    @gpetrov

    Bump

  11. NotYoojun commented on Feb 22, 2026

    @NotYoojun

    Bump

  12. electron-issue-triage commented on May 24, 2026

    @electron-issue-triage

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  13. NotYoojun commented on May 24, 2026

    @NotYoojun

    Bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

13-x-y17-x-y22-x-y24-x-ybug 🪲crash 💥has-repro-gistplatform/windows

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions