Skip to content

Commit 8f9ba2a

Browse files
vtjnashsantigimeno
authored andcommitted
Revert "Revert "unix,tcp: avoid marking server sockets connected""
This reverts commit 2098773. PR-URL: #1741 Reviewed-By: Santiago Gimeno <[email protected]>
1 parent c409b3f commit 8f9ba2a

4 files changed

Lines changed: 53 additions & 5 deletions

File tree

src/unix/stream.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,9 @@ int uv_write2(uv_write_t* req,
14171417
if (uv__stream_fd(stream) < 0)
14181418
return UV_EBADF;
14191419

1420+
if (!(stream->flags & UV_STREAM_WRITABLE))
1421+
return -EPIPE;
1422+
14201423
if (send_handle) {
14211424
if (stream->type != UV_NAMED_PIPE || !((uv_pipe_t*)stream)->ipc)
14221425
return UV_EINVAL;
@@ -1568,6 +1571,9 @@ int uv_read_start(uv_stream_t* stream,
15681571
if (stream->flags & UV_CLOSING)
15691572
return UV_EINVAL;
15701573

1574+
if (!(stream->flags & UV_STREAM_READABLE))
1575+
return -ENOTCONN;
1576+
15711577
/* The UV_STREAM_READING flag is irrelevant of the state of the tcp - it just
15721578
* expresses the desired state of the user.
15731579
*/

src/unix/tcp.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ int uv__tcp_bind(uv_tcp_t* tcp,
158158
if ((flags & UV_TCP_IPV6ONLY) && addr->sa_family != AF_INET6)
159159
return UV_EINVAL;
160160

161-
err = maybe_new_socket(tcp,
162-
addr->sa_family,
163-
UV_STREAM_READABLE | UV_STREAM_WRITABLE);
161+
err = maybe_new_socket(tcp, addr->sa_family, 0);
164162
if (err)
165163
return err;
166164

@@ -335,14 +333,14 @@ int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
335333
if (single_accept)
336334
tcp->flags |= UV_TCP_SINGLE_ACCEPT;
337335

338-
flags = UV_STREAM_READABLE;
336+
flags = 0;
339337
#if defined(__MVS__)
340338
/* on zOS the listen call does not bind automatically
341339
if the socket is unbound. Hence the manual binding to
342340
an arbitrary port is required to be done manually
343341
*/
344342
flags |= UV_HANDLE_BOUND;
345-
#endif
343+
#endif
346344
err = maybe_new_socket(tcp, AF_INET, flags);
347345
if (err)
348346
return err;

test/test-list.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ TEST_DECLARE (tcp_bind_error_fault)
9595
TEST_DECLARE (tcp_bind_error_inval)
9696
TEST_DECLARE (tcp_bind_localhost_ok)
9797
TEST_DECLARE (tcp_bind_invalid_flags)
98+
TEST_DECLARE (tcp_bind_writable_flags)
9899
TEST_DECLARE (tcp_listen_without_bind)
99100
TEST_DECLARE (tcp_connect_error_fault)
100101
TEST_DECLARE (tcp_connect_timeout)
@@ -535,6 +536,7 @@ TASK_LIST_START
535536
TEST_ENTRY (tcp_bind_error_inval)
536537
TEST_ENTRY (tcp_bind_localhost_ok)
537538
TEST_ENTRY (tcp_bind_invalid_flags)
539+
TEST_ENTRY (tcp_bind_writable_flags)
538540
TEST_ENTRY (tcp_listen_without_bind)
539541
TEST_ENTRY (tcp_connect_error_fault)
540542
TEST_ENTRY (tcp_connect_timeout)

test/test-tcp-bind-error.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,45 @@ TEST_IMPL(tcp_listen_without_bind) {
214214
MAKE_VALGRIND_HAPPY();
215215
return 0;
216216
}
217+
218+
219+
TEST_IMPL(tcp_bind_writable_flags) {
220+
struct sockaddr_in addr;
221+
uv_tcp_t server;
222+
uv_buf_t buf;
223+
uv_write_t write_req;
224+
uv_shutdown_t shutdown_req;
225+
int r;
226+
227+
ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
228+
r = uv_tcp_init(uv_default_loop(), &server);
229+
ASSERT(r == 0);
230+
r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
231+
ASSERT(r == 0);
232+
r = uv_listen((uv_stream_t*)&server, 128, NULL);
233+
ASSERT(r == 0);
234+
235+
ASSERT(0 == uv_is_writable((uv_stream_t*) &server));
236+
ASSERT(0 == uv_is_readable((uv_stream_t*) &server));
237+
238+
buf = uv_buf_init("PING", 4);
239+
r = uv_write(&write_req, (uv_stream_t*) &server, &buf, 1, NULL);
240+
ASSERT(r == UV_EPIPE);
241+
r = uv_shutdown(&shutdown_req, (uv_stream_t*) &server, NULL);
242+
#ifdef _WIN32
243+
ASSERT(r == UV_EPIPE);
244+
#else
245+
ASSERT(r == UV_ENOTCONN);
246+
#endif
247+
r = uv_read_start((uv_stream_t*) &server, NULL, NULL);
248+
ASSERT(r == UV_ENOTCONN);
249+
250+
uv_close((uv_handle_t*)&server, close_cb);
251+
252+
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
253+
254+
ASSERT(close_cb_called == 1);
255+
256+
MAKE_VALGRIND_HAPPY();
257+
return 0;
258+
}

0 commit comments

Comments
 (0)