question

Wolfgang Steiner avatar image
Wolfgang Steiner asked

C++ client API shutdown: wait for HTTP threads (avoid callback crashes)

Hi,

we are successfully using the C++ PlayFab client. But we found one problem with the client API.

Our software is embedded into a 3rd party software via runtime plugin mechanisms. Therefore our code has to be able to be completely started & shutdown during the host application's runtime.

This also applies to our PlayFab C++ client API code.

We are using the threaded API mode to avoid stalling the host application, i.e.:

PlayFabSettings::threadedCallbacks = true;

This means the callbacks when calling the PlayFab API will be handled by some PlayFab internal thread (Transport Plugin Thread)

The problem now arises if the host application requests our plugin to shutdown and some PlayFab API requests are still in flight, i.e. being processed by the PlayFab background thread.

We have found no documentation or C++ example code that shows how to wait for the HTTP Transport thread to finish all outstanding API calls before exiting.

Or in other words, if we don't have a way to join our apps' "main" thread with the PlayFab HTTP transport thread, our app will just continue to shutdown and release all memory / class instances.

And after that the PlayFab HTTP thread will still run and eventually try to call some callbacks into our code, which by this time is fully deallocated by this point which will lead to all kinds of crashes obviously.

We found one trick / hack by reading through the PlayFab C++ sources to avoid any callbacks being called after our app was destroyed:

PlayFabPluginManager::SetPlugin((std::shared_ptr<IPlayFabPlugin>)nullptr, PlayFabPluginContract::PlayFab_Transport);

This guarantees that no PlayFab callbacks will be triggered after this line, and lets us safely exit our application code.

This works because the PlayFab HTTP plugin will join the underlying thread (that would also call the callbacks into our code) in its destructor (see https://github.com/PlayFab/XPlatCppSdk/blob/master/code/source/playfab/PlayFabIXHR2HttpPlugin.cpp#L18-L34)

By looking at this code however, you can see that the thread will not join after all outstanding requests where handled, but immediately after the current request from the queue was processed.

This means that some events that were queued to be written by our application would be lost / not written if the happen to be emitted right before our application has to be shut down!

So while avoiding the callback crash problem, this is far from ideal for us.

Please let me know if I missed some API that would let us safely shutdown the PlayFab API while also not losing any API calls that were made.

Thanks

apis
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

·
Citrus Yan avatar image
Citrus Yan answered

We didn’t receive similar requests like yours before therefore your use case is not supported by the current C++ SDK. Our C++ SDK has a complete thread management mechanism, and if you want to take control of the thread-execution process, you may need to implement it by modifying the source code on your own, or make a feature request about it here : https://community.playfab.com/spaces/24/index.html

10 |1200

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

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.