Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Raise UV_ECANCELED on premature close.
Set the error code to the more appropriate UV_ECANCELED instead of UV_EINTR
when the handle is closed and there are in-flight requests.
  • Loading branch information
bnoordhuis committed Jul 27, 2012
1 parent 9f59e8e commit cf05c5f
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 17 deletions.
5 changes: 3 additions & 2 deletions include/uv.h
Expand Up @@ -127,7 +127,8 @@ extern "C" {
XX( 54, ENOSPC, "no space left on device") \
XX( 55, EIO, "i/o error") \
XX( 56, EROFS, "read-only file system" ) \
XX( 57, ENODEV, "no such device" )
XX( 57, ENODEV, "no such device" ) \
XX( 58, ECANCELED, "operation canceled" )


#define UV_ERRNO_GEN(val, name, s) UV_##name = val,
Expand Down Expand Up @@ -436,7 +437,7 @@ UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
*
* In-progress requests, like uv_connect_t or uv_write_t, are cancelled and
* have their callbacks called asynchronously with status=-1 and the error code
* set to UV_EINTR.
* set to UV_ECANCELED.
*/
UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);

Expand Down
6 changes: 3 additions & 3 deletions src/unix/stream.c
Expand Up @@ -120,7 +120,7 @@ void uv__stream_destroy(uv_stream_t* stream) {

if (stream->connect_req) {
uv__req_unregister(stream->loop, stream->connect_req);
uv__set_artificial_error(stream->loop, UV_EINTR);
uv__set_artificial_error(stream->loop, UV_ECANCELED);
stream->connect_req->cb(stream->connect_req, -1);
stream->connect_req = NULL;
}
Expand All @@ -136,7 +136,7 @@ void uv__stream_destroy(uv_stream_t* stream) {
free(req->bufs);

if (req->cb) {
uv__set_artificial_error(req->handle->loop, UV_EINTR);
uv__set_artificial_error(req->handle->loop, UV_ECANCELED);
req->cb(req, -1);
}
}
Expand All @@ -156,7 +156,7 @@ void uv__stream_destroy(uv_stream_t* stream) {

if (stream->shutdown_req) {
uv__req_unregister(stream->loop, stream->shutdown_req);
uv__set_artificial_error(stream->loop, UV_EINTR);
uv__set_artificial_error(stream->loop, UV_ECANCELED);
stream->shutdown_req->cb(stream->shutdown_req, -1);
stream->shutdown_req = NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/win/pipe.c
Expand Up @@ -300,7 +300,7 @@ void uv_pipe_endgame(uv_loop_t* loop, uv_pipe_t* handle) {

/* Already closing. Cancel the shutdown. */
if (req->cb) {
uv__set_sys_error(loop, WSAEINTR);
uv__set_artificial_error(loop, UV_ECANCELED);
req->cb(req, -1);
}

Expand Down
8 changes: 2 additions & 6 deletions src/win/tcp.c
Expand Up @@ -157,7 +157,6 @@ int uv_tcp_init(uv_loop_t* loop, uv_tcp_t* handle) {

void uv_tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle) {
int status;
int sys_error;
unsigned int i;
uv_tcp_accept_t* req;

Expand All @@ -169,19 +168,16 @@ void uv_tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle) {

if (handle->flags & UV_HANDLE_CLOSING) {
status = -1;
sys_error = WSAEINTR;
uv__set_artificial_error(loop, UV_ECANCELED);
} else if (shutdown(handle->socket, SD_SEND) != SOCKET_ERROR) {
status = 0;
handle->flags |= UV_HANDLE_SHUT;
} else {
status = -1;
sys_error = WSAGetLastError();
uv__set_sys_error(loop, WSAGetLastError());
}

if (handle->shutdown_req->cb) {
if (status == -1) {
uv__set_sys_error(loop, sys_error);
}
handle->shutdown_req->cb(handle->shutdown_req, status);
}

Expand Down
2 changes: 1 addition & 1 deletion src/win/tty.c
Expand Up @@ -1751,7 +1751,7 @@ void uv_tty_endgame(uv_loop_t* loop, uv_tty_t* handle) {
/* TTY shutdown is really just a no-op */
if (handle->shutdown_req->cb) {
if (handle->flags & UV_HANDLE_CLOSING) {
uv__set_sys_error(loop, WSAEINTR);
uv__set_artificial_error(loop, UV_ECANCELED);
handle->shutdown_req->cb(handle->shutdown_req, -1);
} else {
handle->shutdown_req->cb(handle->shutdown_req, 0);
Expand Down
2 changes: 1 addition & 1 deletion test/benchmark-udp-packet-storm.c
Expand Up @@ -91,7 +91,7 @@ static void recv_cb(uv_udp_t* handle,
return;

if (nread == -1) {
ASSERT(uv_last_error(loop).code == UV_EINTR); /* FIXME change error code */
ASSERT(uv_last_error(loop).code == UV_ECANCELED);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions test/test-shutdown-close.c
Expand Up @@ -37,9 +37,9 @@ static int close_cb_called = 0;


static void shutdown_cb(uv_shutdown_t* req, int status) {
int err = uv_last_error(uv_default_loop()).code;
ASSERT(req == &shutdown_req);
ASSERT(status == 0 ||
(status == -1 && uv_last_error(uv_default_loop()).code == UV_EINTR));
ASSERT(status == 0 || (status == -1 && err == UV_ECANCELED));
shutdown_cb_called++;
}

Expand Down
2 changes: 1 addition & 1 deletion test/test-tcp-close-while-connecting.c
Expand Up @@ -38,7 +38,7 @@ static void close_cb(uv_handle_t* handle) {

static void connect_cb(uv_connect_t* req, int status) {
ASSERT(status == -1);
ASSERT(uv_last_error(req->handle->loop).code == UV_EINTR);
ASSERT(uv_last_error(req->handle->loop).code == UV_ECANCELED);
uv_timer_stop(&timer2_handle);
connect_cb_called++;
}
Expand Down

0 comments on commit cf05c5f

Please sign in to comment.