@@ -209,21 +209,23 @@ Timeout.prototype.refresh = function() {
209
209
Timeout . prototype . unref = function ( ) {
210
210
if ( this [ kRefed ] ) {
211
211
this [ kRefed ] = false ;
212
- decRefCount ( ) ;
212
+ if ( ! this . _destroyed )
213
+ decRefCount ( ) ;
213
214
}
214
215
return this ;
215
216
} ;
216
217
217
218
Timeout . prototype . ref = function ( ) {
218
- if ( this [ kRefed ] === false ) {
219
+ if ( ! this [ kRefed ] ) {
219
220
this [ kRefed ] = true ;
220
- incRefCount ( ) ;
221
+ if ( ! this . _destroyed )
222
+ incRefCount ( ) ;
221
223
}
222
224
return this ;
223
225
} ;
224
226
225
227
Timeout . prototype . hasRef = function ( ) {
226
- return ! ! this [ kRefed ] ;
228
+ return this [ kRefed ] ;
227
229
} ;
228
230
229
231
function TimersList ( expiry , msecs ) {
@@ -317,12 +319,16 @@ function insertGuarded(item, refed, start) {
317
319
318
320
insert ( item , msecs , start ) ;
319
321
320
- if ( ! item [ async_id_symbol ] || item . _destroyed ) {
322
+ const isDestroyed = item . _destroyed ;
323
+ if ( isDestroyed || ! item [ async_id_symbol ] ) {
321
324
item . _destroyed = false ;
322
325
initAsyncResource ( item , 'Timeout' ) ;
323
326
}
324
327
325
- if ( refed === ! item [ kRefed ] ) {
328
+ if ( isDestroyed ) {
329
+ if ( refed )
330
+ incRefCount ( ) ;
331
+ } else if ( refed === ! item [ kRefed ] ) {
326
332
if ( refed )
327
333
incRefCount ( ) ;
328
334
else
@@ -519,13 +525,14 @@ function getTimerCallbacks(runNextTicks) {
519
525
const asyncId = timer [ async_id_symbol ] ;
520
526
521
527
if ( ! timer . _onTimeout ) {
522
- if ( timer [ kRefed ] )
523
- refCount -- ;
524
- timer [ kRefed ] = null ;
525
-
526
- if ( destroyHooksExist ( ) && ! timer . _destroyed ) {
527
- emitDestroy ( asyncId ) ;
528
+ if ( ! timer . _destroyed ) {
528
529
timer . _destroyed = true ;
530
+
531
+ if ( timer [ kRefed ] )
532
+ refCount -- ;
533
+
534
+ if ( destroyHooksExist ( ) )
535
+ emitDestroy ( asyncId ) ;
529
536
}
530
537
continue ;
531
538
}
@@ -546,15 +553,14 @@ function getTimerCallbacks(runNextTicks) {
546
553
if ( timer . _repeat && timer . _idleTimeout !== - 1 ) {
547
554
timer . _idleTimeout = timer . _repeat ;
548
555
insert ( timer , timer . _idleTimeout , start ) ;
549
- } else if ( ! timer . _idleNext && ! timer . _idlePrev ) {
556
+ } else if ( ! timer . _idleNext && ! timer . _idlePrev && ! timer . _destroyed ) {
557
+ timer . _destroyed = true ;
558
+
550
559
if ( timer [ kRefed ] )
551
560
refCount -- ;
552
- timer [ kRefed ] = null ;
553
561
554
- if ( destroyHooksExist ( ) && ! timer . _destroyed ) {
562
+ if ( destroyHooksExist ( ) )
555
563
emitDestroy ( asyncId ) ;
556
- }
557
- timer . _destroyed = true ;
558
564
}
559
565
}
560
566
0 commit comments