question

robert avatar image
robert asked

PlayFab C# SDK / Thread safe

We get several C# Concurrent Exceptions from Azure when using the C# SDK.

Looks like this can happen when two functions try to perform the first PlayFab call at the very same time when a new instance is starting up. As C# Dictionary is not thread safe by itself, are we supposed to lock functions for single threading ourself, or is this just an internal error and PlayFab SDK should lock the dictionary here?


   private IPlayFabPlugin GetPluginInternal(PluginContract contract, string instanceName)
        {
            var key = new Tuple<PluginContract, string>(contract, instanceName);
            IPlayFabPlugin plugin;
            if (!this.plugins.TryGetValue(key, out plugin)) <-- Exception

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
robert avatar image
robert answered

Short addition: We use a single PlayFabServerInstanceAPI, shared by all functions for performance and memory reasons. If we would use a new PlayFabServerInstanceAPI for each function this exception will of course not be thrown, but if possible we definitely would like to stick to using a shared PlayFabServerInstanceAPI

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image Seth Du ♦ commented ·

May I ask is this issue from Azure Function? Would you share more details about your project?

0 Likes 0 ·
robert avatar image robert Seth Du ♦ commented ·

TitleID: 9FC07

Yes, its from azure. We temporary made a lock() around adding and getting the plugin from the list which for now solves the concurrency exception.

Our primary question at the moment is, is PlayFabServerInstanceAPI not be designed to be used by multiple threads concurrently?

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ robert commented ·

As I can see from other threads in the community, developers tend to use independent PlayFabServerInstanceAPI for each function because it is more convenient. It is reasonable to have a shared Instance API due to the resource saving purpose.

It is correct that there should be no written codes to enable its thread-safe. If you have a requirement of it, you may try to implement a singleton with lazy initialization.

Please feel free to tell us if there are any further question and you may also consult Azure support team about this issue.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.