Skip to content

Commit 8bb7e11

Browse files
committed
pipe: add back error handling to connect / bind (libuv#4202)
This was incorrectly dropped by libuv#4030, where previously connecting to "" might fail eventually, now instead it would return EINVAL and then fail to initialize the struct or call the callback.
1 parent 633629e commit 8bb7e11

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

src/unix/pipe.c

+17-2
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,22 @@ void uv_pipe_connect(uv_connect_t* req,
206206
uv_pipe_t* handle,
207207
const char* name,
208208
uv_connect_cb cb) {
209-
uv_pipe_connect2(req, handle, name, strlen(name), 0, cb);
209+
int err;
210+
211+
err = uv_pipe_connect2(req, handle, name, strlen(name), 0, cb);
212+
213+
if (err) {
214+
handle->delayed_error = err;
215+
handle->connect_req = req;
216+
217+
uv__req_init(handle->loop, req, UV_CONNECT);
218+
req->handle = (uv_stream_t*) handle;
219+
req->cb = cb;
220+
uv__queue_init(&req->queue);
221+
222+
/* Force callback to run on next tick in case of error. */
223+
uv__io_feed(handle->loop, &handle->io_watcher);
224+
}
210225
}
211226

212227

@@ -286,7 +301,7 @@ int uv_pipe_connect2(uv_connect_t* req,
286301
handle->connect_req = req;
287302

288303
uv__req_init(handle->loop, req, UV_CONNECT);
289-
req->handle = (uv_stream_t*)handle;
304+
req->handle = (uv_stream_t*) handle;
290305
req->cb = cb;
291306
uv__queue_init(&req->queue);
292307

src/win/pipe.c

+24-10
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,19 @@ void uv_pipe_connect(uv_connect_t* req,
809809
uv_pipe_t* handle,
810810
const char* name,
811811
uv_connect_cb cb) {
812-
uv_pipe_connect2(req, handle, name, strlen(name), 0, cb);
812+
uv_loop_t* loop;
813+
int err;
814+
815+
err = uv_pipe_connect2(req, handle, name, strlen(name), 0, cb);
816+
817+
if (err) {
818+
loop = handle->loop;
819+
/* Make this req pending reporting an error. */
820+
SET_REQ_ERROR(req, err);
821+
uv__insert_pending_req(loop, (uv_req_t*) req);
822+
handle->reqs_pending++;
823+
REGISTER_HANDLE_REQ(loop, handle, req);
824+
}
813825
}
814826

815827

@@ -819,11 +831,20 @@ int uv_pipe_connect2(uv_connect_t* req,
819831
size_t namelen,
820832
unsigned int flags,
821833
uv_connect_cb cb) {
822-
uv_loop_t* loop = handle->loop;
823-
int err, nameSize;
834+
uv_loop_t* loop;
835+
int err;
836+
size_t nameSize;
824837
HANDLE pipeHandle = INVALID_HANDLE_VALUE;
825838
DWORD duplex_flags;
826839

840+
loop = handle->loop;
841+
UV_REQ_INIT(loop, req, UV_CONNECT);
842+
req->handle = (uv_stream_t*) handle;
843+
req->cb = cb;
844+
req->u.connect.pipeHandle = INVALID_HANDLE_VALUE;
845+
req->u.connect.duplex_flags = 0;
846+
req->u.connect.name = NULL;
847+
827848
if (flags & ~UV_PIPE_NO_TRUNCATE) {
828849
return UV_EINVAL;
829850
}
@@ -844,13 +865,6 @@ int uv_pipe_connect2(uv_connect_t* req,
844865
return UV_ENAMETOOLONG;
845866
}
846867

847-
UV_REQ_INIT(loop, req, UV_CONNECT);
848-
req->handle = (uv_stream_t*) handle;
849-
req->cb = cb;
850-
req->u.connect.pipeHandle = INVALID_HANDLE_VALUE;
851-
req->u.connect.duplex_flags = 0;
852-
req->u.connect.name = NULL;
853-
854868
if (handle->flags & UV_HANDLE_PIPESERVER) {
855869
err = ERROR_INVALID_PARAMETER;
856870
goto error;

test/test-pipe-bind-error.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535

3636
static int close_cb_called = 0;
37+
static int connect_cb_called = 0;
3738

3839

3940
static void close_cb(uv_handle_t* handle) {
@@ -180,6 +181,14 @@ TEST_IMPL(pipe_bind_or_listen_error_after_close) {
180181
return 0;
181182
}
182183

184+
185+
static void connect_overlong_cb(uv_connect_t* connect_req, int status) {
186+
ASSERT_EQ(status, UV_EINVAL);
187+
connect_cb_called++;
188+
uv_close((uv_handle_t*) connect_req->handle, close_cb);
189+
}
190+
191+
183192
TEST_IMPL(pipe_overlong_path) {
184193
char path[512];
185194
uv_pipe_t pipe;
@@ -196,9 +205,17 @@ TEST_IMPL(pipe_overlong_path) {
196205
sizeof(path),
197206
UV_PIPE_NO_TRUNCATE,
198207
(uv_connect_cb) abort));
199-
uv_close((uv_handle_t*) &pipe, NULL);
200208
ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
201209

210+
ASSERT_EQ(UV_EINVAL, uv_pipe_bind(&pipe, ""));
211+
uv_pipe_connect(&req,
212+
&pipe,
213+
"",
214+
(uv_connect_cb) connect_overlong_cb);
215+
ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
216+
ASSERT_EQ(1, connect_cb_called);
217+
ASSERT_EQ(1, close_cb_called);
218+
202219
MAKE_VALGRIND_HAPPY(uv_default_loop());
203220
return 0;
204221

0 commit comments

Comments
 (0)