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

Try to open stdio in non-blocking mode #8787

Merged
merged 2 commits into from
Feb 12, 2020
Merged

Conversation

waj
Copy link
Member

@waj waj commented Feb 11, 2020

Currently if the stdio descriptors are not a TTY they are re-opened with blocking: true. That makes sense for files, for example if the output is being redirected (i.e: ./foo > somefile). But it doesn't if the input is a pipe (i.e: cat | ./foo).

The problem is that programs like this will block all the fibers:

spawn do
  STDIN.gets
end

We used to have problems with stdio (#2713) but it was fixed by not changing the blocking mode from the original file descriptor (#6518)

This PR adds a third state to the blocking argument of FileDescriptor.new. When a nil is passed (the default) it detects the type of descriptor and sets the blocking mode accordingly.

Unfortunately this is quite difficult to test, so there is no specs to find regressions. I added a spec to check the STDIN opens in different modes depending on the fd type. But please, help me find if I didn't miss anything and I'm actually breaking everything again 😅

@waj waj added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:files labels Feb 11, 2020
Copy link
Contributor

@Sija Sija left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of pedantic suggestions

src/io/file_descriptor.cr Outdated Show resolved Hide resolved
src/io/file_descriptor.cr Outdated Show resolved Hide resolved
spec/std/io/file_descriptor_spec.cr Outdated Show resolved Hide resolved
@waj
Copy link
Member Author

waj commented Feb 11, 2020

Couple of pedantic suggestions

Love all the suggestions. You know the language better than me :)

@waj waj added this to the 0.33.0 milestone Feb 11, 2020
@asterite asterite merged commit 9c30c26 into master Feb 12, 2020
@asterite asterite deleted the fix/non-blocking-stdio branch February 12, 2020 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:files topic:stdlib:runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants