8

I'm building an electron app that must reload the render process window if a crash happens.

Currently I can restart the app from the main process

app.relaunch();
app.quit();

But I cannot detect the window crashing. I tried using the

win.on('unresponsive', () => { ... } );

But the event is not getting generated when I crash the process.

To crash the process I tried:

  • invoking the process.crash()
  • using all the available memory.

Both ways successfully crash the process but again, I cannot find a way to detect it.

I tried also using, from the render process, the window.onerror(...) and sending via IPC to the main process a message when the crash is detected, but this seems not to work as well.

Carlinho89
  • 157
  • 2
  • 14
  • Did you ever figure anything out on this? I have the same issue - render process occasionally crashes and it would be really nice to just reload automatically or take other appropriate action. – Brad Peabody Aug 20 '18 at 00:17

2 Answers2

2

You should be looking for the 'crashed' event in webContents. Check https://electronjs.org/docs/api/web-contents#event-crashed

For example put something like this in main process:

win.webContents.on('crashed', (e) => {
    app.relaunch();
    app.quit()
});
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
hristovsn
  • 21
  • 2
  • 1
    This makes sense to listen to `crashed` on webContents, but this still does not work, at least if you're manually calling `process.crash()` in the window. – Collier Devlin Sep 25 '19 at 18:47
0

maybe look into "pm2-windows-service" which can install your app as windows service and watch if it crashes, to restart it

https://www.npmjs.com/package/pm2-windows-service

also electron has app.setLoginItemSettings({ openAtLogin: true }); but that does not guard for crash, only provide automatic app start at windows login

Peminator
  • 783
  • 1
  • 9
  • 24