Skip to content

Instantly share code, notes, and snippets.

@tjfontaine
Created November 12, 2013 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjfontaine/fb0722bf620cf49a269e to your computer and use it in GitHub Desktop.
Save tjfontaine/fb0722bf620cf49a269e to your computer and use it in GitHub Desktop.
From 16934d9210546bf19d4af8d98652aa5d636ce693 Mon Sep 17 00:00:00 2001
From: Timothy J Fontaine <tjfontaine@gmail.com>
Date: Tue, 12 Nov 2013 11:23:19 -0800
Subject: [PATCH] src: add HandleScope in HandleWrap::OnClose
Fixes a 4 byte leak on handles closing. AKA The Walmart leak.
MakeCallback doesn't have a HandleScope. That means the callers scope
will retain ownership of created handles from MakeCallback and related.
There is by default a wrapping HandleScope before uv_run, if the caller
doesn't have a HandleScope on the stack the global will take ownership
which won't be reaped until the uv loop exits.
If a uv callback is fired, and there is no enclosing HandleScope in the
cb, you will appear to leak 4-bytes for every invocation. Take heed.
cc @hueniverse
---
src/handle_wrap.cc | 2 ++
src/node.h | 11 +++++++++++
2 files changed, 13 insertions(+)
diff --git a/src/handle_wrap.cc b/src/handle_wrap.cc
index a63421b..c70b786 100644
--- a/src/handle_wrap.cc
+++ b/src/handle_wrap.cc
@@ -134,6 +134,8 @@ HandleWrap::~HandleWrap() {
void HandleWrap::OnClose(uv_handle_t* handle) {
+ HandleScope scope;
+
HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);
// The wrap object should still be there.
diff --git a/src/node.h b/src/node.h
index 574a93a..b2c8fc4 100644
--- a/src/node.h
+++ b/src/node.h
@@ -238,6 +238,17 @@ node_module_struct* get_builtin_module(const char *name);
*/
NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
+/*
+ * MakeCallback doesn't have a HandleScope. That means the callers scope
+ * will retain ownership of created handles from MakeCallback and related.
+ * There is by default a wrapping HandleScope before uv_run, if the caller
+ * doesn't have a HandleScope on the stack the global will take ownership
+ * which won't be reaped until the uv loop exits.
+ *
+ * If a uv callback is fired, and there is no enclosing HandleScope in the
+ * cb, you will appear to leak 4-bytes for every invocation. Take heed.
+ */
+
NODE_EXTERN void SetErrno(uv_err_t err);
NODE_EXTERN v8::Handle<v8::Value>
MakeCallback(const v8::Handle<v8::Object> object,
--
1.8.3.4 (Apple Git-47)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment