@@ -125,36 +125,37 @@ public function run(): bool
125125
126126 $ threads = range (1 , $ this ->threadCount );
127127
128- $ this ->installInterruptHandler ();
129128 $ async = $ this ->threadCount > 1 && count ($ this ->jobs ) > 1 ;
130129
131- while (($ this ->jobs || $ running ) && !$ this ->isInterrupted ()) {
132- while ($ threads && $ this ->jobs ) {
133- $ running [] = $ job = array_shift ($ this ->jobs );
134- $ job ->setEnvironmentVariable (Environment::THREAD , (string ) array_shift ($ threads ));
135- $ job ->run ($ async ? $ job ::RUN_ASYNC : 0 );
136- }
137-
138- if ($ async ) {
139- usleep (Job::RUN_USLEEP ); // stream_select() doesn't work with proc_open()
140- }
130+ try {
131+ while (($ this ->jobs || $ running ) && !$ this ->interrupted ) {
132+ while ($ threads && $ this ->jobs ) {
133+ $ running [] = $ job = array_shift ($ this ->jobs );
134+ $ job ->setEnvironmentVariable (Environment::THREAD , (string ) array_shift ($ threads ));
135+ $ job ->run ($ async ? $ job ::RUN_ASYNC : 0 );
136+ }
141137
142- foreach ($ running as $ key => $ job ) {
143- if ($ this ->isInterrupted ()) {
144- break 2 ;
138+ if ($ async ) {
139+ usleep (Job::RUN_USLEEP ); // stream_select() doesn't work with proc_open()
145140 }
146141
147- if (!$ job ->isRunning ()) {
148- $ threads [] = $ job ->getEnvironmentVariable (Environment::THREAD );
149- $ this ->testHandler ->assess ($ job );
150- unset($ running [$ key ]);
142+ foreach ($ running as $ key => $ job ) {
143+ if ($ this ->interrupted ) {
144+ break 2 ;
145+ }
146+
147+ if (!$ job ->isRunning ()) {
148+ $ threads [] = $ job ->getEnvironmentVariable (Environment::THREAD );
149+ $ this ->testHandler ->assess ($ job );
150+ unset($ running [$ key ]);
151+ }
151152 }
152153 }
153- }
154- $ this ->removeInterruptHandler ();
155154
156- foreach ($ this ->outputHandlers as $ handler ) {
157- $ handler ->end ();
155+ } finally {
156+ foreach ($ this ->outputHandlers as $ handler ) {
157+ $ handler ->end ();
158+ }
158159 }
159160
160161 return $ this ->result ;
@@ -235,41 +236,6 @@ public function getInterpreter(): PhpInterpreter
235236 }
236237
237238
238- private function installInterruptHandler (): void
239- {
240- if (function_exists ('pcntl_signal ' )) {
241- pcntl_signal (SIGINT , function (): void {
242- pcntl_signal (SIGINT , SIG_DFL );
243- $ this ->interrupted = true ;
244- });
245- } elseif (function_exists ('sapi_windows_set_ctrl_handler ' ) && PHP_SAPI === 'cli ' ) {
246- sapi_windows_set_ctrl_handler (function () {
247- $ this ->interrupted = true ;
248- });
249- }
250- }
251-
252-
253- private function removeInterruptHandler (): void
254- {
255- if (function_exists ('pcntl_signal ' )) {
256- pcntl_signal (SIGINT , SIG_DFL );
257- } elseif (function_exists ('sapi_windows_set_ctrl_handler ' ) && PHP_SAPI === 'cli ' ) {
258- sapi_windows_set_ctrl_handler (null );
259- }
260- }
261-
262-
263- private function isInterrupted (): bool
264- {
265- if (function_exists ('pcntl_signal_dispatch ' )) {
266- pcntl_signal_dispatch ();
267- }
268-
269- return $ this ->interrupted ;
270- }
271-
272-
273239 private function getLastResult (Test $ test ): int
274240 {
275241 $ signature = $ test ->getSignature ();
0 commit comments