contextify: fix ContextifyContext leak

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
This commit is contained in:
Fedor Indutny 2013-09-14 18:29:24 +04:00
parent f31037ddfe
commit 59a075e108
2 changed files with 18 additions and 7 deletions

View File

@ -66,11 +66,13 @@ class ContextifyContext {
, sandbox_(env->isolate(), sandbox) , sandbox_(env->isolate(), sandbox)
, context_(env->isolate(), CreateV8Context(env)) , context_(env->isolate(), CreateV8Context(env))
, proxy_global_(env->isolate(), context()->Global()) , proxy_global_(env->isolate(), context()->Global())
// Wait for both sandbox_'s and proxy_global_'s death // Wait for sandbox_, proxy_global_, and context_ to die
, references_(2) { , references_(3) {
sandbox_.MakeWeak(this, SandboxFreeCallback); sandbox_.MakeWeak(this, WeakCallback);
sandbox_.MarkIndependent(); sandbox_.MarkIndependent();
proxy_global_.MakeWeak(this, SandboxFreeCallback); context_.MakeWeak(this, WeakCallback);
context_.MarkIndependent();
proxy_global_.MakeWeak(this, WeakCallback);
proxy_global_.MarkIndependent(); proxy_global_.MarkIndependent();
} }
@ -175,9 +177,11 @@ class ContextifyContext {
} }
static void SandboxFreeCallback(Isolate* isolate, template <class T>
Persistent<Object>* target, static void WeakCallback(Isolate* isolate,
ContextifyContext* context) { Persistent<T>* target,
ContextifyContext* context) {
target->ClearWeak();
if (--context->references_ == 0) if (--context->references_ == 0)
delete context; delete context;
} }

View File

@ -38,9 +38,16 @@ var interval = setInterval(function() {
if (Date.now() - start > 5 * 1000) { if (Date.now() - start > 5 * 1000) {
// wait 10 seconds. // wait 10 seconds.
clearInterval(interval); clearInterval(interval);
testContextLeak();
} }
}, 1); }, 1);
function testContextLeak() {
for (var i = 0; i < 1000; i++)
require('vm').createContext({});
}
process.on('exit', function() { process.on('exit', function() {
console.error('max mem: %dmb', Math.round(maxMem / (1024 * 1024))); console.error('max mem: %dmb', Math.round(maxMem / (1024 * 1024)));
// make sure we stay below 100mb // make sure we stay below 100mb