Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: call node::Stop on exit #25430

Merged
merged 3 commits into from Sep 14, 2020
Merged

fix: call node::Stop on exit #25430

merged 3 commits into from Sep 14, 2020

Conversation

nornagon
Copy link
Member

Description of Change

I believe this fixes #25267, the "illegal access" message we sometimes see at exit.

I don't have a test or a full root cause analysis for this yet, but what appears to be happening is this:

  1. When we call node::FreeEnvironment from ElectronBrowserMainParts::PostMainMessageLoopRun, it calls node::CleanupHandles.
  2. node::CleanupHandles disallows JS execution, using Isolate::DisallowJavascriptExecutionScope.
  3. node::CleanupHandles triggers a uv loop run, which triggers ElectronBindings::OnCallNextTick.
  4. ElectronBindings::OnCallNextTick runs the microtask queue, which sometimes includes an entry into JS. (Not always, though. This is the bit I'm confused about: what are the conditions in which there's a pending JS microtask here?)
  5. Attempting to execute JS while JS execution is disallowed throws an "illegal access" exception.

This change causes env->can_call_into_js() to return false after node::Stop() is called, which means InternalCallbackScope::Close() won't run any JS, which avoids this error.

The remaining questions in my mind are:

  1. Why were we not calling node::Stop() before? Was it simply an oversight, or was it an intentional choice?
  2. Under what circumstances does CleanupHandles end up invoking JS? What is putting a JS task in the microtask queue during exit?
  3. How can we write a test for this?

Checklist

Release Notes

Notes: Fixed an issue that could cause a normally-exiting process to fail with an "illegal access" message and exit code 7.

@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Sep 11, 2020
@nornagon
Copy link
Member Author

cc @addaleax @codebytere

@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Sep 12, 2020
@zcbenz
Copy link
Member

zcbenz commented Sep 14, 2020

  • Why were we not calling node::Stop() before? Was it simply an oversight, or was it an intentional choice?

node::Stop() was only introduced in v11 and no one had noticed this new handy API since then.

Copy link
Member

@codebytere codebytere left a comment

Choose a reason for hiding this comment

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

We'll also want to invoke this in ELECTRON_RUN_AS_NODE (e.g in node_main) @nornagon

with regard to why we weren't previously calling it, @zcbenz is correct, it wasn't introduced until v11 and was just an oversight. I'll try to spend some time thinking about a potential way to test this more directly 🤔

@nornagon
Copy link
Member Author

The node embedder teardown API certainly seems a lot more complex than it needs to be 🤔

@nornagon nornagon merged commit 459a95a into master Sep 14, 2020
@release-clerk
Copy link

release-clerk bot commented Sep 14, 2020

Release Notes Persisted

Fixed an issue that could cause a normally-exiting process to fail with an "illegal access" message and exit code 7.

@nornagon nornagon deleted the node-stop branch September 14, 2020 21:08
@trop
Copy link
Contributor

trop bot commented Sep 14, 2020

I have automatically backported this PR to "10-x-y", please check out #25458

@trop
Copy link
Contributor

trop bot commented Sep 14, 2020

I have automatically backported this PR to "11-x-y", please check out #25459

@trop trop bot added the merged/11-x-y label Sep 16, 2020
@shawnchain
Copy link

It seems we encountered the same issue on 8.4.1. any hints to port this patch to 8.x.y branch ?

mlaurencin pushed a commit to mlaurencin/electron that referenced this pull request Sep 16, 2020
@nornagon
Copy link
Member Author

Yeah, if this was added in Node 11 then we should probably backport to 8.x.

@nornagon
Copy link
Member Author

/trop run backport-to 9-x-y 8-x-y

@trop
Copy link
Contributor

trop bot commented Sep 16, 2020

The backport process for this PR has been manually initiated -
sending your commits to "9-x-y"!

@nornagon
Copy link
Member Author

/trop run backport-to 8-x-y

@trop
Copy link
Contributor

trop bot commented Sep 16, 2020

I was unable to backport this PR to "9-x-y" cleanly;
you will need to perform this backport manually.

@trop
Copy link
Contributor

trop bot commented Sep 16, 2020

The backport process for this PR has been manually initiated -
sending your commits to "8-x-y"!

@trop
Copy link
Contributor

trop bot commented Sep 16, 2020

I was unable to backport this PR to "8-x-y" cleanly;
you will need to perform this backport manually.

@trop
Copy link
Contributor

trop bot commented Sep 16, 2020

@nornagon has manually backported this PR to "8-x-y", please check out #25501

@trop
Copy link
Contributor

trop bot commented Sep 16, 2020

@nornagon has manually backported this PR to "9-x-y", please check out #25502

codebytere pushed a commit that referenced this pull request Sep 17, 2020
* fix: call node::Stop on exit (#25430)

* Update node_main.cc
@Raynos
Copy link

Raynos commented Nov 9, 2020

When will this be release in 10.1.6 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Electron 10 - sporadic illegal access errors resulting in exit code 7 during process.exit
7 participants