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 where `data` or `error` return `1` when key is `null`

See original GitHub issue

Bug report

Description / Observed Behavior

On very rare occasions, if I return null in the key function of useSWR, my data and / or error field return 1.

At first I thought it might be due to me calling mutate incorrectly somewhere. But it’s happening for different calls (get user, get artists), so I no longer think that’s the acse.

Plus, if the key returns null, shouldn’t the data and error always be undefined?

I did manage to wrap these calls and logged that error was 1 while key was null. But refreshing the page got rid of that bug.

Expected Behavior

Don’t return 1 for data or error if the key returns null.

Repro Steps / Code Example

I don’t have a reproduction currently, I’m still working on that. I’m still trying to reproduce this in my own app.

So as it stands, I know that this issue might not be actionable. Hopefully this isn’t a bug on the SWR side and I’ll find it. However, I find it puzzling that SWR itself is returning these values.

Can any of the maintainers think of a potential reason this could be happening? Maybe an incorrect falsy check, or something like that, which results in SWR returning 1?

Additional Context

SWR version: 0.5.6

This started happening after I upgraded from 0.3.2 to 0.5.6, so I’m going to see if downgrading again solves it.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:23 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
shudingcommented, Aug 16, 2021

Thanks @nandorojo! I updated the sandbox a bit to make it work: https://codesandbox.io/s/angry-snowflake-r6pj5?file=/pages/index.js. I think we have to mutate to force trigger revalidating for existing hooks.

1reaction
shudingcommented, Aug 15, 2021

Plus, if I accidentally auto-import the global mutate from SWR instead of my own custom one, it could be hard to catch.

That’s a very good point, we definitely want to find a way to avoid that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Insert row if not exists leads to race condition? - Stack Overflow
The primary key should be (link, title) or just (link) if title can be null . Then any try to insert an existent...
Read more >
Handling API request race conditions in React
If there is a non-null probability that your UI could end up in such a state, your app is subject to race conditions....
Read more >
SQL Server Simplify Racing condition query
CREATE TABLE dbo.Customer ( RowId bigint IDENTITY(1,1) NOT NULL, CustomerId guid NOT NULL, Name varchar(255) NOT NULL, CONSTRAINT PK_RowId ...
Read more >
What is Race Condition in Java Multithreading? Examples
In the code for hashTable the condition is, if the key is not there already then store the key/value pair. So in that...
Read more >
“UPSERT” Race Condition With MERGE | Dan Guzman Blog
In this post, I'll show how to prevent duplicate key errors and data problems with the MERGE statement too. You might want to...
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