1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-07 13:48:00 -05:00

Improve cancellation token handling in Utilities.cs (#331)

A using statement was added to ensure proper disposal of the CancellationTokenSource. Additional error handling was also included to catch an OperationCanceledException and prevent it from causing unintended side effects. The client response is now properly disposed in the finally block.
This commit is contained in:
Amos 2024-07-07 01:30:40 +01:00 committed by GitHub
parent 036a467bd0
commit ee0b40d657
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1386,16 +1386,21 @@ namespace SharedLibraryCore
IGameEventSubscriptions.ClientMessaged += OnResponse;
await client.TellAsync([prompt], token);
var tokenSource = new CancellationTokenSource(DefaultCommandTimeout);
using var tokenSource = new CancellationTokenSource(DefaultCommandTimeout);
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(tokenSource.Token, token);
clientResponse.Wait(linkedTokenSource.Token);
return response;
}
catch (OperationCanceledException)
{
return null;
}
finally
{
IGameEventSubscriptions.ClientMessaged -= OnResponse;
clientResponse.Dispose();
}
async Task OnResponse(ClientMessageEvent messageEvent, CancellationToken cancellationToken)
@ -1409,6 +1414,7 @@ namespace SharedLibraryCore
if (await validator(response))
{
// ReSharper disable once AccessToDisposedClosure
clientResponse.Set();
}
else