diff --git a/src/uv-unix.c b/src/uv-unix.c index 38e80935..15e9dec6 100644 --- a/src/uv-unix.c +++ b/src/uv-unix.c @@ -427,14 +427,17 @@ void uv__server_io(EV_P_ ev_io* watcher, int revents) { watcher == &stream->write_watcher); assert(revents == EV_READ); - assert(!(((uv_handle_t*)stream)->flags & UV_CLOSING)); + assert(!(stream->flags & UV_CLOSING)); if (stream->accepted_fd >= 0) { ev_io_stop(EV_DEFAULT_ &stream->read_watcher); return; } - while (1) { + /* connection_cb can close the server socket while we're + * in the loop so check it on each iteration. + */ + while (stream->fd != -1) { assert(stream->accepted_fd < 0); fd = uv__accept(stream->fd, (struct sockaddr*)&addr, sizeof addr); @@ -449,7 +452,6 @@ void uv__server_io(EV_P_ ev_io* watcher, int revents) { uv_err_new((uv_handle_t*)stream, errno); stream->connection_cb((uv_stream_t*)stream, -1); } - } else { stream->accepted_fd = fd; stream->connection_cb((uv_stream_t*)stream, 0); @@ -897,7 +899,7 @@ static void uv__stream_io(EV_P_ ev_io* watcher, int revents) { stream->type == UV_NAMED_PIPE); assert(watcher == &stream->read_watcher || watcher == &stream->write_watcher); - assert(!(((uv_handle_t*)stream)->flags & UV_CLOSING)); + assert(!(stream->flags & UV_CLOSING)); if (stream->connect_req) { uv__stream_connect(stream); @@ -2061,6 +2063,8 @@ static int uv__socket(int domain, int type, int protocol) { static int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t slen) { int peerfd; + assert(sockfd >= 0); + do { #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) peerfd = accept4(sockfd, saddr, &slen, SOCK_NONBLOCK | SOCK_CLOEXEC);