@@ -172,65 +172,65 @@ exports.deathQueueGenerator = function(options){
172172 //retry of the 'die' process
173173 die ( retry - 1 ) ;
174174
175- } , wait ) ;
175+ } , wait ) ,
176+ onSuccessorListening = function onSuccessorListening ( onboard ) {
176177
177- //when successor is in place, the old worker could be discontinued finally
178- emitter . on ( 'listening' , function onSuccessorListening ( onboard ) {
178+ if ( successorPid !== onboard ) {
179+ return ; //noop
180+ }
181+ else {
182+ emitter . removeListener ( 'listening' , onSuccessorListening ) ;
183+ }
179184
180- if ( successorPid !== onboard ) {
181- return ; //noop
182- }
183- else {
184- emitter . removeListener ( 'listening' , onSuccessorListening ) ;
185- }
185+ clearTimeout ( successorGuard ) ;
186+ logger . debug ( '[deathQueue] successor:%d of %d is ready, wait for %s and timeout in:%dms' , successorPid , pid , death , wait ) ;
186187
187- clearTimeout ( successorGuard ) ;
188- logger . debug ( '[deathQueue] successor:%d of %d is ready, wait for %s and timeout in:%dms' , successorPid , pid , death , wait ) ;
188+ var onDeath = function onDeath ( dismiss ) {
189189
190- var deathGuard = setTimeout ( function ( ) {
190+ if ( pid !== dismiss ) {
191+ return ;
192+ }
193+ else {
194+ emitter . removeListener ( 'died' , onDeath ) ;
195+ }
191196
192- if ( ! exports . safeKill ( pid , 'SIGTERM' , logger ) ) {
193- //worker still there, should emit 'exit' eventually
194- logger . debug ( '[deathQueue] worker:%d did not report death by:%d, kill by SIGTERM' , pid , wait ) ;
195- //remove the redundant exit listener
196- emitter . removeListener ( 'died' , onDeath ) ;
197- }
198- else { //suicide or accident already happended, process has run away
199- //we emit this from master on behalf of the run away process.
200- logger . debug ( '[deathQueue] worker:%d probably ran away, emit:%s on behalf' , death ) ;
201- //immediately report death to the master
202- emitter . emit ( 'died' , pid ) ;
203- }
197+ logger . debug ( '[deathQueue] %d died' , pid ) ;
204198
205- } , wait ) ;
199+ clearTimeout ( deathGuard ) ; //release the deathGuard
200+
201+ tillDeath . resolve ( pid ) ;
206202
207- worker . kill ( 'SIGINT' ) ;
208-
209- emitter . on ( 'died' , function onDeath ( dismiss ) {
203+ if ( tillPrevDeath === afterDeath ) { //last of dyingQueue resolved, clean up the dyingQueue
210204
211- if ( pid !== dismiss ) {
212- return ;
213- }
214- else {
215- emitter . removeListener ( 'died' , onDeath ) ;
216- }
205+ logger . debug ( '[deathQueue] death queue cleaned up' ) ;
217206
218- logger . debug ( '[deathQueue] %d died' , pid ) ;
207+ tillPrevDeath = null ;
208+
209+ queue = [ ] ;
210+ }
211+ } ,
212+ deathGuard = setTimeout ( function ( ) {
219213
220- clearTimeout ( deathGuard ) ; //release the deathGuard
221-
222- tillDeath . resolve ( pid ) ;
214+ if ( ! exports . safeKill ( pid , 'SIGTERM' , logger ) ) {
215+ //worker still there, should emit 'exit' eventually
216+ logger . debug ( '[deathQueue] worker:%d did not report death by:%d, kill by SIGTERM' , pid , wait ) ;
217+ }
218+ else { //suicide or accident already happended, process has run away
219+ //we emit this from master on behalf of the run away process.
220+ logger . debug ( '[deathQueue] worker:%d probably ran away, emit:%s on behalf' , death ) ;
221+ //immediately report death to the master
222+ emitter . emit ( 'died' , pid ) ;
223+ }
223224
224- if ( tillPrevDeath === afterDeath ) { //last of dyingQueue resolved, clean up the dyingQueue
225+ } , wait ) ;
225226
226- logger . debug ( '[deathQueue] death queue cleaned up' ) ;
227+ worker . kill ( 'SIGINT' ) ;
228+
229+ emitter . on ( 'died' , onDeath ) ;
230+ } ;
227231
228- tillPrevDeath = null ;
229-
230- queue . splice ( 0 , queue . length ) ;
231- }
232- } ) ;
233- } ) ;
232+ //when successor is in place, the old worker could be discontinued finally
233+ emitter . on ( 'listening' , onSuccessorListening ) ;
234234 } ;
235235
236236 if ( ! tillPrevDeath ) { //1st in the dying queue,
0 commit comments