question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timeout happening too early

See original GitHub issue

I’m reporting the case from this SO question:

The following example times out in most cases (outputs timed out):

Promise = require('bluebird');

new Promise(resolve => {
    setTimeout(resolve, 1000);
})
    .timeout(1001)
    .then(() => {
        console.log('finished');
    })
    .catch(error => {
        if (error instanceof Promise.TimeoutError) {
            console.log('timed out');
        } else {
            console.log('other error');
        }
    });

Does this mean that the Bluebird’s promise overhead takes longer than 1ms? I see it often time out even if I use .timeout(1002).


Using Bluebird 3.5.0, under Node.js 8.1.2

I would expect this to never output timed out, since the timeouts happen in order, and the latter with higher timespan value. Given that the promise chain resolution happens in the microtask queue, the fulfillment callback should log finished before the macrotask timer callback from .timeout() runs and even get a chance at cancelling the promise.

Is this a bug, or are my expectations off?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
benjamingrcommented, Jul 10, 2019

@sanji-programmer Node timers are not accurate to the millisecond fwiw. There is an open bug for that I can hunt it if it’s important to you.

1reaction
sanji-programmercommented, Jul 10, 2019

I can confirm that the fix 892286b can avoid the race reported by @bergus; I could reproduce the race with a tool I’ve worked with to test different schedules of the event loop.

Nevertheless, if we do the opposite:

const Promise = require('bluebird');

new Promise(resolve => {
    setTimeout(function timeout() { resolve() }, 1001);
})
    .timeout(1000)
    .then(function then() {
        console.log('finished');
    })
    .catch(function catch_(error) {
        if (error instanceof Promise.TimeoutError) {
            console.log('timed out');
        } else {
            console.log('other error');
        }
    });

the race remains with this code giving ‘time out’ or ‘finished’ based on the status of event loop.

It seems that, at least in Node, callback timeout() in this code and callback timeoutTimeout() in timers.js are registered in the same tick of event loop; so some order between them can be defined. The issue may be that this.drainQueues() (line 16 of async.js) is registered using setImmediate() and it can race with the other timeout callbacks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net - Why is my session timeout happening early?
The timeout attribute in sessionState allows you to set the time in minutes,and default timeout is 20 minutes.
Read more >
Session expires too early - MSDN - Microsoft
It's running on a production server with other applications hosted on it too. Everything was fine unitll suddenly my asp application seems to...
Read more >
Why does my application timeout, before the Session timeout ...
All of the timeouts in the application are set to reasonable values, so why does the browser session time out early.
Read more >
Taking a Timeout from Poor Performance | APIs You Won't Hate
Making timeouts happen early is much more important than getting a fast failure. The most important benefit of failing fast is to give...
Read more >
What is Time-Out? | Essentials | Parenting Information - CDC
Time-out is when your child is removed from where the misbehavior happened. Your child is away from all things that are fun.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found