Skip to content

Multithreading issue when setting a key/value in ApplicationData.Current.LocalSettings #1909

Description

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:

ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
composite["TaskId"] = result.TaskId;
composite["LastStatus"] = Convert.ToInt32(result.LastStatus);
.. some other 20 keys are assigned
// Serialize subtask results to JSON
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(List<SubTaskBackupResult>));
using (MemoryStream ms = new MemoryStream())
{
   js.WriteObject(ms, result.SubTaskResults);
   ms.Position = 0;
   using (StreamReader sr = new StreamReader(ms))
   {
      string json = sr.ReadToEnd();
      composite["SubTaskResults"] = json;
      sr.Close();
      ms.Close();
   }
}
ApplicationData.Current.LocalSettings.Values[key] = composite;

Immediately before creating the background task the UI thread also writes to LocalSettings, like so:

.. some code
AppSettings.Instance.UpdateTaskBackupResult(result);
.. some code
var t = Task.Run(async () =>
{
   // Tell ExecTasksRequestHandler to run
   await _execTasksHandler.RunAsync(taskRequests, _isBackgroundTaskRequest, isFirstSchedularRun, _execTasksCancelTokenSource.Token);

   // Finished
   IsBackupRunning = false;
});

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions