feat: add query-session-end and improve session-end events on Windows by savely-krasovsky · Pull Request #44598 · electron/electron · GitHub
Skip to content

feat: add query-session-end and improve session-end events on Windows#44598

Merged
VerteDinde merged 20 commits into
electron:mainfrom
savely-krasovsky:main
Nov 22, 2024
Merged

feat: add query-session-end and improve session-end events on Windows#44598
VerteDinde merged 20 commits into
electron:mainfrom
savely-krasovsky:main

Conversation

@savely-krasovsky

@savely-krasovsky savely-krasovsky commented Nov 8, 2024

Copy link
Copy Markdown
Contributor

Description of Change

Current Electron version allows to handle only WM_ENDSESSION, but not WM_QUERYENDSESSION. This cause a lot of problems while Electron app is packaged into own MSI and is a menubar/tray app. For example currently we cannot close app using Restart Manager which is used by default since Windows Installer 4.0. We have to use util:CloseApplication or call taskkil.

Quote from Restart Manager guidelines for Applications:

Applications running on Windows Vista and Windows Server 2008 should adhere to these guidelines to ensure that the Restart Manager can shut down and restart applications if necessary to install updates.

GUI applications should listen for the WM_QUERYENDSESSION message and return a value of TRUE if the application is prepared to shut down and restart. If no application returns a value of FALSE, the Restart Manager sends a WM_ENDSESSION message with the lParam parameter set to ENDSESSION_CLOSEAPP (0x1) and the wparam parameter set to TRUE. [...] If any GUI application responds to a WM_QUERYENDSESSION message by returning a value of FALSE, the shutdown is canceled.

So to properly close GUI app we need to handle WM_QUERYENDSESSION properly and return TRUE. But for now for any unknown WM message it will cause us to return FALSE (see calling code: https://github.com/chromium/chromium/blob/134896c2d6148c6bd2f7b096b0664b1e4013cb5c/ui/views/win/hwnd_message_handler.cc#L1069).

My implementation will return TRUE by default (in case of WM_QUERYSESSION), so it could probably fix longstanding issues like this: #15880. ev.preventDefault() can still return old behaviour.

Also both query-session-end and session-end now return a reasons why they receiving this event, it could be shutdown, close-app, critical and logoff.

Checklist

Release Notes

Notes: Added query-session-end event and improved session-end events on Windows

@welcome

welcome Bot commented Nov 8, 2024

Copy link
Copy Markdown

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Nov 8, 2024
@savely-krasovsky savely-krasovsky changed the title feat: add query-session-end event for Windows feat: add query-session-end and improve session-end events on Windows Nov 8, 2024
Comment thread docs/tutorial/multithreading.md
Comment thread docs/api/base-window.md Outdated

@codebytere codebytere left a comment

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.

Thanks for working on this!

Comment thread shell/browser/native_window_views_win.cc
Comment thread docs/api/base-window.md Outdated
Comment thread docs/api/base-window.md Outdated
Comment thread docs/api/browser-window.md Outdated
Comment thread docs/api/browser-window.md Outdated
Comment thread shell/browser/api/electron_api_base_window.cc Outdated
Comment thread shell/browser/api/electron_api_base_window.cc Outdated
Comment thread shell/browser/api/electron_api_base_window.h Outdated
@savely-krasovsky

Copy link
Copy Markdown
Contributor Author

@codebytere thanks for the review! Everything sounds reasonable. I would commit fixes soon.

Comment thread docs/api/base-window.md Outdated
Comment thread docs/api/base-window.md Outdated
Comment thread docs/api/browser-window.md Outdated
Comment thread docs/api/browser-window.md Outdated
Comment thread shell/browser/api/electron_api_base_window.cc Outdated
Comment thread shell/browser/api/electron_api_base_window.cc Outdated
@samuelmaddock samuelmaddock added semver/minor backwards-compatible functionality no-backport labels Nov 15, 2024
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Nov 15, 2024
Comment thread shell/browser/api/electron_api_base_window.cc Outdated
Comment thread shell/common/gin_helper/event_emitter.h Outdated

@samuelmaddock samuelmaddock left a comment

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.

API LGTM

Comment thread docs/api/browser-window.md Outdated
Co-authored-by: Sam Maddock <samuel.maddock@gmail.com>
Comment thread docs/api/base-window.md
@savely-krasovsky

savely-krasovsky commented Nov 18, 2024

Copy link
Copy Markdown
Contributor Author

Any ideas what went wrong with Linux tests? I didn't change anything related. Could it just a flaky test?

Nevermind, it was a flaky test.

@itsananderson itsananderson left a comment

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.

Left a small suggestion for the session-end event, but overall API LGTM

Comment thread docs/api/base-window.md Outdated
Comment thread docs/api/browser-window.md Outdated
Co-authored-by: Will Anderson <will@itsananderson.com>
@savely-krasovsky

Copy link
Copy Markdown
Contributor Author

@itsananderson @samuelmaddock @codebytere requesting a last review I hope! Fixed everything you highlighted.

@itsananderson itsananderson left a comment

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.

Thanks for submitting this!

@VerteDinde VerteDinde merged commit c5ea177 into electron:main Nov 22, 2024
@welcome

welcome Bot commented Nov 22, 2024

Copy link
Copy Markdown

Congrats on merging your first pull request! 🎉🎉🎉

@release-clerk

release-clerk Bot commented Nov 22, 2024

Copy link
Copy Markdown

ostend972 added a commit to ostend972/test that referenced this pull request Nov 11, 2025
…SSION)

Fix définitif du proxy résiduel lors de l'arrêt Windows - Utilisation de l'événement natif

Problème avec v1.0.13 :
- powerMonitor.on('shutdown') ne fonctionne PAS sous Windows
- C'est un bug connu d'Electron : l'événement shutdown n'est pas implémenté pour Windows
- Le proxy restait donc actif après redémarrage, bloquant internet

Solution v1.0.14 :
- Utilisation de mainWindow.on('session-end') qui intercepte WM_ENDSESSION
- WM_ENDSESSION est le vrai message système Windows d'arrêt/redémarrage
- Beaucoup plus fiable et natif à Windows
- Code : main.js:131-151 (gestionnaire session-end sur BrowserWindow)

Améliorations techniques :
- Désactivation synchrone du proxy en 1.5s max (au lieu de 2s)
- Triple nettoyage : netsh + Registry ProxyEnable + Registry ProxyServer
- Logs détaillés avec les raisons (shutdown, logoff, close-app, critical)
- Documentation : WindowSessionEndEvent Electron

Sources :
- https://www.electronjs.org/docs/latest/api/structures/window-session-end-event
- electron/electron#21093 (powerMonitor shutdown ne fonctionne pas)
- electron/electron#44598 (amélioration session-end)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
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.

6 participants