-
Notifications
You must be signed in to change notification settings - Fork 335
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
Multithreading issue when setting a key/value in ApplicationData.Current.LocalSettings #1909
Comments
Protects how? Blocking threads with native mutex/etc can intersect (badly) with COM reentrancy. Does this use CoWaitForMultipleObjects or equivalent COM reentrancy savvy APIs? Could you share this semaphoring singleton code? |
@DrusTheAxe Thanks for the reply. The singleton and the code that is using the semaphore looks as follows:
The app is written in c# and uses the What I have seen is that when I remove the |
How's the thread initialized for COM? STA and COM reentrancy tend to mix poorly.
UI threads tend to be STA. It's unclear how UpdateTaskBackupResult is called, thus hard to say more.
I'd be wary making that assumption. Per the docs
It doesn't say how it's implemented (other than not using a kernel Semaphore object) but it sounds similar to SRWLocks i.e. some form of CAS 'lockless' implementation. Certainly no hint how it interacts (if at all) with COM, STA and/or a message pump. You're using the semaphore as a mutex ( @brialmsft @BenJKuhn @Scottj1s @jonwis do you know current best practices for this scenario? |
@DrusTheAxe Thanks for the reply. I have been investigating this issue a bit more and changed the shared access protection to a Mutex, but it still shows the same marshalling exception.
I am pretty sure that there has to be an issue with the way a composite value is written to local settings in the latest v1 Windows App SDK.
I must also say that the same code is being in use for already 2 years on an existing UWP/win32 desktop bridge app in the Windows Store (SyncFolder) without causing any problems. |
@DrusTheAxe
The "marshalling" exception occurs at step 1. For me, this looks like it is simply not allowed to write to local settings from within a background thread when closely before the thread is started there is a write from within the UI thread. Additional observation after given a 2nd look to this:
The error occurs both with simple string values and with composite data values. This seems to indicate that LocalSettings cannot be used in a WinUI Desktop app that is using the Windows App SDK. Not sure whether the "marshalling" exception is related to this, but I am not sure now whether LocalSettings can be used at all. |
@DrusTheAxe The 'appcontainer' error/warning however is still there:
This probably needs to be the subject of a new issue in order to close this one. |
I suspect that debugger message is a false one. What version of Windows are you running on?
@alexlamtest is this a known issue? |
@alexlamtest |
Describe the bug
I am porting a UWP/win32 desktop bridge app to WinUI 3 Desktop (MSIX packaged, WinUI 3 v1.0 stable) and are getting the following exception when assigning a composite value to ApplicationData.Current.LocalSettings from within a background task.
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
The background task that writes the composite value does this by calling a singleton method
AppSettings.Instance.UpdateTaskBackupResult(result)
, which internally uses a semaphore to protect against concurrent access. The part of this method that writes to local settings looks as follows:Immediately before creating the background task the UI thread also writes to LocalSettings, like so:
I know that writing to local settings is somehow (don't know the details) virtualized in Windows but I am not aware of any multi-threading issues.
Did something change with Windows App SDK 1.0?
I am not sure but just recently are using 'v1.0 stable', instead of 'v1.0 preview 3' and have not seen this happening before.
Steps to reproduce the bug
Create WinUI 3 Desktop app.
Write to local settings from within a UI trigger.
Within the same trigger start a background task and write to local settings there as well.
Expected behavior
No response
Screenshots
No response
NuGet package version
1.0.0
Packaging type
Packaged (MSIX)
Windows version
Windows 10 version 21H1 (19043, May 2021 Update)
IDE
Visual Studio 2019
Additional context
No response
The text was updated successfully, but these errors were encountered: