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 on <Can />

See original GitHub issue

Well, it might not actually be a race condition, as the intended action loses every time, but:

When the <Can /> component’s componentWillReceiveProps is called with a new Ability instance, a setState is called, and then the component’s connectToAbility is called which ultimately fires off this.recheck(). https://github.com/stalniy/casl/blob/774457aa43b8f776a9d59542f1462bb191ae0880/packages/casl-react/src/Can.js#L54-L61

That method (this.recheck) calls this.check and uses this.props as params and this.state.ability as the ability. However, the call stack is still nested under componentWillReceiveProps, so the props we’re rechecking against are the old props, and the state is most likely not the (now updated) state (see: https://reactjs.org/docs/react-component.html#setstate)!

The result is that the render method uses this.state.allowed which was (likely) produced erroneously as described above.

Therefore a good solution to this problem is to move the call to connectToAbility into the setState callback:

  componentWillReceiveProps(props) {
    if (props.ability && this.state.ability !== props.ability) {
      this.setState({ ability: props.ability }, () => {
        this.connectToAbility(props.ability);  // or this.state.ability
      });
    } else {
      this.recheck(props);
    }
  }

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
stalniycommented, Nov 8, 2018

published in @casl/react@0.8.1

0reactions
dfeecommented, Nov 8, 2018

Thanks for the quick update!

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is a Race Condition? - TechTarget
A race condition is an undesirable situation that occurs when a device or system attempts to perform two or more operations at the...
Read more >
What is a Race Condition? | Baeldung on Computer Science
One or more possible outcomes may be undesirable, resulting in a bug. We refer to this kind of behavior as nondeterministic.
Read more >
Race conditions and deadlocks - Visual Basic - Microsoft Learn
A race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the...
Read more >
multithreading - What is a race condition? - Stack Overflow
A race condition occurs when two or more threads can access shared data and they try to change it at the same time....
Read more >
What is a Race Condition? Definition, Examples, Types
A race condition occurs when two threads use the same variable at a given time. Deadlock exists when two threads seek one lock...
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