1

I'm using a 3rd party library to do a heavy calculation. The problem is it hangs for some input. I'd like to add timeout to the synchronous operation. My code runs in single threaded node environment on GCP (Cloud Fuctions).

const result calculate(x, y);

This is what I've already tried:

let calculation = new Promise((resolve, reject) => {
   const result = calculate(x, y)
   resolve(result)
 })

let timeout = new Promise((resolve, reject) => {
   let id = setTimeout(() => {
     clearTimeout(id);
     reject()
   }, 5000)
 })

const result = await Promise.race([timeout, calculation])

Unfortunately it doesn't work, as the calculate method locks the thread so the timeout can't reject the promise. Is it possible to make synchronous method timeout after a given time?

tomrozb
  • 25,773
  • 31
  • 101
  • 122
  • 2
    Move the processing to a different thread, then see if the thread finishes in time. – CertainPerformance Apr 19 '20 at 08:51
  • Does this answer your question? [Node, How to timeout on a synchronous code?](https://stackoverflow.com/questions/48722326/node-how-to-timeout-on-a-synchronous-code) – Cameron Little Apr 19 '20 at 10:23
  • @CameronLittle that doesn't help, as the method that's looping infinitely comes from a 3rd party library. I simply can't change its internals. – tomrozb Apr 20 '20 at 09:29
  • @CertainPerformance it runs on node in single threaded environment (Cloud Functions from GCP platform). Added that info to the question. – tomrozb Apr 20 '20 at 09:30
  • If you have no way to create a separate environment for processing, I don't think it's possible without going into the expensive calculation code and staggering it up – CertainPerformance Apr 20 '20 at 09:31
  • Why do you need timeout? single threaded node doesn't mean it will block other js threads in your code. What is happening inside calculate function? Is it promise or just calculation function? – hurricane Apr 20 '20 at 09:38
  • @hurricane just calculation functions. As I mentioned I'm using 3rd party library which is great, but I found a case when it hangs for specific input. I just wanted to give 5 seconds for the calculations and timeout after that time. What do you think? Here's info that I can't start another thread: https://stackoverflow.com/questions/53985802/multi-thread-firebase-function – tomrozb Apr 21 '20 at 10:16
  • @CertainPerformance you mean copy and paste the source code of the 3rd party library into my project and modify it to timeout after a given time? – tomrozb Apr 21 '20 at 10:20
  • What is the result of `console.time('timer'); const result = calculate(x, y); console.timeEnd('timer');` – hurricane Apr 21 '20 at 10:27
  • @tomrozb Yes, essentially, or something like [this](https://stackoverflow.com/questions/61338780/is-there-a-faster-way-to-yield-to-javascript-event-loop-than-settimeout0), which yields control flow back every few milliseconds to see if the process should be stopped – CertainPerformance Apr 21 '20 at 11:09

0 Answers0