Quitting electron app: no process 'exit' event or window 'unload' event on renderer


#1

I am spawning a command line program from the renderer process:

    this.process = spawn(execPath, args, {
      cwd: this.options.cwd,
      options: {
        detached: false
      }
    });

When quitting the electron app the child process gets reparented outside of the electron process tree so it gets orphaned and has to be killed manually.

This is how I usually do it from a node app and it works fine, but not in Electron:

    let killChild = () => {
      console.log('killing scsynth', this.process);
      if (this.process) {
        this.process.kill('SIGTERM');
        this.process = null;
      }
    };

    process.on('exit', killChild);

With Electron the renderer process does not get any ‘exit’ or ‘beforeExit’ event.

The renderer process is also not getting these window events:

window.addEventListener('beforeunload', () => {
  console.log('beforeunload');
  soundApp.stop();
});
window.addEventListener('unload', () => {
  console.log('unload');
  soundApp.stop();
});

These seem like bugs to me. I would expect application quit to fire page unload (do normal web browsers do that ?)

Certainly it should fire process ‘exit’

I can catch app ‘will-quit’ from the main/background side:

app.on('will-quit', function() {
  console.log('will quit !');
  event.preventDefault();
  // but now the window is shut (renderer thread is stopped)
 // and the child process has already been reparented 
 // and the app is now hanging.
});

Perhaps I should just move it to main but then there will be a lot of ipc traffic from renderer to main.
It’s probably the only solution.


#2

Couldn’t you implement your own “will-exit” event on the renderer side? Any time you call something to close the window, raise the event and then things can handle it.