From 393c1c59a27591d705648919b2d7fb921cba37bc Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 13 Jan 2015 17:33:50 +0100 Subject: [PATCH] unix: set non-block mode in uv_{pipe,tcp,udp}_open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The contract specifies that the file descriptor should already be in non-blocking mode before passing it to libuv. However, node users don't really have an opportunity to do so, never mind the fact that the call to uv_pipe_open() or uv_tcp_open() is an implementation detail that most users won't be aware of. Let's be nice and set the non-blocking flag explicitly. It's a cheap operation anyway. Fixes: https://github.com/libuv/libuv/issues/124 PR: https://github.com/libuv/libuv/pull/134 Reviewed-by: Saúl Ibarra Corretgé --- docs/src/pipe.rst | 3 +-- docs/src/tcp.rst | 4 +--- docs/src/udp.rst | 2 ++ src/unix/pipe.c | 6 +++++- src/unix/tcp.c | 6 ++++++ src/unix/udp.c | 4 ++++ test/test-close-fd.c | 1 - test/test-spawn.c | 1 - 8 files changed, 19 insertions(+), 8 deletions(-) diff --git a/docs/src/pipe.rst b/docs/src/pipe.rst index 614bb2e3b1f..9fbb1f6c209 100644 --- a/docs/src/pipe.rst +++ b/docs/src/pipe.rst @@ -38,8 +38,7 @@ API Open an existing file descriptor or HANDLE as a pipe. - .. note:: - The user is responsible for setting the file descriptor in non-blocking mode. + .. versionchanged:: 1.2.1 the file descriptor is set to non-blocking mode. .. c:function:: int uv_pipe_bind(uv_pipe_t* handle, const char* name) diff --git a/docs/src/tcp.rst b/docs/src/tcp.rst index 2c1001b531f..8baedde86c5 100644 --- a/docs/src/tcp.rst +++ b/docs/src/tcp.rst @@ -36,9 +36,7 @@ API Open an existing file descriptor or SOCKET as a TCP handle. - .. note:: - The user is responsible for setting the file descriptor in - non-blocking mode. + .. versionchanged:: 1.2.1 the file descriptor is set to non-blocking mode. .. c:function:: int uv_tcp_nodelay(uv_tcp_t* handle, int enable) diff --git a/docs/src/udp.rst b/docs/src/udp.rst index 175ce07a2db..9c4aa2102bd 100644 --- a/docs/src/udp.rst +++ b/docs/src/udp.rst @@ -120,6 +120,8 @@ API In other words, other datagram-type sockets like raw sockets or netlink sockets can also be passed to this function. + .. versionchanged:: 1.2.1 the file descriptor is set to non-blocking mode. + .. c:function:: int uv_udp_bind(uv_udp_t* handle, const struct sockaddr* addr, unsigned int flags) Bind the UDP handle to an IP address and port. diff --git a/src/unix/pipe.c b/src/unix/pipe.c index ef47700b7a1..ba833d3f547 100644 --- a/src/unix/pipe.c +++ b/src/unix/pipe.c @@ -125,9 +125,13 @@ void uv__pipe_close(uv_pipe_t* handle) { int uv_pipe_open(uv_pipe_t* handle, uv_file fd) { -#if defined(__APPLE__) int err; + err = uv__nonblock(fd, 1); + if (err) + return err; + +#if defined(__APPLE__) err = uv__stream_try_select((uv_stream_t*) handle, &fd); if (err) return err; diff --git a/src/unix/tcp.c b/src/unix/tcp.c index 8c19c1ab956..4060e7bd709 100644 --- a/src/unix/tcp.c +++ b/src/unix/tcp.c @@ -156,6 +156,12 @@ int uv__tcp_connect(uv_connect_t* req, int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock) { + int err; + + err = uv__nonblock(sock, 1); + if (err) + return err; + return uv__stream_open((uv_stream_t*)handle, sock, UV_STREAM_READABLE | UV_STREAM_WRITABLE); diff --git a/src/unix/udp.c b/src/unix/udp.c index 2e1824c358a..941c0aec6e2 100644 --- a/src/unix/udp.c +++ b/src/unix/udp.c @@ -565,6 +565,10 @@ int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock) { if (handle->io_watcher.fd != -1) return -EALREADY; /* FIXME(bnoordhuis) Should be -EBUSY. */ + err = uv__nonblock(sock, 1); + if (err) + return err; + err = uv__set_reuse(sock); if (err) return err; diff --git a/test/test-close-fd.c b/test/test-close-fd.c index 0d17f076615..93a7bd7c021 100644 --- a/test/test-close-fd.c +++ b/test/test-close-fd.c @@ -54,7 +54,6 @@ TEST_IMPL(close_fd) { int fd[2]; ASSERT(0 == pipe(fd)); - ASSERT(0 == fcntl(fd[0], F_SETFL, O_NONBLOCK)); ASSERT(0 == uv_pipe_init(uv_default_loop(), &pipe_handle, 0)); ASSERT(0 == uv_pipe_open(&pipe_handle, fd[0])); fd[0] = -1; /* uv_pipe_open() takes ownership of the file descriptor. */ diff --git a/test/test-spawn.c b/test/test-spawn.c index 5c25f81926b..a229d225f1b 100644 --- a/test/test-spawn.c +++ b/test/test-spawn.c @@ -1243,7 +1243,6 @@ TEST_IMPL(closed_fd_events) { /* create a pipe and share it with a child process */ ASSERT(0 == pipe(fd)); - ASSERT(0 == fcntl(fd[0], F_SETFL, O_NONBLOCK)); /* spawn_helper4 blocks indefinitely. */ init_process_options("spawn_helper4", exit_cb);