I am having trouble in passing the CancellationToken to the CloudCode. CloudCode api methods accept System.Threading.CancellationToken as a parameter. I have added it as a parameter to my CloudCode function but I can’t pass it from the client side as the generated bindings generate Unity.Services.CloudCode.GeneratedBindings.System.Threading.CancellationToken class which has type mismatch with the System.Threading.CancealltionToken one. I have the following questions:
- Am I even supposed to pass the
CancellationToken from the client code and if so, how?
- If I am not supposed to pass the token from the client then how do I use it inside of the CloudCode?
My idea is that if the async fucntion stops on the client it should also terminate on the CloudCode.
e.g. If the player can’t find an empty lobby to quick join after N second then both client and network functions should terminate.
Could you provide a code example?
Because I see no reason why you couldn’t just use the cancellation token sent to you by cloud code. If the client code cancels, you set cloud code’s cancellation token to cancelled. You just need to manage two tokens - the client’s and the cloud’s.
By “the player can’t find” you mean the client decides to cancel the “search for match”? Because the client isn’t actively scanning for lobbies, that also happens in the cloud (Lobby or Cloud Code).
This example explains my current code setup:
i. The code is abstracted and simplified to show the setup and try/catch block are intentionally left out to reduce boilerplate code in examples.
- Cloud function that does something through UGS
[CloudCodeFunction("DoSomethingInCloud")]
public async Task<Response> DoSomethingInCloud(
IExecutionContext context,
System.Threading.CancellationToken token
)
{
return await m_GameApiClient.SomeService.ServiceMethod(
executionContext: context,
accessToken: context.AccessToken,
projectId: context.ProjectId,
playerId: context.PlayerId,
cancellationToken: cancellationToken,
};
- Upon generating bindings the engine generates the following class in the bindings
namespace Unity.Services.CloudCode.GeneratedBindings.System.Threading
{
public partial class CancellationToken
{
public bool IsCancellationRequested;
public bool CanBeCanceled;
public WaitHandle WaitHandle;
[Preserve]
public CancellationToken ()
{
}
}
}
- When I try to call the CloudCode function in project code with
System.Threading.CancellationToken I get a type mismatch because function now expects the one from generated bindings Unity.Services.CloudCode.GeneratedBindings.System.Threading.CancellationToken.
e.g.
CancellationToken token = this.GetCancellationTokenOnDestroy(); // I am using UniTask
m_Module.DoSomethingInCloud(token);
The code produces the following static semantic error:
Argument type ‘System.Threading.CancellationToken’ is not assignable to parameter type ‘Unity.Services.CloudCode.GeneratedBindings.System.Threading.CancellationToken’
If this is the wrong approach, which is likely based on your response, how do I access and manage cancellation token sent by cloud code?
The provided use case example in the initial post was more of a behaviour example than a concrete implementation. The desired behaviour (if possible) is this:
- Client code starts async call to the cloud code
- Cloud code starts its own async call to the UGS (or any async call)
- Client code cancels the async call for whatever reason
- Cloud code also cancels its own execution
Thank you for your reply.
Try using that type specifically:
‘Unity.Services.CloudCode.GeneratedBindings.System.Threading.CancellationToken’
Does this work?
var token = new Unity.Services.CloudCode.GeneratedBindings.System.Threading.CancellationToken();
m_Module.DoSomethingInCloud(token);
Of course you keep the token in a field and cancel it when the time is due. Don’t rely on Unitask because the “CancelOnDestroy” is a safe fallback, not something I’d use as a reliable and regular code path. Specifically this: GetCancellationTokenOnDestroy();
This indicates that you cancel the operation only when the object gets destroyed. I find this is bad practice especially with async code since any script on that object is also getting destroyed with no way for you to control or prevent or await this. Once an object runs OnDestroy, there is no awaiting - the object will be destroyed at the end of the frame regardless of whether any async operation is still in progress.
Instead of destroying that object when the user cancels, you should instead invoke an event that subscribers listen to, perform their async tasks respectively cancel them, and make all of that awaitable. Only after all of the associated scripts have done their jobs should their objects get destroyed. This leaves you with all opportunities to influence that process, perhaps even cancel the cancellation, and definitely having an easier time debugging because all the associated objects and scripts are still live and functional.
var token = new Unity.Services.CloudCode.GeneratedBindings.System.Threading.CancellationToken();
m_Module.DoSomethingInCloud(token);
Resolves the type mismatch but how do I now cancel the operation using the token?
How did you do it before? Should work the same way. I think you need a cancellation source. So perhaps the new CancellationToken() is oversimplified and it should rather be created via the source? I haven’t worked with cancellation tokens so far.
Hello all!
At this time (package version 2.10 for posterity), Cloud Code doesn’t handle the Cancellation Token.
It’s inclusion in the generated bindings signature is a mistake, and one that will hopefully be corrected in a future version of the package.
At present, any cancellation tokens you might try to use won’t have any impact on a Cloud Code script or module’s execution once it begins.
Hope that clears things up, and thanks very much for the report!