I have a controller action which must run some tasks that don’t need to be awaited to return a response which is why I’m firing-and-forgetting those functions. However these functions are deadlocking hence never completing.
public async Task<IActionResult> AddPlayerToQueue(AddPlayerToQueueRequest request)
{
//...
if (request.MatchPrivacy == MatchPrivacy.Public)
{
// If the queue is full and there is another one which is compatible ready, tell the game to start the match
_ = EvaluateMatchStart(request.Player.UserId); // Fire-and-forget. Don't need to await this function because it's not relevant to the action's scope
}
return Ok();
}
private async Task EvaluateMatchStart(long userId)
{
//...
await gameService.SendStartMatchMessageAsync([readyQueue, opponent]).ConfigureAwait(false); // Deadlocks despite using ConfigureAwait(false)
}
This is the code of a typed HttpClient
public class GameService(HttpClient httpClient, IConfiguration configuration) : IDisposable
{
public async Task SendStartMatchMessageAsync(Queue[] queues)
{
//...
// Send message
HttpResponseMessage res = await httpClient.PostAsJsonAsync($"uri", new
{
message = JsonSerializer.Serialize(new
{
Teams = msgQueues
})
}); // Deadlocks
res.EnsureSuccessStatusCode();
}
public void Dispose() => httpClient?.Dispose();
}