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

Commit

Permalink
test: close stream immediately on error
Browse files Browse the repository at this point in the history
If there is an error in the uv_read_cb, close the uv_stream_t
immediately instead of waiting until the uv_write_cb, and only close the
stream in after_write() if it hasn't been closed already.
  • Loading branch information
trevnorris committed Apr 24, 2014
1 parent 76eb751 commit 4d905fb
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions test/echo-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,20 @@ static void after_write(uv_write_t* req, int status) {
/* Free the read/write buffer and the request */
wr = (write_req_t*) req;
free(wr->buf.base);
free(wr);

if (status == 0)
if (status == 0) {
free(wr);
return;
}

fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));

if (status == UV_ECANCELED)
return;

ASSERT(status == UV_EPIPE);
uv_close((uv_handle_t*)req->handle, on_close);
}

fprintf(stderr,
"uv_write error: %s - %s\n",
uv_err_name(status),
uv_strerror(status));

static void after_shutdown(uv_shutdown_t* req, int status) {
uv_close((uv_handle_t*)req->handle, on_close);
free(req);
if (!uv_is_closing((uv_handle_t*) req->handle))
uv_close((uv_handle_t*) req->handle, on_close);
free(wr);
}


Expand All @@ -77,7 +73,6 @@ static void after_read(uv_stream_t* handle,
const uv_buf_t* buf) {
int i;
write_req_t *wr;
uv_shutdown_t* req;

if (nread < 0) {
/* Error or EOF */
Expand All @@ -87,9 +82,7 @@ static void after_read(uv_stream_t* handle,
free(buf->base);
}

req = (uv_shutdown_t*) malloc(sizeof *req);
uv_shutdown(req, handle, after_shutdown);

uv_close((uv_handle_t*) handle, on_close);
return;
}

Expand Down

0 comments on commit 4d905fb

Please sign in to comment.