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

feat: support 'shutdown' on Windows #24261

Closed
wants to merge 1 commit into from
Closed

Conversation

codebytere
Copy link
Member

Description of Change

Take 2 of #15620.

From initial PR:

Handling shutdown correctly on Windows is more complex than Linux/Mac, since any process can be killed during shutdown. That means renderer processes can be killed first (and usually are) , which could result in loss of data even if the event is handled in the browser process.

Checklist

Release Notes

Notes: Add Windows implementation of powerMonitor "shutdown" event

@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Jun 23, 2020
@codebytere codebytere force-pushed the shutdown-impl-win branch 8 times, most recently from 39992c5 to ea5a88e Compare June 24, 2020 03:06
@codebytere codebytere marked this pull request as ready for review June 24, 2020 03:33
: " on browser process"));
if (blocker && !blocker->OnQueryEndSession()) {
LOG(INFO) << "Shutdown blocked";
ShutdownBlockReasonCreate(hwnd, L"Ensure a clean shutdown");
Copy link
Contributor

Choose a reason for hiding this comment

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

I would consider making this string configurable - this gets shown on shutdown to users and if it's hard-coded to English, this will result in a Confusing Experience™™™ to people using non-English locales!

Copy link
Member Author

Choose a reason for hiding this comment

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

oh excellent point, will do!

@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Jun 24, 2020
@codebytere codebytere force-pushed the shutdown-impl-win branch 7 times, most recently from d6e0f19 to d3bd580 Compare July 2, 2020 18:05

export function setup (binding: typeof process['_linkedBinding']) {
// TODO(codebytere): fix typedef here.
(binding as any).setShutdownHandler(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The shutdown handler in renderer returns undefined, which would then always cancel the shutdown.

// so, as explained here
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa376890(v=vs.85).aspx
//
// The WM_QUERYENDSESSION message must be handled in every every process managed
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not familiar with the WM_QUERYENDSESSION message, but if it must be handled in every process then we also need to handle it in GPU/utility/network/... processes then.

@codebytere codebytere force-pushed the shutdown-impl-win branch 2 times, most recently from 66a38c2 to 1ebc77e Compare August 24, 2020 17:45
@zcbenz zcbenz added the wip ⚒ label Sep 8, 2020
@LorenzoBiondani
Copy link

Any updates on this?

@codebytere
Copy link
Member Author

@LorenzoBiondani none at the moment but i haven't forgotten about it! i'll work on it more this week

@LorenzoBiondani
Copy link

@codebytere thank you, I don't have much time that I can dedicate to this but if you need something let me know, I will try to help.

// so, as explained here
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa376890(v=vs.85).aspx
//
// The WM_QUERYENDSESSION message must be handled in every every process managed
Copy link

Choose a reason for hiding this comment

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

typo / repeated word:

"handled in every every process" should be "handled in every process"

: " on browser process"));
if (blocker && !blocker->OnQueryEndSession()) {
LOG(INFO) << "Shutdown blocked";
ShutdownBlockReasonCreate(hwnd, L"Ensure a clean shutdown");
Copy link

Choose a reason for hiding this comment

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

Calling ShutdownBlockReasonCreate() inside the WM_QUERYENDSESSION handler allows us to notify our code to clean up and shutdown quickly, but we can also use it preemptively when we know that we're running a critical section, like burning a cd, or saving a file to disk.

It would be really nice if an app could call a "block" or "unblock" API when the programmer knows in advance where a shutdown should be blocked, releasing the block afterwards. The "block" API would call ShutdownBlockReasonCreate(hwnd, L"A critical task is running."); The "unblock" API would call ShutdownBlockReasonDestroy(hwnd); The string passed into ShutdownBlockReasonCreate() is shown by windows as the reason for the shutdown being held up.

Application Shutdown Changes in Windows

@giftedunicorn
Copy link

Is anyone pushing this merge?

@cberescu
Copy link

any updates on this?

@zhushenwudi
Copy link

@LorenzoBiondani none at the moment but i haven't forgotten about it! i'll work on it more this week

hello, your pull request is not merge successful, can you fix it and pull again? 3Q

@nornagon
Copy link
Member

Related: #15880

@gergof
Copy link

gergof commented Sep 6, 2022

Any updates on this?

In the meantime I've created a node addon which can be used to detect and to block the shutdown of the system in electron: https://www.npmjs.com/package/@paymoapp/electron-shutdown-handler

@codebytere codebytere closed this Oct 2, 2023
@zcbenz zcbenz deleted the shutdown-impl-win branch October 26, 2023 06:38
@savely-krasovsky
Copy link
Contributor

Can someone explain why we need to handle events from both browser and renderer?

cc @codebytere

@dladeira
Copy link

Was this issue addressed in the end?

Docs say that shutdown for powerMonitor is Linux and MacOS only
https://www.electronjs.org/docs/latest/api/power-monitor#event-shutdown-linux-macos

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

Successfully merging this pull request may close these issues.