Skip to content
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

Fix Ensure the appState is loaded only once and GetAppState always returns #3193

Merged
merged 3 commits into from
Apr 11, 2022

Conversation

tomasfil
Copy link
Contributor

fixes #3192

/// <param name="driver">The suspension driver.</param>
/// <returns>A completed observable.</returns>
private static IObservable<Unit> EnsureLoadAppState(this ISuspensionHost item, ISuspensionDriver? driver = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can possibly use the Interlocked.Exchange(ref initFunction, null)?.Invoke() then store a Action or Func with the init logic. This is thread safe as well but avoids the lock

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, it took me a while, I have never used Interlocked, but I understand and this is clever. Commit will be coming right up

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you got the idea correctly.

.Cast<T>();
if (item.AppState is null)
{
item.EnsureLoadAppState();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a threading issue. If you use the interlock approach and always call the method could avoid that

@glennawatson glennawatson changed the title Ensure the appState is loaded only once and GetAppState always returns Fix Ensure the appState is loaded only once and GetAppState always returns Feb 23, 2022
Using Interlocked.Exchange to prevent dead lock and Threading issues.
@glennawatson glennawatson enabled auto-merge (squash) February 23, 2022 15:05
@glennawatson
Copy link
Contributor

I've got to sleep but thanks for this

@glennawatson
Copy link
Contributor

Looks like Maui has changed so I'll have to fix those build errors. They are unrelated to your stuff

@tomasfil
Copy link
Contributor Author

Figured that out, but thanks for letting me know anyway

@anaisbetts
Copy link
Member

I'm a bit confused, loading the AppState may occur more than once during the lifecycle of an app, if an app is suspended then resumed on mobile platforms - why would we want to only load it once?

@tomasfil
Copy link
Contributor Author

I have never developed on mobile platforms, but the state shouldn't be expected to change in storage while suspended no? So the initially loaded one should be still actual.

If I am wrong, then this needs to be done otherwise.

@glennawatson glennawatson merged commit 65105e5 into reactiveui:main Apr 11, 2022
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 26, 2022
@tomasfil tomasfil deleted the SuspensionInitFix branch October 5, 2022 12:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] RxApp.SuspensionHost.GetAppState sometimes returns null due to concurrency issues
3 participants