You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
iOS - App crashes after turning off the internet connection #3485
👋 Friendly reminder that deploy blockers are time-sensitive ⏱ issues! Check out the open StagingDeployCash deploy checklist to see the list of PRs included in this release, then work quickly to do one of the following:
Identify the pull request that introduced this issue and revert it.
I've seen this and I'm trying to run the staging branch locally to fix this. Running into some issues with XCode and ECash so I'm resolving that right now. Haven't been able to recreate locally yet.
We have a listener subscribed to ONYXKEYS.NETWORK_REQUEST_QUEUE that also uses Onyx.set to update the same key, which in turn is triggering the listener again
The next value for peristedRequests is [] so the expression !persistedRequests does not skip, code continues and calls Onyx.set again and this triggers the callback again and again... forever
The check can be updated to
if (didLoadPersistedRequests || _.size(persistedRequests) === 0) {
return;
}
_.size would return 0 for undefined - _.size(undefined) // 0
Alternatively you can call Onyx.remove(ONYXKEYS.NETWORK_REQUEST_QUEUE) as it seem that was an expected case e.g. not having !persistedRequests
I think this was an existing issue, it just became exposed as now Onyx.set works much faster because it optimistically calls subscribers with the new value
I think this was an existing issue, it just became exposed as now Onyx.set works much faster because it optimistically calls subscribers with the new value
I think you're spot on about this, @kidroca. I came to a similar conclusion by following the loops and already have a PR for it: #3511
It's a testament to how much faster Onyx is that we're seeing regressions like this 😄 .
Now that I look at it again, we might have a problem with the new Onyx.set - this was not an existing problem.
Before Onyx.set used to call keysChanged in the .then block of a Promise, even if the write happens instantly promises are resolved on the next tick and didLoadPersistedRequests would always be set by that time
Now Onyx.set is calling keysChnaged right away, which means that code "stops" at the line Onyx.set(...) and proceeds after all the sync code in Onyx.set is over, which means after keysChanged is called
This can be a problem because keysChanged might have to inform a lot of connections and the UI thread will be blocked during this time - this can be related to the short freezes we get when a chat is selected
Instead we should probably update Onyx.set to schedule the call to keysChanged for the next tick by doing something like
setImmediate(() => keysChanged(key, value)) or Promise.resolve().then(() => keysChanged(key, value)))
This would still feel instant to the user, but would not cause problems like the above
I'm thinking it would actually be better to avoid situations like the above by explicitly avoiding extra loops. We have a weird scenario here (or is this actually common in Expesnsify.cash?) where we're calling the Onyx.set within the Onyx.connect's callback that's causing the infinite loop. Even if keysChanged happened in a promise, it might get called an extra time more than it's needed right?
I think the right solution here is to quit early for empty persistedRequests. Adding the promise there is equivalent to just swapping the order of didLoadPersistedRequests = true and Onyx.set(ONYXKEYS.NETWORK_REQUEST_QUEUE, []). All of these will solve the problem here, but the latter two solutions runs the loop an extra time for no reason.
I think wrapping keysChanged in a promise in Onyx.set() is not a great idea because then we might introduce more race conditions in our code. Code where we're expecting it to go one extra tick feels like it would be harder to maintain.
I think the right solution here is to quit early for empty persistedRequests
Yes, the fix is good it should stay whatever is decided
I think wrapping keysChanged in a promise in Onyx.set() is not a great idea because then we might introduce more race conditions in our code. Code where we're expecting it to go one extra tick feels like it would be harder to maintain.
So far everything worked without expectation of how long it would take for set to finally call keysChaged and I think it should stay that way - components don't subscribe to set or merge they subscribe to receive updates at some point
My concern is only that now set is blocking whatever happens on screen as it has a lot of synchronous code - I might be wrong, but we can investigate if this is the reason (well one of them) for the little hiccup when you select a chat
E.g. if you have a button component that calls Onyx.set in it's press handler it might block the UI for a bit if keysChanged have to iterate a lot of subscribers
Another options would be to use the InteractionsManager and run the keysChanged after interactions are complete - this way hopefully are not blocking UI
Just catching up here, but nice detective work everyone! I hear where you are coming from @TomatoToaster, but I think in general we should first try to preserve the original behavior we had when it's something low level like this and could potentially affect many things.
Since the entire app has been built on the assumption that subscribers are notified async I think we should maybe continue this way for now and move the keysChanged() call to another tick like @kidroca is suggesting.
I think the right solution here is to quit early for empty persistedRequests. Adding the promise there is equivalent to just swapping the order of didLoadPersistedRequests = true and Onyx.set(ONYXKEYS.NETWORK_REQUEST_QUEUE, []). All of these will solve the problem here, but the latter two solutions runs the loop an extra time for no reason.
There's no real problem with the original code - if we can assume things are asynchronous. The order doesn't make any difference in that case. But maybe it's a little confusing if you're not familiar with how Onyx will work.
Also, separately, I think there is maybe a strong case to discuss a future where Onyx isn't an asynchronous system now that we have the local caching stuff. But probably requires a larger conversation.
Activity
MelvinBot commentedon Jun 9, 2021
Triggered auto assignment to @CortneyOfstad (
AutoAssignerTriage), see https://stackoverflow.com/c/expensify/questions/4749 for more details.OSBotify commentedon Jun 9, 2021
👋 Friendly reminder that deploy blockers are time-sensitive ⏱ issues! Check out the open
StagingDeployCashdeploy checklist to see the list of PRs included in this release, then work quickly to do one of the following:MelvinBot commentedon Jun 9, 2021
Triggered auto assignment to @TomatoToaster (
Engineering), see https://stackoverflow.com/c/expensify/questions/4319 for more details.TomatoToaster commentedon Jun 9, 2021
I've seen this and I'm trying to run the staging branch locally to fix this. Running into some issues with XCode and ECash so I'm resolving that right now. Haven't been able to recreate locally yet.
trjExpensify commentedon Jun 10, 2021
👋 Any luck, @TomatoToaster?
8 remaining items
kidroca commentedon Jun 10, 2021
@TomatoToaster thanks for the links
Here's what I've found out:
https://github.com/Expensify/Expensify.cash/blob/533a0230986b3724397e1152f7ce70a2edb2569a/src/libs/Network.js#L32-L45
We have a listener subscribed to
ONYXKEYS.NETWORK_REQUEST_QUEUEthat also usesOnyx.setto update the same key, which in turn is triggering the listener againThe check here does not work well
https://github.com/Expensify/Expensify.cash/blob/533a0230986b3724397e1152f7ce70a2edb2569a/src/libs/Network.js#L35-L37
The code that follows updates Onyx and this triggers the
callbackagainhttps://github.com/Expensify/Expensify.cash/blob/533a0230986b3724397e1152f7ce70a2edb2569a/src/libs/Network.js#L42
The next value for
peristedRequestsis[]so the expression!persistedRequestsdoes not skip, code continues and callsOnyx.setagain and this triggers thecallbackagain and again... foreverThe check can be updated to
_.sizewould return0forundefined-_.size(undefined) // 0Alternatively you can call
Onyx.remove(ONYXKEYS.NETWORK_REQUEST_QUEUE)as it seem that was an expected case e.g. not having!persistedRequestsI think this was an existing issue, it just became exposed as now
Onyx.setworks much faster because it optimistically calls subscribers with the new valuekidroca commentedon Jun 10, 2021
Here's a debug video of the loop:
Expensify.cash.-.Google.Chrome.2021-06-10.15-23-56.mp4
TomatoToaster commentedon Jun 10, 2021
I think you're spot on about this, @kidroca. I came to a similar conclusion by following the loops and already have a PR for it: #3511
It's a testament to how much faster Onyx is that we're seeing regressions like this 😄 .
kidroca commentedon Jun 10, 2021
Now that I look at it again, we might have a problem with the new
Onyx.set- this was not an existing problem.Before
Onyx.setused to callkeysChangedin the.thenblock of a Promise, even if the write happens instantly promises are resolved on the next tick anddidLoadPersistedRequestswould always be set by that timeNow
Onyx.setis callingkeysChnagedright away, which means that code "stops" at the lineOnyx.set(...)and proceeds after all the sync code in Onyx.set is over, which means afterkeysChangedis calledThis can be a problem because
keysChangedmight have to inform a lot of connections and the UI thread will be blocked during this time - this can be related to the short freezes we get when a chat is selectedInstead we should probably update
Onyx.setto schedule the call tokeysChangedfor the next tick by doing something likesetImmediate(() => keysChanged(key, value))orPromise.resolve().then(() => keysChanged(key, value)))This would still feel instant to the user, but would not cause problems like the above
cc @marcaaron
TomatoToaster commentedon Jun 10, 2021
I'm thinking it would actually be better to avoid situations like the above by explicitly avoiding extra loops. We have a weird scenario here (or is this actually common in Expesnsify.cash?) where we're calling the
Onyx.setwithin theOnyx.connect's callback that's causing the infinite loop. Even ifkeysChangedhappened in a promise, it might get called an extra time more than it's needed right?I think the right solution here is to quit early for empty
persistedRequests. Adding the promise there is equivalent to just swapping the order ofdidLoadPersistedRequests = trueandOnyx.set(ONYXKEYS.NETWORK_REQUEST_QUEUE, []). All of these will solve the problem here, but the latter two solutions runs the loop an extra time for no reason.I think wrapping
keysChangedin a promise inOnyx.set()is not a great idea because then we might introduce more race conditions in our code. Code where we're expecting it to go one extra tick feels like it would be harder to maintain.kidroca commentedon Jun 10, 2021
Yes, the fix is good it should stay whatever is decided
So far everything worked without expectation of how long it would take for
setto finally callkeysChagedand I think it should stay that way - components don't subscribe tosetormergethey subscribe to receive updates at some pointMy concern is only that now
setis blocking whatever happens on screen as it has a lot of synchronous code - I might be wrong, but we can investigate if this is the reason (well one of them) for the little hiccup when you select a chatE.g. if you have a button component that calls
Onyx.setin it'spresshandler it might block the UI for a bit ifkeysChangedhave to iterate a lot of subscriberskidroca commentedon Jun 10, 2021
Another options would be to use the InteractionsManager and run the
keysChangedafter interactions are complete - this way hopefully are not blocking UImarcaaron commentedon Jun 10, 2021
Just catching up here, but nice detective work everyone! I hear where you are coming from @TomatoToaster, but I think in general we should first try to preserve the original behavior we had when it's something low level like this and could potentially affect many things.
Since the entire app has been built on the assumption that subscribers are notified async I think we should maybe continue this way for now and move the
keysChanged()call to another tick like @kidroca is suggesting.marcaaron commentedon Jun 10, 2021
There's no real problem with the original code - if we can assume things are asynchronous. The order doesn't make any difference in that case. But maybe it's a little confusing if you're not familiar with how Onyx will work.
Also, separately, I think there is maybe a strong case to discuss a future where Onyx isn't an asynchronous system now that we have the local caching stuff. But probably requires a larger conversation.
kidroca commentedon Jun 10, 2021
I think the code should still be updated, since
persistedRequestsis an array this check should account for it being emptyhttps://github.com/Expensify/Expensify.cash/blob/533a0230986b3724397e1152f7ce70a2edb2569a/src/libs/Network.js#L35-L37
Also why not disconnect from Onyx and release this connection - nothing would ever set
didLoadPersistedRequeststofalsemarcaaron commentedon Jun 10, 2021
sure, it could be improved (like many things) - but those issues are not causing an infinite loop 🙃