@@ -358,37 +358,47 @@ export class ProcessContainer {
358358 }
359359 }
360360
361+ // src/process-container.ts (inside the ProcessContainer class)
362+
361363 private handleExit ( code : number | null ) {
362364 const wasOnline = this . status === "online" ;
363365 this . status = code === 0 ? "stopped" : "errored" ;
364366 this . pid = undefined ;
365367 this . process = null ;
366-
368+
367369 this . cleanupTimers ( ) ;
368-
370+
369371 const uptime = Date . now ( ) - this . startedAt ;
370-
371- if ( wasOnline && this . config . autorestart && this . restartCount < this . config . maxRestarts ) {
372+
373+ if ( wasOnline && this . config . autorestart ) {
372374 if ( uptime < this . config . minUptime ) {
373375 this . unstableRestarts ++ ;
376+ // Cap consecutive unstable restarts
377+ if ( this . unstableRestarts >= this . config . maxRestarts ) {
378+ console . log ( `[bm2] ${ this . name } reached max consecutive unstable restarts (${ this . config . maxRestarts } ), not restarting` ) ;
379+ this . status = "errored" ;
380+ return ;
381+ }
382+ } else {
383+ // Process survived minUptime: reset the consecutive unstable restart budget
384+ this . unstableRestarts = 0 ;
374385 }
375-
386+
376387 this . status = "waiting-restart" ;
377388 const delay = this . config . restartDelay || 0 ;
378-
389+
379390 this . restartTimer = setTimeout ( ( ) => {
380- this . restartCount ++ ;
381- console . log ( `[bm2] Restarting ${ this . name } (attempt ${ this . restartCount } / ${ this . config . maxRestarts } )` ) ;
391+ this . restartCount ++ ; // Keep cumulative for observability
392+ console . log ( `[bm2] Restarting ${ this . name } (cumulative attempt ${ this . restartCount } )` ) ;
382393 this . start ( ) . catch ( ( err ) => {
383394 console . error ( `[bm2] Failed to restart ${ this . name } :` , err ) ;
384395 } ) ;
385396 } , delay ) ;
386- } else if ( this . restartCount >= this . config . maxRestarts ) {
387- console . log ( `[bm2] ${ this . name } reached max restarts (${ this . config . maxRestarts } ), not restarting` ) ;
388- this . status = "errored" ;
397+ } else if ( ! this . config . autorestart ) {
398+ this . status = "stopped" ;
389399 }
390400 }
391-
401+
392402 private cleanupTimers ( ) {
393403 if ( this . monitorInterval ) {
394404 clearInterval ( this . monitorInterval ) ;
0 commit comments