0

I'm running a Node.js application that is doing some GPIO work. If any sort of error occurs or the user closes the application with CTRL + C I'm currently handling it. One scenario I can't catch though is when I disconnect my SSH session by closing my SSH client.

Closing my SSH client is killing the Node.js application (which is fine) but there are certain GPIO cleanup methods that need to happen before it closes.

These are the events I've tried and none of them are catching when SSH closes:

var cleanup = () => { /*cleanup code*/ };
var domain = require('domain').create();
domain.on('error', cleanup);
process.on('uncaughtException', cleanup);
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
process.on('exit', cleanup);
process.on('unhandledRejection', cleanup);
process.on('disconnect', cleanup);
process.on('SIGUSR1', cleanup);
process.on('SIGUSR2', cleanup);
process.on('beforeExit', cleanup);

What event emits when an app is closing due to an SSH session terminating?

NessDan
  • 1,137
  • 1
  • 16
  • 34
  • It's typically SIGHUP, did you try listening for that signal? – mscdex Sep 29 '18 at 02:49
  • @mscdex thank you for the response, unfortunately that doesn't seem to change anything :( You did lead me to https://nodejs.org/api/process.html#process_signal_events and I will read some of these and add a few more to test and see if they'll also work. – NessDan Sep 29 '18 at 05:31

1 Answers1

0

Sorry for the not so great answer, but I'm not sure which of these is the reason it's working (or if it's a combination of many of these, since multiple signals get fired when the SSH session closes while running the app) but the final list I landed on was:

process.on('SIGINT', cleanup);
process.on('SIGHUP', cleanup);
process.on('SIGTERM', cleanup);
process.on('SIGCONT', cleanup);

Now, exiting the SSH session while running the program properly runs the cleanup methods.


For anyone curious of what all my error handling code actually looks like:

var domain = require('domain').create();
domain.on('error', cleanup);
process.on('unhandledRejection', cleanup);
process.on('uncaughtException', cleanup);
process.on('SIGINT', cleanup); // Handle CTRL + C, *nix only https://stackoverflow.com/a/20165643/231730
process.on('SIGHUP', cleanup); // SSH hangup (probably needed with SIGCONT)
process.on('SIGTERM', cleanup); // Handles `kill PID` command on linux
process.on('SIGCONT', cleanup); // Handle when SSH connection closes that was running the app
NessDan
  • 1,137
  • 1
  • 16
  • 34