Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cancellation token handling in Utilities.cs #331

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion SharedLibraryCore/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,16 +1386,21 @@ public static async Task<string> PromptClientInput(this EFClient client, string
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)
Expand All @@ -1409,6 +1414,7 @@ async Task OnResponse(ClientMessageEvent messageEvent, CancellationToken cancell

if (await validator(response))
{
// ReSharper disable once AccessToDisposedClosure
clientResponse.Set();
}
else
Expand Down
Loading