Skip to content

Commit 393c1c5

Browse files
bnoordhuissaghul
authored andcommitted
unix: set non-block mode in uv_{pipe,tcp,udp}_open
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: libuv#124 PR: libuv#134 Reviewed-by: Saúl Ibarra Corretgé <[email protected]>
1 parent bb5f5d1 commit 393c1c5

File tree

8 files changed

+19
-8
lines changed

8 files changed

+19
-8
lines changed

docs/src/pipe.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ API
3838
3939
Open an existing file descriptor or HANDLE as a pipe.
4040
41-
.. note::
42-
The user is responsible for setting the file descriptor in non-blocking mode.
41+
.. versionchanged:: 1.2.1 the file descriptor is set to non-blocking mode.
4342
4443
.. c:function:: int uv_pipe_bind(uv_pipe_t* handle, const char* name)
4544

docs/src/tcp.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ API
3636
3737
Open an existing file descriptor or SOCKET as a TCP handle.
3838
39-
.. note::
40-
The user is responsible for setting the file descriptor in
41-
non-blocking mode.
39+
.. versionchanged:: 1.2.1 the file descriptor is set to non-blocking mode.
4240
4341
.. c:function:: int uv_tcp_nodelay(uv_tcp_t* handle, int enable)
4442

docs/src/udp.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ API
120120
In other words, other datagram-type sockets like raw sockets or netlink
121121
sockets can also be passed to this function.
122122
123+
.. versionchanged:: 1.2.1 the file descriptor is set to non-blocking mode.
124+
123125
.. c:function:: int uv_udp_bind(uv_udp_t* handle, const struct sockaddr* addr, unsigned int flags)
124126
125127
Bind the UDP handle to an IP address and port.

src/unix/pipe.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ void uv__pipe_close(uv_pipe_t* handle) {
125125

126126

127127
int uv_pipe_open(uv_pipe_t* handle, uv_file fd) {
128-
#if defined(__APPLE__)
129128
int err;
130129

130+
err = uv__nonblock(fd, 1);
131+
if (err)
132+
return err;
133+
134+
#if defined(__APPLE__)
131135
err = uv__stream_try_select((uv_stream_t*) handle, &fd);
132136
if (err)
133137
return err;

src/unix/tcp.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ int uv__tcp_connect(uv_connect_t* req,
156156

157157

158158
int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock) {
159+
int err;
160+
161+
err = uv__nonblock(sock, 1);
162+
if (err)
163+
return err;
164+
159165
return uv__stream_open((uv_stream_t*)handle,
160166
sock,
161167
UV_STREAM_READABLE | UV_STREAM_WRITABLE);

src/unix/udp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,10 @@ int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock) {
565565
if (handle->io_watcher.fd != -1)
566566
return -EALREADY; /* FIXME(bnoordhuis) Should be -EBUSY. */
567567

568+
err = uv__nonblock(sock, 1);
569+
if (err)
570+
return err;
571+
568572
err = uv__set_reuse(sock);
569573
if (err)
570574
return err;

test/test-close-fd.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ TEST_IMPL(close_fd) {
5454
int fd[2];
5555

5656
ASSERT(0 == pipe(fd));
57-
ASSERT(0 == fcntl(fd[0], F_SETFL, O_NONBLOCK));
5857
ASSERT(0 == uv_pipe_init(uv_default_loop(), &pipe_handle, 0));
5958
ASSERT(0 == uv_pipe_open(&pipe_handle, fd[0]));
6059
fd[0] = -1; /* uv_pipe_open() takes ownership of the file descriptor. */

test/test-spawn.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,6 @@ TEST_IMPL(closed_fd_events) {
12431243

12441244
/* create a pipe and share it with a child process */
12451245
ASSERT(0 == pipe(fd));
1246-
ASSERT(0 == fcntl(fd[0], F_SETFL, O_NONBLOCK));
12471246

12481247
/* spawn_helper4 blocks indefinitely. */
12491248
init_process_options("spawn_helper4", exit_cb);

0 commit comments

Comments
 (0)