Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminating hung Promises in javascript

I have a javascript program that I would like to keep executing constantly by feeding an input from RabbitMQ. The problem is that it can occasionally hang indefinitely and block the whole execution. My initial idea was to use Promise.race to timeout after a specific period of time and let the next message in, but the broken Promise itself is still running somewhere in the background and never ends. In this design I will start generating "zombie" Promises that will eventually eat my system resources (since my app is running the whole time). I don't really have a control over this program. There is nothing like abort() function I can send to it. So my question is: Is there a way to kill the Promise and remove it from event loop completely?

If it is not possible, then how about spawning second nodejs program as a child process and kill it if is something goes wrong? Will it properly clean my system resources?

like image 794
Bociek Avatar asked Sep 18 '26 13:09

Bociek


1 Answers

Is there a way to kill the Promise and remove it from event loop completely?

No. Promises represent the result of an asynchronous task, they don't know about the task itself and thus cannot cancel it.

If it is not possible, then how about spawning second nodejs program as a child process and kill it if is something goes wrong?

That would be an option, but spawning a process is a heavy operation as it needs its own runtime / memory / etc. so you shouldn't do that often.

Instead you can manage the cancellation of the underlying asynchronous task. It depends on the actual task wether that is possible though.

like image 178
Jonas Wilms Avatar answered Sep 20 '26 02:09

Jonas Wilms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!