Skip to content

Commit 16934d9

Browse files
committed
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
1 parent ac799ba commit 16934d9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/handle_wrap.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ HandleWrap::~HandleWrap() {
134134

135135

136136
void HandleWrap::OnClose(uv_handle_t* handle) {
137+
HandleScope scope;
138+
137139
HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);
138140

139141
// The wrap object should still be there.

src/node.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ node_module_struct* get_builtin_module(const char *name);
238238
*/
239239
NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
240240

241+
/*
242+
* MakeCallback doesn't have a HandleScope. That means the callers scope
243+
* will retain ownership of created handles from MakeCallback and related.
244+
* There is by default a wrapping HandleScope before uv_run, if the caller
245+
* doesn't have a HandleScope on the stack the global will take ownership
246+
* which won't be reaped until the uv loop exits.
247+
*
248+
* If a uv callback is fired, and there is no enclosing HandleScope in the
249+
* cb, you will appear to leak 4-bytes for every invocation. Take heed.
250+
*/
251+
241252
NODE_EXTERN void SetErrno(uv_err_t err);
242253
NODE_EXTERN v8::Handle<v8::Value>
243254
MakeCallback(const v8::Handle<v8::Object> object,

0 commit comments

Comments
 (0)