diff --git a/src/unix/stream.c b/src/unix/stream.c index 0bc5fe85..c1b5e3c5 100644 --- a/src/unix/stream.c +++ b/src/unix/stream.c @@ -282,6 +282,7 @@ int uv__stream_try_select(uv_stream_t* stream, int* fd) { int fds[2]; int ret; int kq; + int old_fd; kq = kqueue(); if (kq == -1) { @@ -333,16 +334,20 @@ int uv__stream_try_select(uv_stream_t* stream, int* fd) { s->fake_fd = fds[0]; s->int_fd = fds[1]; - if (uv_thread_create(&s->thread, uv__stream_osx_select, stream)) - goto fatal4; - + old_fd = *fd; s->stream = stream; stream->select = s; *fd = s->fake_fd; + if (uv_thread_create(&s->thread, uv__stream_osx_select, stream)) + goto fatal4; + return 0; fatal4: + s->stream = NULL; + stream->select = NULL; + *fd = old_fd; close(s->fake_fd); close(s->int_fd); s->fake_fd = -1;