1

I would like to be able to tune the Node event loop to abort or throw an exception if ever a piece of code listening for an event takes too long to execute.

Using the Async Hooks API, it is possible to monitor the time that a callback runs for. However, I cannot find a way to take control in any way.

Ideally I would like to be able to tune Node so that no synchronous code runs for too long. Code running for too long will cause the application to become blocked and unresponsive. (see example). So if I could set a limit to how long any callback is allowed to run for, that would be good.

From my research I think that what I want is not possible, but I would love to be wrong :)

EDIT: Any documentation about what tuning options are available would close this issue. So far I have only heard of garbage collection and heap size.

hrdwdmrbl
  • 4,814
  • 2
  • 32
  • 41
  • Ideally, if code must not consume too much time on the Javascript stack, it should be designed such that it does not consume too much time on the Javascript stack. An example is described on the page you linked: If a JSON parse could contend with more important code on the stack, the computation should be broken up as a set of streaming components that yield to other functions that may need the stack. Trying to design a system that terminates **any callback** in a way that keeps the program in a consistent state feels like a larger undertaking than considering the design of those callbacks. – ctt May 08 '19 at 04:32
  • If a callback is being terminated, I meant to imply that the work would be thrown away and not resumed. The dev would need to be aware of that when using the setting. – hrdwdmrbl May 08 '19 at 04:45

1 Answers1

1

Node.js is single-threaded by design. For this reason it's not a good choice for CPU-intensive apps in the first place.

It's generally unsafe to interrupt a running thread asynchronously, regardless of the programming language, and JavaScript is no exception. A thread might be in a state when it locked some resources, allocated memory, is modifying global data, etc. so interrupting it can lead to deadlock, memory leaks, data corruption, etc.

A process that can be safely interrupted needs to implement that explicitly, for example by periodically checking for some flag and then stopping in a safe manner. Games often use time-based compute loops in which the elapsed time is checked periodically and the loop exits when it reaches e.g. 20ms, for the process to be continued later during the next invocation.

When possible, break down a long-running computation into a series of shorter steps/callbacks.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • You're right but that doesn't answer my question :) In reality I am exploring those options for my application but I want to make sure to thoroughly research this avenue too – hrdwdmrbl May 08 '19 at 10:22
  • The answer is your research is correct - what you want cannot be done ;) – rustyx May 08 '19 at 10:52
  • That's alright. As you say, there are multiple ways for me to accomplish the same thing. I'm just looking for a simple way to enforce at as a global constant a minimum responsiveness. This just means that we need to be very observant forever – hrdwdmrbl May 08 '19 at 10:57