-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stale closure bug in useDecision #170
Comments
@opti-jnguyen can you reproduce the issue and do you know how to solve it in the optimizely react-sdk? my problem is that often the experiment is not activated because of this bug, but it's flaky because of the race condition. |
Internal Ticket: FSSDK-8665 |
Hi @fabb, Thanks for raising this issue. It will be very helpful if you could provide an example scenario where this exact problem occurs so that we can look into it thoroughly. Thanks! |
Hi @fabb , apologies for the very prolonged delays with initial feedback on this. Are you able to provide some example scenarios (e.g., code snippets) where this occurs? We're unfortunately unable to continue analyzing without some more information. We hope to hear from you soon. |
Ooff, that was a year ago, I need to check if I can find the scenario where I used this. But it should be reproducible just with the description in my initial comment, you just need multiple rerenders in fast succession with different |
I worked through using useDecision with (randomly) overriding the userId, but was unable to reproduce a race condition, at least in my contrived React app setup. I also cross-referenced our tickets to find where other clients may have reported stale decision. Thanks for linking and to the point in code where the rule of hooks was violated. Perhaps you could give a PR to outline your solution? |
Hi, |
Thanks for providing the history and your honest feedback. I'll work this issue via the other GH reports, closing this issue to reduce noise. |
I'm running into a stale closure bug in my application. My component with the
useDecision
hook rerenders multiple times (on purpose) and during one of the last rerenders, theoverrides
(3rd parameter) to theuseDecision
hook change (on purpose). I have an experiment that targets this exact user attribute. When setting breakpoints, I can see that the experiment is activated, and then directly deactivated again (I haveautoUpdate
active which I need due to the changing user attributes).I found that the last deactivation is caused by the 1st
useEffect
callback in theuseDecision
hook function. It calls an old stale version ofgetCurrentDecision
which uses old staleoverrideAttrs
which are not up-to-date and are missing theoverrides
I provided in the last call touseDecision
.You have a stale closure problem because you are violating the rules of hooks. The function
getCurrentDecision
is not part of the dependency array of theuseEffect
. You need to find a way to solve this, probably using auseRef
to use an up-to-date version of that function. (unfortunatelyuseEvent
has not yet been released, but maybe you could use a polyfill).The text was updated successfully, but these errors were encountered: