cathode icon indicating copy to clipboard operation
cathode copied to clipboard

Support cooked input cancellation on Windows

Open alexrp opened this issue 3 years ago • 8 comments

See discussion on #46 and #61, as well as commit adf2a455ea0dbc35f48d3204179324a7e37c0d82.

alexrp avatar Dec 30 '21 12:12 alexrp

Started a discussion: microsoft/terminal#12143

alexrp avatar Jan 16 '22 03:01 alexrp

Proposed a whacky but potentially workable(?) hack for this here: https://github.com/microsoft/terminal/issues/12143#issuecomment-1873533831

Thoughts on that approach very welcome. (cc @scottbilas @xoofx)

alexrp avatar Jan 04 '24 11:01 alexrp

I'm always in favor of an approach that works, even with caveats, over endless waiting for the gears to turn in a big software company. :) Seems like it's hard to get their attention on this.

scottbilas avatar Jan 04 '24 22:01 scottbilas

One aspect to consider is how this would work when standard I/O handles are redirected. Obviously, if we close redirected handles, we can't just simply reopen them again like we can with the console. We would need to special-case files, pipes, etc.

🤔

Looking at the .NET BCL, there's a whole bunch of code to handle cancellation in the FileStream backend code, both for the synchronous and asynchronous cases. So I'm now thinking this should be tied into #76 so we can make use of all the infrastructure that the .NET BCL already has in place for handling synchronous/asynchronous cancellation for those cases. Due to the Unix issue mentioned there, we could just do this work in the Windows terminal driver only, for now. (That's fine anyway, as the Unix terminal driver is quite clean in general, and UnixCancellationPipe has served us pretty well.)

alexrp avatar Jan 05 '24 08:01 alexrp

Sigh. Doesn't seem like that hack will work either.

  • Reading from GetStdHandle(STD_INPUT_HANDLE) and then closing that handle from another thread doesn't cause the read to get interrupted for some reason.
  • Reading from CreateFile("CONIN$", ...) and then closing that handle from another thread just causes CloseHandle() to block until the read completes.

I guess this might have worked on Windows 7, but Windows 8.1 completely changed how console handles work, so I figure that's why I had no luck here.

Sadness all around.

alexrp avatar Jan 08 '24 00:01 alexrp

Need to see if NtClose() will work, since CloseHandle() (apparently?) special-cases console handles.

alexrp avatar Jan 08 '24 14:01 alexrp

Ok, good news: It seems to work. (See the discussion on https://github.com/microsoft/terminal/issues/12143 for context.)

If you can, please either clone the Git repo and try the cancellation sample, or consume the GitHub packages and see if it works that way. If there are no issues with it, I'll make a release soon-ish.

alexrp avatar Jan 17 '24 07:01 alexrp

Going to reopen because of this: https://github.com/microsoft/terminal/issues/12143#issuecomment-1895629003

Still, we support cancellation of raw input now, so that's a lot better than nothing. This issue will track cancellation support for cooked input now.

alexrp avatar Jan 17 '24 11:01 alexrp