From 38fc6ad839734a3f76d2535b8f62d92e9cfef8c7 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 2 Jan 2012 09:55:48 +0100 Subject: [PATCH] unix: unref fs event watcher Watchers were being ref-counted twice which wasn't harmful in itself but stopped uv_unref() from working like you'd expect it to. --- src/unix/kqueue.c | 2 ++ src/unix/linux.c | 2 ++ src/unix/sunos.c | 2 ++ test/test-fs-event.c | 20 +++++++++++++++++++- test/test-list.h | 2 ++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/unix/kqueue.c b/src/unix/kqueue.c index 551bce0a..58a6816d 100644 --- a/src/unix/kqueue.c +++ b/src/unix/kqueue.c @@ -43,10 +43,12 @@ static void uv__fs_event_start(uv_fs_event_t* handle) { handle->fd, EV_LIBUV_KQUEUE_HACK); ev_io_start(handle->loop->ev, &handle->event_watcher); + ev_unref(handle->loop->ev); } static void uv__fs_event_stop(uv_fs_event_t* handle) { + ev_ref(handle->loop->ev); ev_io_stop(handle->loop->ev, &handle->event_watcher); } diff --git a/src/unix/linux.c b/src/unix/linux.c index 965de017..3ed65653 100644 --- a/src/unix/linux.c +++ b/src/unix/linux.c @@ -283,12 +283,14 @@ int uv_fs_event_init(uv_loop_t* loop, ev_io_init(&handle->read_watcher, uv__inotify_read, fd, EV_READ); ev_io_start(loop->ev, &handle->read_watcher); + ev_unref(loop->ev); return 0; } void uv__fs_event_destroy(uv_fs_event_t* handle) { + ev_ref(handle->loop->ev); ev_io_stop(handle->loop->ev, &handle->read_watcher); uv__close(handle->fd); handle->fd = -1; diff --git a/src/unix/sunos.c b/src/unix/sunos.c index cf9f162a..dbdad3ce 100644 --- a/src/unix/sunos.c +++ b/src/unix/sunos.c @@ -166,12 +166,14 @@ int uv_fs_event_init(uv_loop_t* loop, ev_io_init(&handle->event_watcher, uv__fs_event_read, portfd, EV_READ); ev_io_start(loop->ev, &handle->event_watcher); + ev_unref(loop->ev); return 0; } void uv__fs_event_destroy(uv_fs_event_t* handle) { + ev_ref(handle->loop->ev); ev_io_stop(handle->loop->ev, &handle->event_watcher); uv__close(handle->fd); handle->fd = -1; diff --git a/test/test-fs-event.c b/test/test-fs-event.c index 59bdebc0..8b52f67f 100644 --- a/test/test-fs-event.c +++ b/test/test-fs-event.c @@ -282,7 +282,7 @@ static void timer_cb(uv_timer_t* handle, int status) { ASSERT(status == 0); r = uv_fs_event_init(handle->loop, &fs_event, ".", fs_event_fail, 0); - ASSERT(r != -1); + ASSERT(r == 0); uv_close((uv_handle_t*)&fs_event, close_cb); uv_close((uv_handle_t*)handle, close_cb); @@ -308,3 +308,21 @@ TEST_IMPL(fs_event_immediate_close) { return 0; } + + +TEST_IMPL(fs_event_unref) { + uv_loop_t* loop; + int r; + + loop = uv_default_loop(); + + r = uv_fs_event_init(loop, &fs_event, ".", fs_event_fail, 0); + ASSERT(r == 0); + + uv_unref(loop); + + r = uv_run(loop); + ASSERT(r == 0); + + return 0; +} diff --git a/test/test-list.h b/test/test-list.h index 3efc721f..f6b58a69 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -112,6 +112,7 @@ TEST_DECLARE (fs_event_watch_file) TEST_DECLARE (fs_event_watch_file_current_dir) TEST_DECLARE (fs_event_no_callback_on_close) TEST_DECLARE (fs_event_immediate_close) +TEST_DECLARE (fs_event_unref) TEST_DECLARE (fs_readdir_empty_dir) TEST_DECLARE (fs_readdir_file) TEST_DECLARE (fs_open_dir) @@ -268,6 +269,7 @@ TASK_LIST_START TEST_ENTRY (fs_event_watch_file_current_dir) TEST_ENTRY (fs_event_no_callback_on_close) TEST_ENTRY (fs_event_immediate_close) + TEST_ENTRY (fs_event_unref) TEST_ENTRY (fs_readdir_empty_dir) TEST_ENTRY (fs_readdir_file) TEST_ENTRY (fs_open_dir)