Repository navigation
Give the pending read up when a run ends, so the next key is not swallowed [patch] - #206
Merged
Merged
Conversation
…lowed [patch] RunAsync abandoned its in-flight read when Shutdown(), Ctrl+C or the caller's token ended the loop, and SpectreConsoleProvider's read was an uncancellable Console.ReadKey on a pool thread. That orphaned read took the next key meant for the host, or raced a second RunAsync for every key. - IConsoleProvider gains ReadInputAsync(CancellationToken), defaulting to the parameterless read so existing providers keep working - SpectreConsoleProvider implements it by polling Console.KeyAvailable and reading only once a key is waiting, so cancelling it leaves no thread blocked in ReadKey - The input loop cancels its read however it ends and gives it a moment to finish. A read its provider would not give up is carried into the next run instead of being raced by a second one Fixes #149 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016rdMXULeUT13t6FCwNfocx
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016rdMXULeUT13t6FCwNfocx
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #149
What changed
IConsoleProvider.ReadInputAsync(CancellationToken): a new overload with a default implementation that calls the parameterless read. Existing providers compile and behave as before.SpectreConsoleProviderimplements the overload by pollingConsole.KeyAvailableevery 15 ms and callingReadKeyonly once a key is waiting. Cancelling the read therefore leaves no thread blocked inReadKey. With redirected stdin,KeyAvailablethrows the sameInvalidOperationExceptionas before, so the bounded-failure handling from End the run after repeated failed reads instead of spinning forever #177 still applies.UIApplication.ProcessInputAsyncreads with a token that is cancelled however the loop ends (Shutdown(), Ctrl+C, the caller's token, or an exception). It then waits up to 250 ms for the cancelled read to finish, so a cancellable provider has stopped reading beforeRunAsyncreturns. If a provider ignores the token, its unfinished read is carried into the next run rather than raced by a second read. A carried read that was cancelled is discarded and replaced.InteractiveDemo: acrefto the method is nowReadInputAsync(), because it became ambiguous (CS0419).The loop body is re-indented to sit inside the new
try/finally. Apart from the read call and the cancelled-carry-over check, its logic is unchanged.git diff -wshows the real change.Tests
UIApplicationPendingReadTestsuses a fake provider whose keys go to the earliest waiting read, as withConsole.ReadKey:AShutdownLeavesNoReadOutstanding: no read is still waiting after aShutdown()-ended run.AKeyPressedInASecondRunReachesTheRoot: runs once with a provider that honours cancellation and once with one that ignores it. This is the issue's reproduction.SpectreConsoleProviderTests: a cancellable read waits for an available key and then reads it, and cancelling a waiting read ends it without callingReadKey.CancellationToken.None), all threeUIApplicationPendingReadTestscases fail, and they pass with the fix. The full solution test run passes (261/261).🤖 Generated with Claude Code
https://claude.ai/code/session_016rdMXULeUT13t6FCwNfocx
Generated by Claude Code