Skip to content

Commit 4e69782

Browse files
schlesseraclaude
andcommitted
Remove the Docker containers of runs that were stopped or killed
A run removed its containers in shutdown functions, which PHP skips when a signal ends it, so Ctrl+C or a timeout left the database containers running. Exit through the signal handlers when the pcntl extension is available, so the shutdown functions run. Label the containers with the host and process of their run, and remove those of runs on this host whose process is gone before starting new ones, which covers runs that were killed outright. Containers of runs that are still going, of other hosts and those without the label are left alone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 035668f commit 4e69782

2 files changed

Lines changed: 96 additions & 4 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ Docker is not used on Windows, when collecting code coverage, when Docker is not
280280

281281
Each worker gets its own network, temporary directory and database server, of the same type and version as the one configured through the `WP_CLI_TEST_DB*` variables (or set the image through `WP_CLI_TEST_DOCKER_DB`, e.g. `mysql:8.0`, so that the host needs neither a database server nor a client). Nothing is shared between workers, so they can run any scenario. The database servers keep their data in memory. Scenarios selected by path or name run in a single container.
282282

283+
The containers are removed when the run ends, also when it is stopped with Ctrl+C or a termination signal (with the `pcntl` extension). Containers of a run that was killed outright are removed by the next run on the same host, which only removes those whose run is no longer going.
284+
283285
The containers run the same PHP version as the host, unless `WP_CLI_TEST_DOCKER_PHP` sets another one:
284286
```bash
285287
WP_CLI_TEST_DOCKER=1 WP_CLI_TEST_DOCKER_PHP=7.4 WP_CLI_TEST_PARALLEL=4 composer behat

‎utils/behat-parallel.php‎

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,65 @@ function wp_cli_tests_docker_mounts( $base_dir, $vendor = null ) {
451451
return $mounts;
452452
}
453453

454+
/**
455+
* Get the label value that marks the Docker containers of this run, by host and process.
456+
*
457+
* @return string
458+
*/
459+
function wp_cli_tests_docker_owner() {
460+
return php_uname( 'n' ) . ':' . getmypid();
461+
}
462+
463+
/**
464+
* Get whether a process still runs.
465+
*
466+
* @param int $pid
467+
* @return bool|null Null if that cannot be told.
468+
*/
469+
function wp_cli_tests_process_exists( $pid ) {
470+
if ( function_exists( 'posix_kill' ) ) {
471+
// A process of another user cannot be signalled, but exists.
472+
return posix_kill( $pid, 0 ) || ( function_exists( 'posix_get_last_error' ) && 1 === posix_get_last_error() );
473+
}
474+
475+
if ( is_dir( '/proc/self' ) ) {
476+
return is_dir( "/proc/{$pid}" );
477+
}
478+
479+
return null;
480+
}
481+
482+
/**
483+
* Remove the containers that earlier runs on this host left behind, such as when they were killed.
484+
*
485+
* Only containers labelled by a run whose process is gone are removed, so runs that are still
486+
* going keep theirs, as do containers this script did not start.
487+
*/
488+
function wp_cli_tests_docker_remove_abandoned() {
489+
exec( 'docker ps -a --filter label=wp-cli-tests.owner --format ' . escapeshellarg( '{{.ID}} {{.Label "wp-cli-tests.owner"}}' ) . ' 2>/dev/null', $lines, $exit_code );
490+
if ( 0 !== $exit_code ) {
491+
return;
492+
}
493+
494+
$host = php_uname( 'n' );
495+
$abandoned = [];
496+
foreach ( $lines as $line ) {
497+
$parts = explode( ' ', trim( $line ), 2 );
498+
if ( 2 !== count( $parts ) || ! preg_match( '/^(.+):(\d+)$/', $parts[1], $matches ) || $host !== $matches[1] || getmypid() === (int) $matches[2] ) {
499+
continue;
500+
}
501+
502+
if ( false === wp_cli_tests_process_exists( (int) $matches[2] ) ) {
503+
$abandoned[] = $parts[0];
504+
}
505+
}
506+
507+
if ( $abandoned ) {
508+
exec( 'docker rm -f ' . implode( ' ', array_map( 'escapeshellarg', $abandoned ) ) . ' > /dev/null 2>&1' );
509+
fwrite( STDERR, sprintf( "Removed %d Docker containers that earlier runs left behind.\n", count( $abandoned ) ) );
510+
}
511+
}
512+
454513
/**
455514
* Start the containers of a worker and get the command that runs its scenarios.
456515
*
@@ -464,10 +523,11 @@ function wp_cli_tests_docker_mounts( $base_dir, $vendor = null ) {
464523
*/
465524
function wp_cli_tests_docker_worker_command( $name, $php_image, $db_image, array $mounts, array $user_args, $scenario_list ) {
466525
$network = '';
526+
$label = ' --label ' . escapeshellarg( 'wp-cli-tests.owner=' . wp_cli_tests_docker_owner() );
467527

468528
if ( null !== $db_image ) {
469529
exec(
470-
'docker run -d --rm --name ' . escapeshellarg( "{$name}-db" )
530+
'docker run -d --rm --name ' . escapeshellarg( "{$name}-db" ) . $label
471531
. ' --tmpfs /var/lib/mysql'
472532
. ' -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=wp_cli_test -e MYSQL_PASSWORD=password1'
473533
. ' ' . escapeshellarg( $db_image ) . ' ' . wp_cli_tests_docker_db_options( $db_image ) . ' 2>&1'
@@ -489,7 +549,7 @@ function wp_cli_tests_docker_worker_command( $name, $php_image, $db_image, array
489549
}
490550
}
491551

492-
$command = 'docker run --rm --name ' . escapeshellarg( $name ) . $network
552+
$command = 'docker run --rm --name ' . escapeshellarg( $name ) . $label . $network
493553
. ' --tmpfs /tmp:exec,mode=1777'
494554
. ' -u ' . escapeshellarg( getmyuid() . ':' . getmygid() )
495555
. ' -w ' . escapeshellarg( (string) getcwd() );
@@ -596,6 +656,28 @@ function wp_cli_tests_docker_worker_command( $name, $php_image, $db_image, array
596656
}
597657
}
598658

659+
if ( $docker ) {
660+
wp_cli_tests_docker_remove_abandoned();
661+
662+
// Ending the run through exit() on Ctrl+C or a termination signal removes its containers.
663+
if ( function_exists( 'pcntl_async_signals' ) ) {
664+
pcntl_async_signals( true );
665+
$exit_codes = [
666+
SIGINT => 130,
667+
SIGTERM => 143,
668+
SIGHUP => 129,
669+
];
670+
foreach ( $exit_codes as $signal => $code ) {
671+
pcntl_signal(
672+
$signal,
673+
static function () use ( $code ) {
674+
exit( $code );
675+
}
676+
);
677+
}
678+
}
679+
}
680+
599681
// Kept between runs, so that the workers' caches stay warm and failed scenarios can be inspected.
600682
$base_dir = sys_get_temp_dir() . '/wp-cli-test-parallel-' . substr( md5( (string) getcwd() ), 0, 8 );
601683

@@ -610,8 +692,16 @@ static function () use ( $name ) {
610692
exec( 'docker rm -f ' . escapeshellarg( $name ) . ' ' . escapeshellarg( "{$name}-db" ) . ' > /dev/null 2>&1' );
611693
}
612694
);
613-
passthru( wp_cli_tests_docker_worker_command( $name, (string) $php_image, $db_image, wp_cli_tests_docker_mounts( $base_dir, $vendor ), $user_args, null ), $exit_code );
614-
exit( $exit_code );
695+
// Waiting in PHP rather than in passthru() lets the signal handlers run.
696+
$proc = proc_open( wp_cli_tests_docker_worker_command( $name, (string) $php_image, $db_image, wp_cli_tests_docker_mounts( $base_dir, $vendor ), $user_args, null ), [ STDIN, STDOUT, STDERR ], $pipes );
697+
if ( ! is_resource( $proc ) ) {
698+
exit( 1 );
699+
}
700+
do {
701+
usleep( 200000 );
702+
$status = proc_get_status( $proc );
703+
} while ( $status['running'] );
704+
exit( $status['exitcode'] );
615705
}
616706

617707
require getcwd() . '/vendor/autoload.php';

0 commit comments

Comments
 (0)