pfirpfel/node-exclusive-keyboard

Issues shutting down my nodejs application when using this library

Closed this issue · 1 comments

Hello,

I'm playing around with this library.
I want to implement a small application which needs a "proper" shutdown. Unfortunately I don't get it working.
I feel it has something to do with this library...

This is the code:

const ExclusiveKeyboard = require('exclusive-keyboard');

var keyboard = new ExclusiveKeyboard("event6", false);
keyboard.on('error', keyboardError);
keyboard.on('keypress', keydown);
keyboard.on('close', keyboardClose);

function keyboardError(error) {
  console.error("Got keyboard error %s", error)
}

function keyboardClose() {
  console.log("Keyboard closed");
}

function keydown(keyboardEvent) {
  var keyId = keyboardEvent.keyId
  console.log(keyId)
}

function handleExit(signal) {
  console.log(`Received ${signal}. Close my server properly.`)
  keyboard.close();
  keyboard = undefined;
  setTimeout(function () {
    console.log("Will stop now");
    process.exit(0);    
  }, 1000);
}

process.on('SIGINT', handleExit);
process.on('SIGQUIT', handleExit);
process.on('SIGTERM', handleExit);

When you now run the app and hit <ctl+c> it prints:

^CReceived SIGINT. Close my server properly.
Keyboard closed
Will stop now

But it never really stops. I have to kill it using kill -9 <pid>
If I remove handleExit and following code, it works fine.
Any idea what could be the issue here?

found a workaround similar to lsongdev/input-event#21 (comment)