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

stream: autodetect direction #1964

Closed
wants to merge 4 commits into from

Conversation

vtjnash
Copy link
Member

@vtjnash vtjnash commented Aug 28, 2018

Previously, we required the user to specify the expected read/write flags for a pipe or tty. But we've already been asking the OS to tell us what they actually are (posix standard fcntl F_GETFL), so we can hopefully just use that information directly.

Fix #1936

@addaleax
Copy link
Contributor

@refack
Copy link
Contributor

refack commented Aug 28, 2018

Could you devise a test for this?

@santigimeno
Copy link
Member

Changes LGTM. A couple of questions / comments:

  • We'll need updating the documentation.
  • What's the semver-ness of this change? I mean, what if the caller of uv_tty_init() passes readable = 1 (now unused) on a O_WRONLY fd. The expectation until now would be having a readable stream. Should we do something about it?
  • When you say in the comment But we've already been asking the OS to tell us what they actually are, when we were doing that?

@vtjnash
Copy link
Member Author

vtjnash commented Aug 30, 2018

When you say in the comment But we've already been asking the OS to tell us what they actually are, when we were doing that?

The non-blocking state (O_NONBLOCK) is part of the same flags, so libuv is already dependent on this operation succeeding per POSIX.1-2001. I had been somewhat concerned that some system would not not define or report these flags correctly, but from the CI reports above, that does not appear to be the case.

What's the semver-ness of this change? I mean, what if the caller of uv_tty_init() passes readable = 1 (now unused) on a O_WRONLY fd. The expectation until now would be having a readable stream. Should we do something about it?

That's an interesting question. That may be the expectation, but realistically, any actual attempt to read from it would have been blocked by the kernel. I think this could have the potential to change where and how that error would get reported (since we'll now know up-front that it'll fail). So I think this doesn't affect semver?

@vtjnash
Copy link
Member Author

vtjnash commented Aug 31, 2018

Added some tests for this and updated docs to remove reference to this (now unused) parameter.

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

Mostly LGTM. Should probably run the CI and Node-CI again, given the updates and the amount of red the last time around.


Set ``readable`` to true if you plan to use ``uv_read_start()`` on the stream.
The ``unused`` parameter is now auto-detected.
Copy link
Contributor

Choose a reason for hiding this comment

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

This sentence doesn't really make sense without some context on what unused used to do.

@@ -370,9 +370,9 @@ terminal information.
The first thing to do is to initialize a ``uv_tty_t`` with the file descriptor
it reads/writes from. This is achieved with::

int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable)
int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int unused)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this change calls for a .. versionchanged:: entry.

@@ -382,12 +386,6 @@ int uv_tty_set_mode(uv_tty_t* tty, uv_tty_mode_t mode) {
}


int uv_is_tty(uv_file file) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be a separate commit.

Not used anywhere or exported. Most of this code also cares which direction handle is open too (Input or Output), so it's not particularly useful.
Previously, we required the user to specify the expected read/write flags
for a pipe or tty. But we've already been asking the OS to tell us
what they actually are (fcntl F_GETFL), so we can hopefully just use that
information directly.

Fix libuv#1936
@vtjnash
Copy link
Member Author

vtjnash commented Sep 5, 2018

Thanks @cjihrig, I made the requested updates.

@vtjnash
Copy link
Member Author

vtjnash commented Sep 12, 2018

bump

Copy link
Member

@santigimeno santigimeno left a comment

Choose a reason for hiding this comment

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

Other than the test failure, LGTM.

@@ -1751,6 +1751,14 @@ TEST_IMPL(spawn_inherit_streams) {
ASSERT(uv_pipe_open(&pipe_stdout_child, fds_stdout[1]) == 0);
ASSERT(uv_pipe_open(&pipe_stdin_parent, fds_stdin[1]) == 0);
ASSERT(uv_pipe_open(&pipe_stdout_parent, fds_stdout[0]) == 0);
ASSERT(uv_is_readable((uv_stream_t*) &pipe_stdin_child));
ASSERT(!uv_is_writable((uv_stream_t*) &pipe_stdin_child));
Copy link
Member

Choose a reason for hiding this comment

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

This test is failing on FreeBSD and SmartOS as on these systems pipe() returns a bidirectional data channel. See: https://www.freebsd.org/cgi/man.cgi?pipe(2) and https://illumos.org/man/2/pipe.
The failure is:

not ok 203 - spawn_inherit_streams
# exit code 134
# Output from process `spawn_inherit_streams`:
# Assertion failed in test/test-spawn.c on line 1755: !uv_is_writable((uv_stream_t*) &pipe_stdin_child)

@vtjnash
Copy link
Member Author

vtjnash commented Sep 18, 2018

That's fascinating! I've updated the test to accept either behavior.

@cjihrig
Copy link
Contributor

cjihrig commented Sep 19, 2018

santigimeno pushed a commit that referenced this pull request Sep 19, 2018
Not used anywhere or exported. Most of this code also cares which
direction handle is open too (Input or Output), so it's not particularly
useful.

PR-URL: #1964
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
santigimeno pushed a commit that referenced this pull request Sep 19, 2018
Previously, we required the user to specify the expected read/write
flags for a pipe or tty. But we've already been asking the OS to tell us
what they actually are (fcntl F_GETFL), so we can hopefully just use
that information directly.

Fixes: #1936
PR-URL: #1964
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
@santigimeno
Copy link
Member

Landed in: 956bf6b and 4049879. Thanks!

@vtjnash vtjnash deleted the jn/detect-readable-flag branch September 19, 2018 16:37
@vtjnash
Copy link
Member Author

vtjnash commented Sep 19, 2018

Yay, Thanks! :)

inkydragon pushed a commit to one-pr/Chinese-uvbook that referenced this pull request Dec 16, 2021
Previously, we required the user to specify the expected read/write
flags for a pipe or tty. But we've already been asking the OS to tell us
what they actually are (fcntl F_GETFL), so we can hopefully just use
that information directly.

Fixes: libuv/libuv#1936
PR-URL: libuv/libuv#1964
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants