1

I try to develop an extension for Microsoft Edge based on native messaging and the official guide provides the example. And there is synchronization of access to the dictionaries of AppServiceConnections and their Deferrals in the OnBackgroundActivated method, but there is no such a thing in other event handling methods...

So my question is about UWP App Service threading model. Is it guaranteed that only one event handling method can be performed at a time? Or should I provide a correct synchronization of access to my data?

Is AppServiceConnection thread safe? Can I use SendMessageAsync from different threads at the same time? Or should I synchronize its usage?

1 Answers1

1

I guess your issue is that you didn't see lock keyword inside events like OnAppServiceRequestReceived, OnAppServicesCanceled and so on, which is to do thread synchronization, and you're not sure if you should do this by yourself.

I think the answer should be no.lock inside OnBackgroundActivated is ensured to set correct desktopBridgeConnectionIndex or connectionIndex. Without the keyword lock inside these event handles not means that the event handle must be triggered only one time at a time. For one app service, if client A is connecting the app service, at the same time, another client B asks for the same app service, for this scenario the app service will spin up another instance of the same background task. So that for client A, its app service connection there is no side effect on client App B. In another words, each app service connection has its own instance, messages sending based on one app service connection have no influence with others. You may reference this video to look more details about app service, app service relative is about starting from 25th minute.

If you check the code snippet inside the event, you may see there are code lines to judge the request is from which app service connection, for example this.desktopBridgeConnection = desktopBridgeConnections[this.currentConnectionIndex].You will send message to correct AppServiceConnection, and this should be thread safe. If you met actual thread save issue when performing this, you could ask issue with testing details.

Sunteen Wu
  • 10,509
  • 1
  • 10
  • 21
  • You correctly understood my question. However when I run this extension I see that there is only one instance of `NativeMessagingHostInProcess` for all Edge tabs, all of which creates a connection to this host. I have asked a similar question to Stefan Wick in [his blog](https://stefanwick.com/2018/04/16/uwp-with-desktop-extension-part-3/comment-page-1/#comment-102) and he confirmed that the event handlers may fire on different threads. Thus there is a data race: we can read data from `desktopBridgeConnections` at the time when we modify it in `OnBackgroundActivated`. Am I right? – Alexey Sidorov May 01 '18 at 12:54
  • "he confirmed that the event handlers may fire on different threads." For this, just as I mentioned in my reply,you could have many clients but one server host, and they work in different threads. But I'm not sure about your last issue. Where and when you read data? What I can confirm is for different connects, they 're separated, you could read the connection A at the same time read connection B. @AlexeySidorov – Sunteen Wu May 09 '18 at 09:31
  • Probably I can not explain my question correctly... Well, in this sample there is a `Dictionary`, we add new elements in `onBackgroundActivated`, read elements it in `OnAppServiceRequestReceived` and remove elements in `OnAppServicesCanceled` and `AppServiceConnection_ServiceClosed`. And these methods can be executed simultaneously and [Dictionary](https://msdn.microsoft.com/en-us/library/xfhwa508.aspx) is not a thread safe class. So my question is why there is no synchronization of access to the `desktopBridgeConnections` dictionary? – Alexey Sidorov May 10 '18 at 04:54