@@ -209,21 +209,23 @@ Timeout.prototype.refresh = function() {
209209Timeout . prototype . unref = function ( ) {
210210 if ( this [ kRefed ] ) {
211211 this [ kRefed ] = false ;
212- decRefCount ( ) ;
212+ if ( ! this . _destroyed )
213+ decRefCount ( ) ;
213214 }
214215 return this ;
215216} ;
216217
217218Timeout . prototype . ref = function ( ) {
218- if ( this [ kRefed ] === false ) {
219+ if ( ! this [ kRefed ] ) {
219220 this [ kRefed ] = true ;
220- incRefCount ( ) ;
221+ if ( ! this . _destroyed )
222+ incRefCount ( ) ;
221223 }
222224 return this ;
223225} ;
224226
225227Timeout . prototype . hasRef = function ( ) {
226- return ! ! this [ kRefed ] ;
228+ return this [ kRefed ] ;
227229} ;
228230
229231function TimersList ( expiry , msecs ) {
@@ -317,12 +319,16 @@ function insertGuarded(item, refed, start) {
317319
318320 insert ( item , msecs , start ) ;
319321
320- if ( ! item [ async_id_symbol ] || item . _destroyed ) {
322+ const isDestroyed = item . _destroyed ;
323+ if ( isDestroyed || ! item [ async_id_symbol ] ) {
321324 item . _destroyed = false ;
322325 initAsyncResource ( item , 'Timeout' ) ;
323326 }
324327
325- if ( refed === ! item [ kRefed ] ) {
328+ if ( isDestroyed ) {
329+ if ( refed )
330+ incRefCount ( ) ;
331+ } else if ( refed === ! item [ kRefed ] ) {
326332 if ( refed )
327333 incRefCount ( ) ;
328334 else
@@ -519,13 +525,14 @@ function getTimerCallbacks(runNextTicks) {
519525 const asyncId = timer [ async_id_symbol ] ;
520526
521527 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 ) {
528529 timer . _destroyed = true ;
530+
531+ if ( timer [ kRefed ] )
532+ refCount -- ;
533+
534+ if ( destroyHooksExist ( ) )
535+ emitDestroy ( asyncId ) ;
529536 }
530537 continue ;
531538 }
@@ -546,15 +553,14 @@ function getTimerCallbacks(runNextTicks) {
546553 if ( timer . _repeat && timer . _idleTimeout !== - 1 ) {
547554 timer . _idleTimeout = timer . _repeat ;
548555 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+
550559 if ( timer [ kRefed ] )
551560 refCount -- ;
552- timer [ kRefed ] = null ;
553561
554- if ( destroyHooksExist ( ) && ! timer . _destroyed ) {
562+ if ( destroyHooksExist ( ) )
555563 emitDestroy ( asyncId ) ;
556- }
557- timer . _destroyed = true ;
558564 }
559565 }
560566
0 commit comments