Skip to content

Commit 2a4f6b5

Browse files
swissspidyclaude
andcommitted
Stop the jobs and clean up when the display is signalled
On SIGINT or SIGTERM, peek.php exited right away, leaving the xargs driver and its clone or pull jobs running and the socket directory behind. It now signals the driver and every process below it, kills what is left after two seconds, and removes the socket and temp directory before exiting with 128 + the signal number. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5n8EwiVkC4y4eEWszr
1 parent 2dae208 commit 2a4f6b5

1 file changed

Lines changed: 58 additions & 2 deletions

File tree

‎.maintenance/peek.php‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,52 @@ function peek_drain( $srv, PeekDisplay $disp ) {
499499
}
500500
}
501501

502+
/**
503+
* Send a signal to a process and every process below it, then wait briefly
504+
* for the process to exit and kill whatever is left.
505+
*/
506+
function peek_kill_tree( $proc, $signo ) {
507+
$status = proc_get_status( $proc );
508+
if ( ! $status['running'] ) {
509+
return;
510+
}
511+
$pids = array( (int) $status['pid'] );
512+
$children = array();
513+
@exec( 'ps -A -o pid= -o ppid= 2>/dev/null', $rows );
514+
foreach ( (array) $rows as $row ) {
515+
$cols = preg_split( '/\s+/', trim( $row ) );
516+
if ( 2 === count( $cols ) ) {
517+
$children[ (int) $cols[1] ][] = (int) $cols[0];
518+
}
519+
}
520+
$queue = $pids;
521+
while ( $queue ) {
522+
$pid = array_shift( $queue );
523+
if ( isset( $children[ $pid ] ) ) {
524+
$pids = array_merge( $pids, $children[ $pid ] );
525+
$queue = array_merge( $queue, $children[ $pid ] );
526+
}
527+
}
528+
$send = function ( $sig ) use ( $pids ) {
529+
if ( function_exists( 'posix_kill' ) ) {
530+
foreach ( $pids as $pid ) {
531+
@posix_kill( $pid, $sig );
532+
}
533+
} else {
534+
@exec( 'kill -' . (int) $sig . ' ' . implode( ' ', $pids ) . ' 2>/dev/null' );
535+
}
536+
};
537+
$send( $signo );
538+
for ( $i = 0; $i < 20; $i++ ) {
539+
$status = proc_get_status( $proc );
540+
if ( ! $status['running'] ) {
541+
return;
542+
}
543+
usleep( 100000 );
544+
}
545+
$send( 9 );
546+
}
547+
502548
function peek_cmd_serve( array $argv ) {
503549
$peek_lines = 6;
504550
$fps = 12.5;
@@ -583,9 +629,19 @@ function peek_cmd_serve( array $argv ) {
583629
register_shutdown_function( $restore );
584630
if ( function_exists( 'pcntl_async_signals' ) ) {
585631
pcntl_async_signals( true );
586-
$on_signal = function () use ( $restore ) {
632+
// Stop the jobs and remove the socket before exiting, so a cancelled
633+
// run leaves no clone or pull running and no temp directory behind.
634+
$on_signal = function ( $signo ) use ( $proc, $pipes, $srv, $sock_path, $tmp, $restore ) {
635+
peek_kill_tree( $proc, $signo );
636+
@fclose( $pipes[1] );
637+
if ( ! proc_get_status( $proc )['running'] ) {
638+
proc_close( $proc );
639+
}
640+
@fclose( $srv );
641+
@unlink( $sock_path );
642+
@rmdir( $tmp );
587643
$restore();
588-
exit( 130 );
644+
exit( 128 + $signo );
589645
};
590646
pcntl_signal( SIGINT, $on_signal );
591647
pcntl_signal( SIGTERM, $on_signal );

0 commit comments

Comments
 (0)