77class ProcessManager
88{
99 private string $ binaryPath ;
10-
1110 private $ currentProcess = null ;
1211
1312 public function __construct (string $ binaryPath )
1413 {
1514 $ this ->binaryPath = $ binaryPath ;
16- // Enable async signals
1715 if (function_exists ('pcntl_async_signals ' )) {
1816 pcntl_async_signals (true );
1917 pcntl_signal (SIGINT , [$ this , 'handleSignal ' ]);
@@ -35,7 +33,7 @@ public function execute(array $config, bool $streamOutput): string
3533 [$ success , $ process , $ pipes ] = $ this ->openProcess ();
3634 $ this ->currentProcess = $ process ;
3735
38- if (! $ success || ! is_array ($ pipes )) {
36+ if (!$ success || !is_array ($ pipes )) {
3937 throw new RuntimeException ('Failed to start process of volt test ' );
4038 }
4139
@@ -44,7 +42,14 @@ public function execute(array $config, bool $streamOutput): string
4442 fclose ($ pipes [0 ]);
4543
4644 $ output = $ this ->handleProcess ($ pipes , $ streamOutput );
47- } finally {
45+
46+ // Store stderr content before closing
47+ $ stderrContent = '' ;
48+ if (isset ($ pipes [2 ]) && is_resource ($ pipes [2 ])) {
49+ rewind ($ pipes [2 ]);
50+ $ stderrContent = stream_get_contents ($ pipes [2 ]);
51+ }
52+
4853 // Clean up pipes
4954 foreach ($ pipes as $ pipe ) {
5055 if (is_resource ($ pipe )) {
@@ -56,12 +61,23 @@ public function execute(array $config, bool $streamOutput): string
5661 $ exitCode = $ this ->closeProcess ($ process );
5762 $ this ->currentProcess = null ;
5863 if ($ exitCode !== 0 ) {
59- throw new RuntimeException ('Process failed with exit code ' . $ exitCode );
64+ echo "\nError: " . trim ($ stderrContent ) . "\n" ;
65+ return '' ;
6066 }
6167 }
62- }
6368
64- return $ output ;
69+ return $ output ;
70+ } finally {
71+ foreach ($ pipes as $ pipe ) {
72+ if (is_resource ($ pipe )) {
73+ fclose ($ pipe );
74+ }
75+ }
76+ if (is_resource ($ process )) {
77+ $ this ->closeProcess ($ process );
78+ $ this ->currentProcess = null ;
79+ }
80+ }
6581 }
6682
6783 protected function openProcess (): array
@@ -80,7 +96,7 @@ protected function openProcess(): array
8096 ['bypass_shell ' => true ]
8197 );
8298
83- if (! is_resource ($ process )) {
99+ if (!is_resource ($ process )) {
84100 return [false , null , []];
85101 }
86102
@@ -112,7 +128,6 @@ private function handleProcess(array $pipes, bool $streamOutput): string
112128 if (feof ($ pipe )) {
113129 fclose ($ pipe );
114130 unset($ pipes [$ type ]);
115-
116131 continue ;
117132 }
118133 }
@@ -140,7 +155,7 @@ protected function writeInput($pipe, string $input): void
140155
141156 protected function closeProcess ($ process ): int
142157 {
143- if (! is_resource ($ process )) {
158+ if (!is_resource ($ process )) {
144159 return -1 ;
145160 }
146161
@@ -149,6 +164,7 @@ protected function closeProcess($process): int
149164 proc_terminate ($ process );
150165 }
151166
167+
152168 return proc_close ($ process );
153169 }
154- }
170+ }
0 commit comments