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
This commit is contained in:
Andrius Bentkus 2014-08-21 10:59:35 +02:00
parent a4f03504ce
commit d5e6f4372f
5 changed files with 103 additions and 12 deletions

View File

@ -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 \

View File

@ -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);
}
}

View File

@ -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)

View File

@ -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

1
uv.gyp
View File

@ -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',