Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
contextify: fix ContextifyContext leak
Browse files Browse the repository at this point in the history
Apparently, context->Global() won't be destroyed if the context itself
isn't marked as weak and independent.

Also, the weakness flag should be cleared once the weak callback is
executed, otherwise we'll get crashes in Debug builds.

fix #6115 and #6201
  • Loading branch information
indutny committed Sep 14, 2013
1 parent f31037d commit 59a075e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ class ContextifyContext {
, sandbox_(env->isolate(), sandbox)
, context_(env->isolate(), CreateV8Context(env))
, proxy_global_(env->isolate(), context()->Global())
// Wait for both sandbox_'s and proxy_global_'s death
, references_(2) {
sandbox_.MakeWeak(this, SandboxFreeCallback);
// Wait for sandbox_, proxy_global_, and context_ to die
, references_(3) {
sandbox_.MakeWeak(this, WeakCallback);
sandbox_.MarkIndependent();
proxy_global_.MakeWeak(this, SandboxFreeCallback);
context_.MakeWeak(this, WeakCallback);
context_.MarkIndependent();
proxy_global_.MakeWeak(this, WeakCallback);
proxy_global_.MarkIndependent();
}

Expand Down Expand Up @@ -175,9 +177,11 @@ class ContextifyContext {
}


static void SandboxFreeCallback(Isolate* isolate,
Persistent<Object>* target,
ContextifyContext* context) {
template <class T>
static void WeakCallback(Isolate* isolate,
Persistent<T>* target,
ContextifyContext* context) {
target->ClearWeak();
if (--context->references_ == 0)
delete context;
}
Expand Down
7 changes: 7 additions & 0 deletions test/pummel/test-vm-memleak.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ var interval = setInterval(function() {
if (Date.now() - start > 5 * 1000) {
// wait 10 seconds.
clearInterval(interval);

testContextLeak();
}
}, 1);

function testContextLeak() {
for (var i = 0; i < 1000; i++)
require('vm').createContext({});
}

process.on('exit', function() {
console.error('max mem: %dmb', Math.round(maxMem / (1024 * 1024)));
// make sure we stay below 100mb
Expand Down

0 comments on commit 59a075e

Please sign in to comment.