File tree 3 files changed +33
-0
lines changed 3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ class HandleWrap : public AsyncWrap {
53
53
private:
54
54
friend class Environment ;
55
55
friend void GetActiveHandles (const v8::FunctionCallbackInfo<v8::Value>&);
56
+ friend bool HasUnrefedTimerHandles (Environment* env);
56
57
static void OnClose (uv_handle_t * handle);
57
58
ListNode<HandleWrap> handle_wrap_queue_;
58
59
unsigned int flags_;
Original file line number Diff line number Diff line change @@ -1543,6 +1543,18 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
1543
1543
args.GetReturnValue ().Set (ary);
1544
1544
}
1545
1545
1546
+ bool HasUnrefedTimerHandles (Environment* env) {
1547
+
1548
+ HandleScope handle_scope (env->isolate ());
1549
+
1550
+ for (auto w : *env->handle_wrap_queue ()) {
1551
+ if (!w->persistent ().IsEmpty () && w->flags_ & HandleWrap::kUnref )
1552
+ return true ;
1553
+ }
1554
+
1555
+ return false ;
1556
+ }
1557
+
1546
1558
1547
1559
static void Abort (const FunctionCallbackInfo<Value>& args) {
1548
1560
ABORT ();
@@ -3969,6 +3981,11 @@ static void StartNodeInstance(void* arg) {
3969
3981
v8::platform::PumpMessageLoop (default_platform, isolate);
3970
3982
EmitBeforeExit (env);
3971
3983
3984
+ // We need to run an extra event loop turn to purge unrefed handles.
3985
+ bool unrefedTimers = HasUnrefedTimerHandles (env);
3986
+ if (unrefedTimers == true )
3987
+ uv_run (env->event_loop (), UV_RUN_NOWAIT);
3988
+
3972
3989
// Emit `beforeExit` if the loop became alive either after emitting
3973
3990
// event, or after running some callbacks.
3974
3991
more = uv_loop_alive (env->event_loop ());
Original file line number Diff line number Diff line change
1
+ require ( '../common' ) ;
2
+ const assert = require ( 'assert' ) ;
3
+
4
+ var once = 0 ;
5
+
6
+ process . on ( 'beforeExit' , ( ) => {
7
+ setTimeout ( ( ) => { } , 1 ) . unref ( ) ;
8
+ once ++ ;
9
+ } ) ;
10
+
11
+ process . on ( 'exit' , ( code ) => {
12
+ if ( code !== 0 ) return ;
13
+
14
+ assert . strictEqual ( once , 1 ) ;
15
+ } ) ;
You can’t perform that action at this time.
0 commit comments