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

Console streams are blocking on Windows #14576

Open
straight-shoota opened this issue May 8, 2024 · 1 comment
Open

Console streams are blocking on Windows #14576

straight-shoota opened this issue May 8, 2024 · 1 comment

Comments

@straight-shoota
Copy link
Member

straight-shoota commented May 8, 2024

This has been mentioned before but I don't think there's a dedicated issue for it.

The standard streams on Windows are blocking. On Unix systems, on the other hand, they are non-blocking for TTYs. It allows to continue other fibers while waiting on IO. This is particularly relevant for STDIN.

For example, the following program prints the current time and updates it every second, while pressing enter aborts.

spawn do
  loop do
    print Time.utc.to_s("%H:%M:%S\r")
    sleep 1.second
  end
end

STDIN.gets

On Windows, it doesn't print anything because STDIN.gets is blocking and the loop fiber never gets a chance to execute.

We should have the same behaviour as on Unix. I'm not sure how we can best achieve it.

  • We're already detecting whether the standard streams are a console. So this information can be used.
  • It might be possible that we can set blocking: false in FileDescriptor.from_stdio on Windows. This certainly requires Support asynchronous file I/O on Windows #14321 and maybe something else. We would probably have to duplicate the file descriptor as we do on Unix, in order to avoid problems with the parent process. But I'm not sure about the Windows mechanics here.
  • The Win32 API also provides special functions for dealing with console handles, such as GetNumberOfConsoleInputEvents.
@HertzDevil
Copy link
Contributor

HertzDevil commented May 10, 2024

IIRC Win32 console handles do not support overlapped I/O, so any kind of asynchronous capability requires threads; the standard input and output streams are not opened with FILE_FLAG_OVERLAPPED, and additional console handles opened using CreateFile do not respect that flag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: To investigate
Development

No branches or pull requests

2 participants