Print this page
joyent/v8plus#7 v8plus should not hold the event loop open forever

@@ -521,10 +521,16 @@
                 pfh = v8::Persistent<v8::Function>::New(it->second.ch_hdl);
                 it->second.ch_hdl = pfh;
                 it->second.ch_persist = _B_TRUE;
         }
         ++it->second.ch_refs;
+
+        /*
+         * If the consumer puts a hold on a callback, we should also put a hold
+         * on the V8 event loop to prevent it dematerialising beneath us.
+         */
+        v8plus_eventloop_hold();
 }
 
 extern "C" void
 v8plus_jsfunc_rele_direct(v8plus_jsfunc_t f)
 {

@@ -542,10 +548,15 @@
                         v8::Persistent<v8::Function> pfh(it->second.ch_hdl);
                         pfh.Dispose();
                 }
                 cbhash.erase(it);
         }
+
+        /*
+         * Release the event loop hold we took in v8plus_jsfunc_hold():
+         */
+        v8plus_eventloop_rele_direct();
 }
 
 static size_t
 library_name(const char *base, const char *version, char *buf, size_t len)
 {

@@ -635,13 +646,24 @@
 extern "C" void
 v8plus_obj_hold(const void *cop)
 {
         v8plus::ObjectWrap *op = v8plus::ObjectWrap::objlookup(cop);
         op->public_Ref();
+
+        /*
+         * If the consumer puts a hold on an object, we should also put a hold
+         * on the V8 event loop to prevent it dematerialising beneath us.
+         */
+        v8plus_eventloop_hold();
 }
 
 extern "C" void
 v8plus_obj_rele_direct(const void *cop)
 {
         v8plus::ObjectWrap *op = v8plus::ObjectWrap::objlookup(cop);
         op->public_Unref();
+
+        /*
+         * Release the event loop hold we took in v8plus_obj_hold():
+         */
+        v8plus_eventloop_rele_direct();
 }