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

Mechanism to cancel reading from standard input #46

Closed
alexrp opened this issue Dec 16, 2021 · 6 comments
Closed

Mechanism to cancel reading from standard input #46

alexrp opened this issue Dec 16, 2021 · 6 comments
Labels
area: drivers Issues related to the terminal drivers. area: io Issues related to core terminal I/O.
Milestone

Comments

@alexrp
Copy link
Member

alexrp commented Dec 16, 2021

For input processing we will need to constantly be reading standard in. But we also need a way to cancel that reading so the input thread doesn't just sit there forever.

Investigate how we can do this on each supported platform. Might be worth checking what, if anything, Notcurses does.

@alexrp alexrp added type: feature state: deliberation Issues that require design work and/or discussion. area: drivers Issues related to the terminal drivers. area: io Issues related to core terminal I/O. labels Dec 16, 2021
@alexrp alexrp self-assigned this Dec 16, 2021
@alexrp
Copy link
Member Author

alexrp commented Dec 16, 2021

Setting a flag and writing some garbage with ioctl(TIOCSTI) could potentially be an option. The equivalent on Windows should simply be WriteConsole on the input handle (probably, maybe). Or WriteConsoleInput.

@alexrp
Copy link
Member Author

alexrp commented Dec 16, 2021

Setting a flag and writing some garbage with ioctl(TIOCSTI) could potentially be an option.

Then again, setting a flag and sending a signal should work just as well and seems less hacky. No such mechanism on Windows, though. Would also require knowing the thread ID of the reading thread.

@alexrp
Copy link
Member Author

alexrp commented Dec 16, 2021

Here's an approach I think is workable and way less hacky.

TerminalReader.Read (and APIs that use it) should be changed to accept an optional CancellationToken.

We will create a pipe on startup. Instead of trying to read immediately, we will poll on the input FD and pipe read FD. When poll returns, we check if the pipe had data written. If yes, we drain it and throw an OperationCanceledException (leaving any potential data in the input FD unread). If no, proceed to read input as normal.

The TerminalReader will hook into the cancellation token with UnsafeRegister. The callback will simply write a byte to the pipe write FD to indicate that cancellation is requested.

This scheme should in principle work the same on Windows - just replace pipe with CreatePipe, poll with WaitForMultipleObjects, read with ReadConsole/ReadFile, etc.

@alexrp alexrp added state: approved Enhancements and tasks that have been approved. and removed state: deliberation Issues that require design work and/or discussion. labels Dec 16, 2021
@alexrp
Copy link
Member Author

alexrp commented Dec 16, 2021

Bad news: WaitForMultipleObjects doesn't work on pipes. So the Unix approach won't translate neatly to Windows.

alexrp added a commit that referenced this issue Dec 16, 2021
@alexrp
Copy link
Member Author

alexrp commented Dec 16, 2021

Mostly done in fe53706 (with a kinda gross but functional workaround for Windows).

Still need to see if cancellation can somehow be plumbed through for ReadLine in addition to ReadRaw.

@alexrp
Copy link
Member Author

alexrp commented Dec 16, 2021

Still need to see if cancellation can somehow be plumbed through for ReadLine in addition to ReadRaw.

It will probably be possible in .NET 7; see:

Closing this.

@alexrp alexrp closed this as completed Dec 16, 2021
@alexrp alexrp added state: resolved and removed state: approved Enhancements and tasks that have been approved. labels Dec 16, 2021
@alexrp alexrp changed the title Mechanism to cancel reading from standard in Mechanism to cancel reading from standard input Dec 17, 2021
@alexrp alexrp added this to the v1.0 milestone Dec 20, 2021
@alexrp alexrp removed their assignment Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: drivers Issues related to the terminal drivers. area: io Issues related to core terminal I/O.
Development

No branches or pull requests

1 participant