From 9a5c1bad909d2845cc0f1e32bb222a90c8ee3d53 Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Mon, 27 Feb 2012 12:54:45 -0800 Subject: [PATCH 1/4] windows: fix time conversion in stat --- src/win/util.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/win/util.c b/src/win/util.c index 0fcfb9c3..40bea97e 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -253,6 +253,7 @@ void uv_filetime_to_time_t(FILETIME* file_time, time_t* stat_time) { time.tm_hour = system_time.wHour; time.tm_min = system_time.wMinute; time.tm_sec = system_time.wSecond; + time.tm_isdst = -1; *stat_time = mktime(&time); } else { From 44c9f63cb1df484fed8117c4611358eca5f67786 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 11 Jan 2012 20:41:27 +0100 Subject: [PATCH 2/4] unix: don't rely on libev to track timer state An obscure libev bug sometimes makes it miss state changes. Keep track of the state ourselves. Fixes joyent/node#2515. --- src/unix/core.c | 46 +++++++++++++++++++++++++++++++++++---------- src/unix/ev/ev.c | 1 + src/unix/internal.h | 4 +++- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/unix/core.c b/src/unix/core.c index 40fb3d95..28a82a3a 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -66,7 +66,6 @@ static void uv__finish_close(uv_handle_t* handle); void uv_close(uv_handle_t* handle, uv_close_cb close_cb) { uv_udp_t* udp; uv_async_t* async; - uv_timer_t* timer; uv_stream_t* stream; uv_process_t* process; @@ -123,11 +122,7 @@ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) { break; case UV_TIMER: - timer = (uv_timer_t*)handle; - if (ev_is_active(&timer->timer_watcher)) { - ev_ref(timer->loop->ev); - } - ev_timer_stop(timer->loop->ev, &timer->timer_watcher); + uv_timer_stop((uv_timer_t*)handle); break; case UV_PROCESS: @@ -524,10 +519,23 @@ int uv_async_send(uv_async_t* async) { } +static int uv__timer_active(const uv_timer_t* timer) { + return timer->flags & UV_TIMER_ACTIVE; +} + + +static int uv__timer_repeating(const uv_timer_t* timer) { + return timer->flags & UV_TIMER_REPEAT; +} + + static void uv__timer_cb(EV_P_ ev_timer* w, int revents) { uv_timer_t* timer = w->data; - if (!ev_is_active(w)) { + assert(uv__timer_active(timer)); + + if (!uv__timer_repeating(timer)) { + timer->flags &= ~UV_TIMER_ACTIVE; ev_ref(EV_A); } @@ -550,43 +558,61 @@ int uv_timer_init(uv_loop_t* loop, uv_timer_t* timer) { int uv_timer_start(uv_timer_t* timer, uv_timer_cb cb, int64_t timeout, int64_t repeat) { - if (ev_is_active(&timer->timer_watcher)) { + if (uv__timer_active(timer)) { return -1; } timer->timer_cb = cb; + timer->flags |= UV_TIMER_ACTIVE; + + if (repeat) + timer->flags |= UV_TIMER_REPEAT; + else + timer->flags &= ~UV_TIMER_REPEAT; + ev_timer_set(&timer->timer_watcher, timeout / 1000.0, repeat / 1000.0); ev_timer_start(timer->loop->ev, &timer->timer_watcher); ev_unref(timer->loop->ev); + return 0; } int uv_timer_stop(uv_timer_t* timer) { - if (ev_is_active(&timer->timer_watcher)) { + if (uv__timer_active(timer)) { ev_ref(timer->loop->ev); } + timer->flags &= ~(UV_TIMER_ACTIVE | UV_TIMER_REPEAT); ev_timer_stop(timer->loop->ev, &timer->timer_watcher); + return 0; } int uv_timer_again(uv_timer_t* timer) { - if (!ev_is_active(&timer->timer_watcher)) { + if (!uv__timer_active(timer)) { uv__set_sys_error(timer->loop, EINVAL); return -1; } + assert(uv__timer_repeating(timer)); ev_timer_again(timer->loop->ev, &timer->timer_watcher); return 0; } + void uv_timer_set_repeat(uv_timer_t* timer, int64_t repeat) { assert(timer->type == UV_TIMER); timer->timer_watcher.repeat = repeat / 1000.0; + + if (repeat) + timer->flags |= UV_TIMER_REPEAT; + else + timer->flags &= ~UV_TIMER_REPEAT; } + int64_t uv_timer_get_repeat(uv_timer_t* timer) { assert(timer->type == UV_TIMER); return (int64_t)(1000 * timer->timer_watcher.repeat); diff --git a/src/unix/ev/ev.c b/src/unix/ev/ev.c index b6e190f7..a432bfbf 100644 --- a/src/unix/ev/ev.c +++ b/src/unix/ev/ev.c @@ -2554,6 +2554,7 @@ void ev_unref (EV_P) { --activecnt; + if (activecnt < 0) abort(); } void diff --git a/src/unix/internal.h b/src/unix/internal.h index 3ae5d054..fc7aad14 100644 --- a/src/unix/internal.h +++ b/src/unix/internal.h @@ -151,7 +151,9 @@ enum { UV_READABLE = 0x20, /* The stream is readable */ UV_WRITABLE = 0x40, /* The stream is writable */ UV_TCP_NODELAY = 0x080, /* Disable Nagle. */ - UV_TCP_KEEPALIVE = 0x100 /* Turn on keep-alive. */ + UV_TCP_KEEPALIVE = 0x100, /* Turn on keep-alive. */ + UV_TIMER_ACTIVE = 0x080, + UV_TIMER_REPEAT = 0x100 }; size_t uv__strlcpy(char* dst, const char* src, size_t size); From b3fe1830409fff6a55131af3156654ee4a712426 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 29 Feb 2012 15:55:05 +0100 Subject: [PATCH 3/4] unix: fix fs_event refcount bug on darwin The event loop got unref'd twice when the handle was closed. --- src/unix/kqueue.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/unix/kqueue.c b/src/unix/kqueue.c index 58a6816d..b689d68c 100644 --- a/src/unix/kqueue.c +++ b/src/unix/kqueue.c @@ -68,11 +68,12 @@ static void uv__fs_event(EV_P_ ev_io* w, int revents) { handle->cb(handle, NULL, events, 0); - uv__fs_event_stop(handle); + if (handle->fd == -1) + return; /* File watcher operates in one-shot mode, re-arm it. */ - if (handle->fd != -1) - uv__fs_event_start(handle); + uv__fs_event_stop(handle); + uv__fs_event_start(handle); } From 1ac71a31e9ae4eb1e8c6c9af5c2a9f4b8b62ba01 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Wed, 7 Mar 2012 21:21:50 +0100 Subject: [PATCH 4/4] Map EBUSY and ENOTEMPTY errors --- include/uv.h | 5 +++-- src/unix/error.c | 2 ++ src/win/error.c | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/uv.h b/include/uv.h index b6ce8ec2..fa9812cd 100644 --- a/include/uv.h +++ b/include/uv.h @@ -79,7 +79,7 @@ typedef intptr_t ssize_t; XX( 7, EAFNOSUPPORT, "") \ XX( 8, EALREADY, "") \ XX( 9, EBADF, "bad file descriptor") \ - XX( 10, EBUSY, "mount device busy") \ + XX( 10, EBUSY, "resource busy or locked") \ XX( 11, ECONNABORTED, "software caused connection abort") \ XX( 12, ECONNREFUSED, "connection refused") \ XX( 13, ECONNRESET, "connection reset by peer") \ @@ -119,7 +119,8 @@ typedef intptr_t ssize_t; XX( 49, ENAMETOOLONG, "name too long") \ XX( 50, EPERM, "operation not permitted") \ XX( 51, ELOOP, "too many symbolic links encountered") \ - XX( 52, EXDEV, "cross-device link not permitted") + XX( 52, EXDEV, "cross-device link not permitted") \ + XX( 53, ENOTEMPTY, "directory not empty") #define UV_ERRNO_GEN(val, name, s) UV_##name = val, diff --git a/src/unix/error.c b/src/unix/error.c index 93c5ac1f..071b03af 100644 --- a/src/unix/error.c +++ b/src/unix/error.c @@ -88,6 +88,8 @@ uv_err_code uv_translate_sys_error(int sys_errno) { case ESRCH: return UV_ESRCH; case ETIMEDOUT: return UV_ETIMEDOUT; case EXDEV: return UV_EXDEV; + case EBUSY: return UV_EBUSY; + case ENOTEMPTY: return UV_ENOTEMPTY; default: return UV_UNKNOWN; } diff --git a/src/win/error.c b/src/win/error.c index 1922f203..3f6615f5 100644 --- a/src/win/error.c +++ b/src/win/error.c @@ -78,6 +78,8 @@ uv_err_code uv_translate_sys_error(int sys_errno) { case WSAEAFNOSUPPORT: return UV_EAFNOSUPPORT; case WSAEWOULDBLOCK: return UV_EAGAIN; case WSAEALREADY: return UV_EALREADY; + case ERROR_LOCK_VIOLATION: return UV_EBUSY; + case ERROR_SHARING_VIOLATION: return UV_EBUSY; case ERROR_CONNECTION_ABORTED: return UV_ECONNABORTED; case WSAECONNABORTED: return UV_ECONNABORTED; case ERROR_CONNECTION_REFUSED: return UV_ECONNREFUSED; @@ -102,6 +104,7 @@ uv_err_code uv_translate_sys_error(int sys_errno) { case ERROR_OUTOFMEMORY: return UV_ENOMEM; case ERROR_NOT_CONNECTED: return UV_ENOTCONN; case WSAENOTCONN: return UV_ENOTCONN; + case ERROR_DIR_NOT_EMPTY: return UV_ENOTEMPTY; case ERROR_NOT_SUPPORTED: return UV_ENOTSUP; case ERROR_INSUFFICIENT_BUFFER: return UV_EINVAL; case ERROR_INVALID_FLAGS: return UV_EBADF;