Cancel Matchmaking Ticket - Error Handling


On a C# rest server, when a user tries to cancel a matchmaking ticket, I make the following call from the server:
var response = await _gameLiftClient.StopMatchmakingAsync(stopRequest, cancellationToken);

This works most of the time and all is fine. However, if this request is sent at exactly the time the ticket moved to Completed or Placing states, this will throw an InvalidRequestException, saying something like “Matchmaking ticket is in PLACING status and cannot be canceled.”.

I imagine that the desired flow would be that I first call _gameLiftClient.DescribeMatchmaking, and then if its still searching, cancel it, however since in the world of the internet all is async, even if I do that there is no guarantee that when I call “StopMatchmakingAsync” it wont have moved to an invalid state anyway.

So for now what I am doing is parsing the exception Message to check like… “does the exception message have the word “PLACING” in it? what about COMPLETED?” because in those cases, what I will do is basically nothing, it was too late to cancel the ticket, so the user will go into the match anyway.

The reason I am posting here is that this is terrible coding… unmaintainable… if tomorrow you guys decide to change the message of the exception, my server will have the same bug again… so is there any better way to do this that I am not seeing? Or, if not, could you please, from the “StopMatchmakingAsync”, instead of throwing an exception just return a result that allows us to elegantly deal with the result?


Hi Allan,

When StopMatchmaking throws an exception, you could potentially call DescribeMatchmaking to see if its state is PLACING, COMPLETED, TIMED_OUT or FAILED, and then act accordingly. Thoughts on this approach?

Hi James,

Thank you for the reply. Indeed, that could be a way to go about it, but that would mean an extra step in the process that comes with a few concerns. I would need to hold the caught exception (which at this point Im not sure what it is since I am not parsing the message), call DescribeMatchmaking, which now is in Placing, and then parse that result assuming that the exception was indeed about the Placing stage, but, given the delay between the calls, if the exception was about anything else that might throw the same type of exception, I will be discarding a different type of exception assuming it was something different.

Maybe this is not the case and there is no other exception that gets thrown of the same type and therefore a specific catch method would suffice, but, this still sounds like a less than ideal solution.


Would it be helpful if GameLift throws a dedicated exception for this? For instance, UncancellableMatchException?

We cannot easily throw a dedicated exception for this because it could break backwards compatibility for other customers. For instance, some customers may be catching InvalidRequestException, and we cannot use inheritance to solve this problem due to some internal limitations.

I’ll make a comment in the service backend code to ensure that no alternations to the error message will be made.