@@ -1129,7 +1129,7 @@ public function afterScenario( AfterScenarioScope $scope ): void {
11291129 $ is_ci = getenv ( 'CI ' );
11301130 $ is_debug = getenv ( 'WP_CLI_TEST_DEBUG_BEHAT_ENV ' );
11311131 if ( $ scope ->getTestResult ()->getResultCode () <= 10 || ( $ is_ci && ! $ is_debug ) ) {
1132- self ::remove_dir ( self ::$ run_dir );
1132+ self ::remove_dir_in_background ( self ::$ run_dir );
11331133 }
11341134 self ::$ run_dir = null ;
11351135 }
@@ -1141,7 +1141,7 @@ public function afterScenario( AfterScenarioScope $scope ): void {
11411141
11421142 // Remove SUITE_CACHE_DIR if any.
11431143 if ( self ::$ suite_cache_dir ) {
1144- self ::remove_dir ( self ::$ suite_cache_dir );
1144+ self ::remove_dir_in_background ( self ::$ suite_cache_dir );
11451145 self ::$ suite_cache_dir = null ;
11461146 }
11471147
@@ -1850,13 +1850,20 @@ public function background_proc( $cmd ): void {
18501850 ];
18511851 }
18521852
1853+ // If something else is already listening on the port, the process failing to bind to it
1854+ // would go unnoticed when waiting for the port, so fall back to waiting the full second.
1855+ $ port = self ::get_listening_port ( $ cmd );
1856+ if ( null !== $ port && self ::is_port_open ( $ port ) ) {
1857+ $ port = null ;
1858+ }
1859+
18531860 $ proc = proc_open ( $ cmd , $ descriptors , $ pipes , $ this ->variables ['RUN_DIR ' ], self ::get_process_env_variables () );
18541861
18551862 if ( Utils \is_windows () ) {
18561863 fclose ( $ pipes [0 ] );
18571864 }
18581865
1859- sleep ( 1 );
1866+ self :: wait_for_background_proc ( $ proc , $ port );
18601867
18611868 $ status = proc_get_status ( $ proc );
18621869
@@ -1881,6 +1888,61 @@ public function background_proc( $cmd ): void {
18811888 }
18821889 }
18831890
1891+ /**
1892+ * Give a background process up to a second to start.
1893+ *
1894+ * Returns early once the process accepts connections on the given port.
1895+ *
1896+ * @param resource $proc
1897+ * @param int|null $port Port the process is expected to listen on, if known.
1898+ */
1899+ private static function wait_for_background_proc ( $ proc , $ port ): void {
1900+ $ deadline = microtime ( true ) + 1 ;
1901+
1902+ while ( microtime ( true ) < $ deadline ) {
1903+ $ status = proc_get_status ( $ proc );
1904+
1905+ if ( ! $ status ['running ' ] || ( null !== $ port && self ::is_port_open ( $ port ) ) ) {
1906+ return ;
1907+ }
1908+
1909+ usleep ( 50000 );
1910+ }
1911+ }
1912+
1913+ /**
1914+ * Get the local port a background command is going to listen on, if it can be told from the command.
1915+ *
1916+ * @param string $cmd
1917+ * @return int|null
1918+ */
1919+ private static function get_listening_port ( $ cmd ): ?int {
1920+ if ( preg_match ( '/(?:localhost|127\.0\.0\.1):(\d+)/ ' , $ cmd , $ matches ) || preg_match ( '/--port[= ](\d+)/ ' , $ cmd , $ matches ) ) {
1921+ return (int ) $ matches [1 ];
1922+ }
1923+
1924+ return null ;
1925+ }
1926+
1927+ /**
1928+ * Whether a local port accepts connections.
1929+ *
1930+ * @param int $port
1931+ * @return bool
1932+ */
1933+ private static function is_port_open ( $ port ): bool {
1934+ // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- A refused connection is the expected answer for a closed port.
1935+ $ connection = @fsockopen ( 'localhost ' , $ port , $ errno , $ errstr , 0.1 );
1936+
1937+ if ( false === $ connection ) {
1938+ return false ;
1939+ }
1940+
1941+ fclose ( $ connection );
1942+
1943+ return true ;
1944+ }
1945+
18841946 /**
18851947 * Clean up temporary files safely.
18861948 *
@@ -1937,6 +1999,22 @@ public static function remove_dir( $dir ): void {
19371999 @rmdir ( $ dir );
19382000 }
19392001
2002+ /**
2003+ * Remove a directory without waiting for it to be gone.
2004+ *
2005+ * Only meant for directories with a unique name, which no later scenario is going to use.
2006+ *
2007+ * @param string $dir
2008+ */
2009+ private static function remove_dir_in_background ( $ dir ): void {
2010+ if ( Utils \is_windows () || ! is_dir ( $ dir ) ) {
2011+ self ::remove_dir ( $ dir );
2012+ return ;
2013+ }
2014+
2015+ exec ( 'rm -rf ' . escapeshellarg ( $ dir ) . ' > /dev/null 2>&1 & ' );
2016+ }
2017+
19402018 /**
19412019 * Copy a directory (recursive). Destination directory must exist.
19422020 *
0 commit comments