-
Notifications
You must be signed in to change notification settings - Fork 275
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
Make read sync block for windows systems #195
Comments
My suggestions would be to default to length 1, to actually block. |
@MSleepyPanda could you create a toy example? You're probably right, if |
Sure This immediately exits on my machine (println to prevent llvm from optimizing out). fn main() {
let input = crossterm::input();
for _ in input.read_sync() {
println!("Hello World!");
}
} Hacky workaround: fn main() {
let input = crossterm::input();
loop {
for _ in input.read_sync() {
println!("Hello World!");
}
}
} This busy loops (1 core @ 100%) and leaks memory in the console subsytem (conhost.exe) I've confirmed locally that a) patch sections in cargo are a mess and b) defaulting to length 1 fixes this. |
Hey, thanks for the issue, I'll see what I can do here and what valid solutions would be I take your suggestion into account. |
Yea so basicly TUI had the same problem with this and should be fixed. Windows returns directly if there is nothing to read and Linux will wait for input before returning. |
Iirc this can be resolved by solely removing the length check, as it's not even used further down in the code, at least in the master branch. Backporting it to 0.9 might also make sense. I'm on vacation over the next two weeks and thus not able to create a PR in the meantime, but if its still unresolved by then i'll give it a shot. If someone is interested, feel free to take it :) |
Just ran into this issue on the current master branch. I've been digging into it (stymied a bit by the difficulty of debugging Rust on Windows, but I'm doing my best) and wanted to share what I've found. Simply removing the length check and early return in
I haven't checked it out yet but I would speculate that there's something wrong with how the FFI boundary is getting crossed, leading to the appearance of an event where none exists, specifically I thought I'd share my progress thus far in case somebody can take it farther. |
@joshhansen You have to keep in mind that the first example may also early return because some events are not handled So you still have to use a loop, but it shouldn't pin a core to 100% anymore. |
@MSleepyPanda can you checkout out crossterm-rs/crossterm-winapi#2 because it uses a loop as solutions. The .net framework seems to do it like that as well. I am not sure if there are any other ways on windows to block read for input. |
@MSleepyPanda Ah, I see. Is there any reason not to just ignore the unhandled events instead of terminating the iteration? |
@TimonPost I'll have a look on wednesday :) I had a quick look but i think this would still busy loop because you early return and don't give the windows api a chance to block the thread. |
@joshhansen I think ignoring makes sense, but it wasn't straightforward within the current architecture iirc. I'm still on vacation, but i'll be back on wednesday. |
@TimonPost Sorry for the delay, i just submitted my review crossterm-rs/crossterm-winapi#2 (review) |
I'm still getting early exit from read_sync on Windows with 0.12.0. Did this make it into the 0.12.0 release? |
Apparently not, the code in question is here crossterm/src/input/input/windows.rs Line 216 in 2815833
The current behaviour is documented here crossterm/src/input/input/windows.rs Lines 159 to 162 in 2815833
If you loop over |
The documentation of
TerminalInput
states forTerminalInput::read_sync
:On windows this will resolve in call to
ReadConsoleInput
and according to MSDN it blocks.But there is a problematic fast return, causing
ReadConsoleInput
not to be called.https://github.com/TimonPost/crossterm/blob/8223cc9e4a5e35e15a0287bd9d8aa53dc84d1c34/crossterm_winapi/src/console.rs#L170-L173
Also some events return None, even though further events can be received:
https://github.com/TimonPost/crossterm/blob/8223cc9e4a5e35e15a0287bd9d8aa53dc84d1c34/crossterm_input/src/input/windows_input.rs#L230-L233
While this is valid behaviour for iterators, this causes early returns when iterating with
for
orwhile
. This should be either changed or documented.The text was updated successfully, but these errors were encountered: