zigbee-herdsman: Unhandled promise rejection when stopping the coordinator
Hey, I encountered a bug today where my Node process exited due to an unhandled promise rejection and finally found out that stopping the coordinator was the cause.
Following code can reproduce the issue:
const {Controller} = require("zigbee-herdsman");
const coordinator = new Controller({
databasePath: 'test.db',
serialPort: {path: '/dev/ttyACM0'}
});
let date = new Date();
coordinator
.start()
.then(() => {
coordinator.stop()
.then(() => console.log("Stopped", new Date() - date))
.catch(err => console.log("Catch error (2):", new Date() - date, err));
console.log("Controller created", new Date() - date);
})
.catch(err => console.log("Catch error (1):", new Date() - date, err))
I added dates for debugging purposes but when the app crashes no error handler is invoked. I used my stopwatch and it takes 10 seconds to crash. Output:
Controller created 211
Stopped 319
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "TIMEOUT".] {
code: 'ERR_UNHANDLED_REJECTION'
}
I tried to get the stack using the unhandled rejection event but it was undefined, I do not know where to search. It would be nice to restart the controller without terminating Node itself. I am using CC253X with DECONZ Adapter.
Maybe this information is helpful to debug the problem. If you have any further questions, let me know. I am using v0.14.40 and the test.db file had no previous devices in it.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (16 by maintainers)
Commits related to this issue
- Add await on deconzAdapter stop. https://github.com/Koenkk/zigbee-herdsman/issues/555 — committed to Koenkk/zigbee-herdsman by Koenkk 2 years ago
- Fix unhandled promise rejection (#555) Deconz driver: Clear intervals and shared arrays after close; catch all unhandled promises and forward errors to debugger — committed to ercksen/zigbee-herdsman by ercksen 2 years ago
- Fix unhandled promise rejection (#555) (#561) Deconz driver: Clear intervals and shared arrays after close; catch all unhandled promises and forward errors to debugger — committed to Koenkk/zigbee-herdsman by ercksen 2 years ago
You can click the edit icon here: https://github.com/Koenkk/zigbee-herdsman/blob/master/src/adapter/deconz/driver/driver.ts (pencil) then make a pr
This looks good to me, could you make a pr?
I think I figured out why it crashes. In line 101 of
src/adapter/deconz/adapter/deconzAdapter.tsthere is a missingawaitfor the driversclosefunction. Butclose()returns a promise that is not returned or caught. So if it rejects, Node will terminate. Of course that still would mean thatclose()throws an error but that would probably be another topic. I will do some testing on the next days when I have more time and let you know.