From d5e6f4372f553b97f58ed5bc8b1efad8b0546bba Mon Sep 17 00:00:00 2001 From: Andrius Bentkus Date: Thu, 21 Aug 2014 10:59:35 +0200 Subject: [PATCH] unix: fix tcp write after bad connect freezing If the connect wouldn't go off (no such tcp remote or any other failure), the subsequent writes made would not be called. Now we call the writes in the queue with ECANCELED if the connect fails. Fix #1432 --- Makefile.am | 1 + src/unix/stream.c | 38 +++++++++++----- test/test-list.h | 7 +++ test/test-tcp-write-after-connect.c | 68 +++++++++++++++++++++++++++++ uv.gyp | 1 + 5 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 test/test-tcp-write-after-connect.c diff --git a/Makefile.am b/Makefile.am index f72cb9ea..cd9af1cd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -196,6 +196,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \ test/test-tcp-shutdown-after-write.c \ test/test-tcp-unexpected-read.c \ test/test-tcp-write-to-half-open-connection.c \ + test/test-tcp-write-after-connect.c \ test/test-tcp-writealot.c \ test/test-tcp-try-write.c \ test/test-tcp-write-queue-order.c \ diff --git a/src/unix/stream.c b/src/unix/stream.c index cd85d2fc..07c82a24 100644 --- a/src/unix/stream.c +++ b/src/unix/stream.c @@ -361,10 +361,22 @@ int uv__stream_open(uv_stream_t* stream, int fd, int flags) { } -void uv__stream_destroy(uv_stream_t* stream) { +void uv__stream_flush_write_queue(uv_stream_t* stream, int error) { uv_write_t* req; QUEUE* q; + while (!QUEUE_EMPTY(&stream->write_queue)) { + q = QUEUE_HEAD(&stream->write_queue); + QUEUE_REMOVE(q); + req = QUEUE_DATA(q, uv_write_t, queue); + req->error = error; + + QUEUE_INSERT_TAIL(&stream->write_completed_queue, &req->queue); + } +} + + +void uv__stream_destroy(uv_stream_t* stream) { assert(!uv__io_active(&stream->io_watcher, UV__POLLIN | UV__POLLOUT)); assert(stream->flags & UV_CLOSED); @@ -374,16 +386,7 @@ void uv__stream_destroy(uv_stream_t* stream) { stream->connect_req = NULL; } - while (!QUEUE_EMPTY(&stream->write_queue)) { - q = QUEUE_HEAD(&stream->write_queue); - QUEUE_REMOVE(q); - - req = QUEUE_DATA(q, uv_write_t, queue); - req->error = -ECANCELED; - - QUEUE_INSERT_TAIL(&stream->write_completed_queue, &req->queue); - } - + uv__stream_flush_write_queue(stream, -ECANCELED); uv__write_callbacks(stream); if (stream->shutdown_req) { @@ -1232,10 +1235,21 @@ static void uv__stream_connect(uv_stream_t* stream) { stream->connect_req = NULL; uv__req_unregister(stream->loop, req); - uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLOUT); + + if (error < 0 || QUEUE_EMPTY(&stream->write_queue)) { + uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLOUT); + } if (req->cb) req->cb(req, error); + + if (uv__stream_fd(stream) == -1) + return; + + if (error < 0) { + uv__stream_flush_write_queue(stream, -ECANCELED); + uv__write_callbacks(stream); + } } diff --git a/test/test-list.h b/test/test-list.h index a5aa2103..cb069668 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -55,6 +55,9 @@ TEST_DECLARE (tcp_ping_pong_v6) TEST_DECLARE (pipe_ping_pong) TEST_DECLARE (delayed_accept) TEST_DECLARE (multiple_listen) +#ifndef _WIN32 +TEST_DECLARE (tcp_write_after_connect) +#endif TEST_DECLARE (tcp_writealot) TEST_DECLARE (tcp_try_write) TEST_DECLARE (tcp_write_queue_order) @@ -343,6 +346,10 @@ TASK_LIST_START TEST_ENTRY (delayed_accept) TEST_ENTRY (multiple_listen) +#ifndef _WIN32 + TEST_ENTRY (tcp_write_after_connect) +#endif + TEST_ENTRY (tcp_writealot) TEST_HELPER (tcp_writealot, tcp4_echo_server) diff --git a/test/test-tcp-write-after-connect.c b/test/test-tcp-write-after-connect.c new file mode 100644 index 00000000..aa03228f --- /dev/null +++ b/test/test-tcp-write-after-connect.c @@ -0,0 +1,68 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef _WIN32 + +#include "uv.h" +#include "task.h" + +uv_loop_t loop; +uv_tcp_t tcp_client; +uv_connect_t connection_request; +uv_write_t write_request; +uv_buf_t buf = { "HELLO", 4 }; + + +static void write_cb(uv_write_t *req, int status) { + ASSERT(status == UV_ECANCELED); + uv_close((uv_handle_t*) req->handle, NULL); +} + + +static void connect_cb(uv_connect_t *req, int status) { + ASSERT(status == UV_ECONNREFUSED); +} + + +TEST_IMPL(tcp_write_after_connect) { + struct sockaddr_in sa; + ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &sa)); + ASSERT(0 == uv_loop_init(&loop)); + ASSERT(0 == uv_tcp_init(&loop, &tcp_client)); + + ASSERT(0 == uv_tcp_connect(&connection_request, + &tcp_client, + (const struct sockaddr *) + &sa, + connect_cb)); + + ASSERT(0 == uv_write(&write_request, + (uv_stream_t *)&tcp_client, + &buf, 1, + write_cb)); + + uv_run(&loop, UV_RUN_DEFAULT); + + MAKE_VALGRIND_HAPPY(); + return 0; +} + +#endif diff --git a/uv.gyp b/uv.gyp index b9edcafa..187b25b8 100644 --- a/uv.gyp +++ b/uv.gyp @@ -378,6 +378,7 @@ 'test/test-tcp-connect6-error.c', 'test/test-tcp-open.c', 'test/test-tcp-write-to-half-open-connection.c', + 'test/test-tcp-write-after-connect.c', 'test/test-tcp-writealot.c', 'test/test-tcp-try-write.c', 'test/test-tcp-unexpected-read.c',