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.

Race condition/out of order results possible when data.src is an async function

See original GitHub issue
  • System Information

    • Browser type and version Any (I’m on Chrome 94.0.4606.114)
    • OS type and version Any (I’m on Chrome OS)
    • WINDOWS: be sure to indicate which terminal you’re using – (i.e., cmd.exe, powershell, git- bash, cygwin, Ubuntu via windows subsystem for linux, etc…)
    • Node version (IF applicable)
      • Any error messages that may be in the console where you ran npm start
    • Any error messages in the JS console
  • Describe the bug

I have a simple async data.src function like this in the config that gets autocomplete results from an endpoint. I’m also using a no-op search engine so I just rely on results from the endpoint.

...
src: async (query) => {
        const response = await fetch(url);
        let json = await response.json();
        
        ... // do processing on json
        return json;
}
...
searchEngine: (q, r) => r,
...

The problem is that if the endpoint returns things out of order the final displayed results could be old and not make sense with the query. In practice this happens a lot because the geolocation API I’m using is rather unreliable (in terms of speed).

  • To Reproduce

This is difficult to reproduce because it requires an autocomplete endpoint/API to return results out of order. I’m using geoapify.

  • Expected behavior

Results from the src function be rejected if newer results have already been returned, or else some workaround allowing old results to be rejected.

I have tried this workaround which does work, but leaves uncaught errors:

...
src: async (query) => {
        const sent_timestamp = (new Date()).getTime();

        const response = await fetch(url);
        let json = await response.json();
        
        // Reject out of order results
        if (sent_timestamp < newest_timestamp) {
            throw Error("Old results");
        }
        newest_timestamp = sent_timestamp;
        
        ... // do processing on json
        return json;
}
...

I’m new to promises/async so might be missing some other way to reject old results cleanly. But regardless I believe this should be handled automatically.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
folknorcommented, Nov 11, 2021

Returning an Error instead of throwing

Haha! Can’t believe I misread that in your code, silly me 😛 I didn’t even read the keyword throw, I just read the Error part and wrote my comment. Good thing accidentally helped you anyway!

1reaction
ethanhjenningscommented, Nov 11, 2021

Returning an Error instead of throwing it works just fine for me! Thanks! Also, FYI, I found jQuery autocomplete had this same issue and a fix was implemented: https://bugs.jqueryui.com/ticket/5982

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does this JavaScript example create “race conditions”? (To ...
Yes, race conditions can and do occur in JS as well. Just because it is single-threaded it doesn't mean race conditions can't happen ......
Read more >
Node.js race conditions
A race condition is a type of programming error that can occur when multiple processes or threads are accessing the same shared resource,...
Read more >
Is there Race Condition in Javascript: Yes and No
In a nutshell: The race condition in Javascript could only be fabricated but one could almost never find race condition in reality.
Read more >
JavaScript Race Conditions & Function Throttling with Promises
In this blog post I outline a method to avoid race conditions and to throttle a function in JavaScript. Race conditions in JavaScript?...
Read more >
How to avoid async race conditions in JavaScript - Medium
The idea is that JavaScript is singe threaded (unless we use background workers), and when some async call returns, its result is put...
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