unix: run callbacks of pending writes when handle is closed

This commit is contained in:
Ben Noordhuis 2011-09-08 19:10:00 +02:00
parent 98b9f582f4
commit 431195c944
3 changed files with 32 additions and 0 deletions

View File

@ -235,6 +235,8 @@ void uv__finish_close(uv_handle_t* handle) {
case UV_TCP:
assert(!ev_is_active(&((uv_stream_t*)handle)->read_watcher));
assert(!ev_is_active(&((uv_stream_t*)handle)->write_watcher));
assert(((uv_stream_t*)handle)->fd == -1);
uv__stream_destroy((uv_stream_t*)handle);
break;
case UV_UDP:

View File

@ -81,6 +81,7 @@ void uv_fatal_error(const int errorno, const char* syscall);
/* stream */
int uv__stream_open(uv_stream_t*, int fd, int flags);
void uv__stream_destroy(uv_stream_t* stream);
void uv__stream_io(EV_P_ ev_io* watcher, int revents);
void uv__server_io(EV_P_ ev_io* watcher, int revents);
int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t len);

View File

@ -75,6 +75,35 @@ int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
}
/* Clears out the write queue, invokes the callbacks attached
* to each write request. Used when a stream is destroyed.
*/
static void uv__clear_queue(ngx_queue_t* wq, int status, uv_err_code code) {
uv_write_t* req;
ngx_queue_t* q;
while (!ngx_queue_empty(wq)) {
q = ngx_queue_head(wq);
ngx_queue_remove(q);
req = ngx_queue_data(q, uv_write_t, queue);
if (req->cb) {
uv_err_new(req->handle->loop, code);
req->cb(req, status);
}
if (req->bufs != req->bufsml)
free(req->bufs);
}
}
void uv__stream_destroy(uv_stream_t* stream) {
uv__clear_queue(&stream->write_queue, -1, UV_EINTR);
uv__clear_queue(&stream->write_completed_queue, 0, UV_OK);
}
void uv__server_io(EV_P_ ev_io* watcher, int revents) {
int fd;
struct sockaddr_storage addr;