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.

unsafeFetchRecordsWithSQL & fetch - possible race condition?

See original GitHub issue

Alright, I’ve spent a fairly reasonable amount of time attempting to debug this including putting log statements in the node_modules source of watermelondb.

In a nutshell the symptom of the issue is that I’m getting the classic [Diagnostic error: Record ID foos#12345 was sent over the bridge, but it's not cached error message. It feels as though there might be some sort of race condition going on based on what I’m seeing, but I’ll let the experts be the judge of that. If I can provide any more information I’d be happy to. Here’s the breakdown:

I’m using unsafeFetchRecordsWithSQL to fetch a particular record, and separately fetching a larger set of that records type with a traditional query/fetch. These two operations happen asynchronously in the context of one another. The minimum reproducible code that I’ve created is something to the effect of the following (please note: in reality the unsafe query is more complex, but the below appears to do the job of reproducing):

collection.unsafeFetchRecordsWithSQL(`
        SELECT f.*
        FROM foos AS f
        WHERE f.id = '12345'
        LIMIT 1
    `)
    .catch((err) => {
        console.error('Error when executing unsafe fetch', err);
    })
;

// `12345` will also be fetched by this query
collection.query()
    .fetch()
    .catch((err) => {
        console.error('Error when executing safe collection fetch', err)
    })
;

In this case, the safe fetch will throw the Diagnostic error: Record ID foos#12345 was sent over the bridge, but it's not cached] error.

When logging, it appears that while the record cache has an ID in the map for 12345 it points to undefined which I can only assume is the result of the fact that the unsafeFetchRecordsWithSQL has begun, but hasn’t fully resolved.

Thoughts? Thanks in advance!

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
radexcommented, Jan 25, 2021

Yes I do, and so fixing your own problems is very much appreciated, especially if it comes with a pull request 😃 But happy to assist, I don’t want any observable race conditions in this project 😃

unsafe raw: https://github.com/Nozbe/WatermelonDB/blob/master/src/Collection/index.js#L122-L124 fetch: https://github.com/Nozbe/WatermelonDB/blob/master/src/Query/index.js#L103-L105

the former is async, so it does work in multiple ticks… the latter works on callbacks, so if the adapter can call back synchrounsly, it will. I think what needs to happen is to refactor unsafeRawfetchsomethingsomething into callback style. That should work. A simple test case would be appreciated, to make sure no regressions can occur 😃

0reactions
jspizziricommented, Jan 28, 2021

@radex any chance you could take a look at the linked PR?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixing Race Conditions in React with useEffect - Max Rozen
If you're using useEffect to fetch data, chances are you've either run into a race condition, or have one without realising it. Let's...
Read more >
Changelog - WatermelonDB documentation - GitHub Pages
unsafeFetchRecordsWithSQL () is now deprecated. ... [jsi] Fix a race condition where commands sent to the database right after instantiating SQLiteAdapter ...
Read more >
How to avoid race conditions when fetching data with Redux?
The problem is due to suboptimal state organization. In a Redux app, state keys like currentPost are usually an anti-pattern.
Read more >
WatermelonDB changelog - Awesome JavaScript - LibHunt
unsafeFetchRecordsWithSQL () is now deprecated. ... [jsi] Fix a race condition where commands sent to the database right after instantiating SQLiteAdapter ...
Read more >
Avoiding Race Conditions when Fetching Data with React ...
If you're not careful to clean up your React useEffect hook, you can end up with a race condition! In this post, we'll...
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