Skip to content

Commit

Permalink
windows: fix handling closed socket while poll handle is closing
Browse files Browse the repository at this point in the history
fixes joyent#1278

(backport from master for v0.10 branch)
  • Loading branch information
saghul committed Jun 17, 2014
1 parent c38e97e commit 12bb46c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ TESTS= \
test/test-platform-output.o \
test/test-poll.o \
test/test-poll-close.o \
test/test-poll-closesocket.o \
test/test-process-title.o \
test/test-ref.o \
test/test-run-nowait.o \
Expand Down
3 changes: 2 additions & 1 deletion src/win/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ static void uv__fast_poll_process_poll_req(uv_loop_t* loop, uv_poll_t* handle,
if (afd_poll_info->Handles[0].Events & AFD_POLL_LOCAL_CLOSE) {
/* Stop polling. */
handle->events = 0;
uv__handle_stop(handle);
if (uv__is_active(handle))
uv__handle_stop(handle);
}

if (events != 0) {
Expand Down
2 changes: 2 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ TEST_DECLARE (poll_duplex)
TEST_DECLARE (poll_unidirectional)
TEST_DECLARE (poll_close)
#ifdef _WIN32
TEST_DECLARE (poll_closesocket)
TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows)
TEST_DECLARE (argument_escaping)
TEST_DECLARE (environment_creation)
Expand Down Expand Up @@ -449,6 +450,7 @@ TASK_LIST_START
TEST_ENTRY (kill)

#ifdef _WIN32
TEST_ENTRY (poll_closesocket)
TEST_ENTRY (spawn_detect_pipe_name_collisions_on_windows)
TEST_ENTRY (argument_escaping)
TEST_ENTRY (environment_creation)
Expand Down
89 changes: 89 additions & 0 deletions test/test-poll-closesocket.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* 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.
*/

#ifdef _WIN32

#include <errno.h>

#include "uv.h"
#include "task.h"

uv_os_sock_t sock;
uv_poll_t handle;

static int close_cb_called = 0;


static void close_cb(uv_handle_t* h) {
close_cb_called++;
}


static void poll_cb(uv_poll_t* h, int status, int events) {
int r;

ASSERT(status == 0);
ASSERT(h == &handle);

r = uv_poll_start(&handle, UV_READABLE, poll_cb);
ASSERT(r == 0);

closesocket(sock);
uv_close((uv_handle_t*) &handle, close_cb);

}


TEST_IMPL(poll_closesocket) {
struct WSAData wsa_data;
int r;
unsigned long on;
struct sockaddr_in addr;

r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
ASSERT(r == 0);

sock = socket(AF_INET, SOCK_STREAM, 0);
ASSERT(sock != INVALID_SOCKET);
on = 1;
r = ioctlsocket(sock, FIONBIO, &on);
ASSERT(r == 0);

r = uv_ip4_addr("127.0.0.1", TEST_PORT, &addr);
ASSERT(r == 0);

r = connect(sock, (const struct sockaddr*) &addr, sizeof addr);
ASSERT(r != 0);
ASSERT(WSAGetLastError() == WSAEWOULDBLOCK);

r = uv_poll_init_socket(uv_default_loop(), &handle, sock);
ASSERT(r == 0);
r = uv_poll_start(&handle, UV_WRITABLE, poll_cb);
ASSERT(r == 0);

uv_run(uv_default_loop(), UV_RUN_DEFAULT);

ASSERT(close_cb_called == 1);

MAKE_VALGRIND_HAPPY();
return 0;
}
#endif
1 change: 1 addition & 0 deletions uv.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
'test/test-platform-output.c',
'test/test-poll.c',
'test/test-poll-close.c',
'test/test-poll-closesocket.c',
'test/test-process-title.c',
'test/test-ref.c',
'test/test-run-nowait.c',
Expand Down

0 comments on commit 12bb46c

Please sign in to comment.