electron: session-end doesn't fire at all

Electron version: 3.0.3 OS: Windows 10 Pro, version: 1803

Yesterday I’ve opened session-end doesn’t fire on Electron 1.7.10 thinking that it’s an issue with Electron 1.7.10. This now at least for us, turns to be the case with Electron in general, regardless of the version (we are currently on 3.0.3).

To minimize any side-effects when testing, I’ve used Electron Starter and here is the test code:

const { app, BrowserWindow } = require('electron');
const path = require('path');
const fs = require('fs');

let mainWindow;

function createWindow() {
  mainWindow = new BrowserWindow({ width: 800, height: 600 });
  mainWindow.loadFile(path.join(__dirname, '../renderer/index.html'));
  mainWindow.on('closed', function() {
    mainWindow = null;
  });

  mainWindow.on('session-end', function() {
    fs.mkdirSync(path.join(app.getPath('downloads'), 'test123'));
  });
}

app.on('ready', createWindow);
app.on('window-all-closed', function() {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', function() {
  if (mainWindow === null) {
    createWindow();
  }
});

Expectation:

  1. Run the app
  2. Restart your machine
  3. A new folder inside Downloads folder should be created

Result Folder is not created. It seams that the event is not fired at all.

Based on my research and very minimal understanding of Windows API’s, this is how session-end should be invoked:

  1. We restart the computer
  2. System sends WM_QUERYENDSESSION messages
  3. System sends WM_ENDSESSION to all the applications that returned TRUE.

By using SendMessage I was able to play with this a bit. If I directly send WM_ENDSESSION to our app process with WPARAM: endsession, event fires.

This issue could also be very well related to callbacks for hookWindowMessage should have their return value sent to OS.

If the context from above article is true and we indeed don’t return anything on WM_QUERYENDSESSION, why do we then expect to get WM_ENDSESSION? Doesn’t WM_ENDSESSION only fires for those applications that return TRUE to WM_QUERYENDSESSION?

Please, feel free to ask for any more information, I’m willing to help.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 17 (6 by maintainers)

Most upvoted comments

I haven’t attempted to reproduce this lately but I’m pretty sure it’s still an issue.

Still an issue with Electron 13.0.0

Looking at documentation around queryendsession, it sounds like it would be useful to also provide a “query-end-session” event because “session-end” means that the app will be force killed soon - only absolutely necessary cleanup should be done there (everything possible should be in query-session-end such as autosaving).