[Session] Align state and global agent switchpoints #3845
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Extracted from #3728.
There's a few different changes here but they all work together towards the goal of aligning
__globalAgent
assignments withsetState
calls that modifycurrentAgentState
. Once they fully align, we can kill the global.This PR doesn't fully get us all the way there because it leaves some existing timing differences.
I will address them in a follow-up.
Reviewing
These are meant to be safe but slightly riskier than the ones before.
I suggest reading each change in isolation.
97a5304
This moves this
setState
onpersisted.onUpdate
callback to the beginning of the function.The purpose of this
setState
is to update the currentaccounts
to thepersisted.accounts
from another tab. We always need to do it, and it doesn't depend on any other data, so it makes sense to do it early.Note for this change to be safe, we need to make sure that it wasn't clashing with any other
setState
during this callback. Here's how we verify this (links are to the existing code):setState
does not touchaccounts
so it doesn't matter whether it runs before or after — they accumulate regardless of order.initSession
call happens in the middle of the function but it's not being awaited. Therefore, only the code before the firstawait
would run, which transitively has nosetState
calls.Therefore, it is safe to move this
setState
to the beginning of the function.af704f7
If another tab logged us out, that has already been persisted to the storage by that tab. So it doesn't make sense to persist anything in the storage here.
I believe this was only set to
true
due to the previous structure of the code which had helper methods likeclearCurrentAccount
(which always had persistence on). Now that we've gotten rid of helper methods, we can see more clearly what is actually going on, and can adjust it more precisely.5923f3f
This removes a
try/catch
aroundresumeSessionWithFreshAccount
in the "saved session expired duringinitSession
" codepath. The thinking here is similar to #3836 — we can rely ononAgentSessionChange
callback to log us out. In fact, we're already relying on it because clearly updating__globalAgent
is not enough — it does not update the UI. This code was only updating__globalAgent
so it can be removed (updating__globalAgent
on expiry is done by #3838).Another consequence of this change is that
initSession
would now throw if resuming the session failed. Counter-intuitively, I'm removing the logic inside the existingcatch
es in this commit. Since it did not throw in the past, those were superflouous. However, I'm keeping thecatch
es itself so that the calling code's expectations don't change.Let's audit its callsites to verify this:
InnerApp
calls here and here already handle exceptions, no change in behavior.useAccountSwitcher
has aninitSession
call which does change the behavior.catch
because we weren't hitting it in the past either. It's superfluous because both clearing the account and showing the failure toast is already handled byonAgentSessionChange
.ChooseAccountForm
.catch
because it wasn't getting called in the past either. So it's superflouous.Note how by the end of this change, all
__globalAgent
assignments have a matchingsetState
next to them. There's sometimes an async gap between them, which I'll fix in a follow-up PR.Test Plan
Verify syncing between tabs (login / logout / switch) still works.
Verify expiration during resumption is handled (see above and test plan from #3838 for testing scenarios).