Permalink
Browse files

unix, windows: fix memory leak in fs-poll.c

The internal timer handle associated with the uv_fs_poll_t wasn't always closed
when the fs poll handle was closed, leaking about 650 bytes memory.
  • Loading branch information...
1 parent f793298 commit 6342658ad8281a1fb70cf3777d35be21dba6c8bb @bnoordhuis bnoordhuis committed Oct 1, 2012
Showing with 13 additions and 9 deletions.
  1. +13 −9 src/fs-poll.c
View
@@ -103,11 +103,15 @@ int uv_fs_poll_stop(uv_fs_poll_t* handle) {
ctx = handle->poll_ctx;
assert(ctx != NULL);
assert(ctx->parent_handle != NULL);
-
ctx->parent_handle = NULL;
- uv_timer_stop(&ctx->timer_handle);
-
handle->poll_ctx = NULL;
+
+ /* Close the timer if it's active. If it's inactive, there's a stat request
+ * in progress and poll_cb will take care of the cleanup.
+ */
+ if (uv__is_active(&ctx->timer_handle))
+ uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
+
uv__handle_stop(handle);
return 0;
@@ -123,12 +127,7 @@ static void timer_cb(uv_timer_t* timer, int status) {
struct poll_ctx* ctx;
ctx = container_of(timer, struct poll_ctx, timer_handle);
-
- if (ctx->parent_handle == NULL) { /* handle has been stopped or closed */
- uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
- return;
- }
-
+ assert(ctx->parent_handle != NULL);
assert(ctx->parent_handle->poll_ctx == ctx);
ctx->start_time = uv_now(ctx->loop);
@@ -171,6 +170,11 @@ static void poll_cb(uv_fs_t* req) {
out:
uv_fs_req_cleanup(req);
+ if (ctx->parent_handle == NULL) { /* handle has been stopped by callback */
+ uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
+ return;
+ }
+
/* Reschedule timer, subtract the delay from doing the stat(). */
interval = ctx->interval;
interval -= (uv_now(ctx->loop) - ctx->start_time) % interval;

0 comments on commit 6342658

Please sign in to comment.