@@ -161,6 +161,67 @@ public function testConnectCancellationWillRejectWithExceptionWhenProcessExitsBe
161161 Loop ::run ();
162162 }
163163
164+ public function testConnectWillTerminateNewProcessWhenPreviousProcessExitsLateAfterIdleTimeout ()
165+ {
166+ $ connector = new SshSocksConnector ('host ' );
167+
168+ $ ref = new \ReflectionProperty ($ connector , 'cmd ' );
169+ if (PHP_VERSION_ID < 80100 ) {
170+ $ ref ->setAccessible (true );
171+ }
172+ $ ref ->setValue ($ connector , 'trap "sleep 0.2; exit 0" TERM; while :; do sleep 0.01; done; # ' );
173+
174+ $ deferred = new Deferred ();
175+ $ socks = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
176+ $ socks ->expects ($ this ->once ())->method ('connect ' )->with ('example.com:81 ' )->willReturn ($ deferred ->promise ());
177+
178+ $ ref = new \ReflectionProperty ($ connector , 'socks ' );
179+ if (PHP_VERSION_ID < 80100 ) {
180+ $ ref ->setAccessible (true );
181+ }
182+ $ ref ->setValue ($ connector , $ socks );
183+
184+ // first process only exits 0.2s after being terminated by the idle timer
185+ $ promise = $ connector ->connect ('example.com:80 ' );
186+ $ promise ->cancel ();
187+
188+ $ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->isInstanceOf ('RuntimeException ' )));
189+
190+ $ process = null ;
191+ $ onRejected = $ this ->expectCallableOnceWith ($ this ->isInstanceOf ('RuntimeException ' ));
192+ Loop ::addTimer (0.05 , function () use ($ connector , &$ process , $ onRejected ) {
193+ $ ref = new \ReflectionProperty ($ connector , 'cmd ' );
194+ if (PHP_VERSION_ID < 80100 ) {
195+ $ ref ->setAccessible (true );
196+ }
197+ $ ref ->setValue ($ connector , 'echo "debug1: channel 0: new [port listener]" >&2; exec sleep 2; # ' );
198+
199+ $ promise = $ connector ->connect ('example.com:81 ' );
200+ $ promise ->then (null , $ onRejected );
201+
202+ $ ref = new \ReflectionProperty ($ connector , 'listen ' );
203+ if (PHP_VERSION_ID < 80100 ) {
204+ $ ref ->setAccessible (true );
205+ }
206+ $ listen = $ ref ->getValue ($ connector );
207+ assert ($ listen instanceof PromiseInterface);
208+
209+ $ listen ->then (function ($ value ) use (&$ process ) {
210+ $ process = $ value ;
211+ });
212+ });
213+
214+ // second process becomes idle only after first process exited
215+ Loop ::addTimer (0.5 , function () use ($ deferred ) {
216+ $ deferred ->reject (new \RuntimeException ('Connection refused ' ));
217+ });
218+
219+ Loop ::run ();
220+
221+ $ this ->assertInstanceOf ('React\ChildProcess\Process ' , $ process );
222+ $ this ->assertTrue ($ process ->isTerminated ());
223+ }
224+
164225 public function testConnectWillRejectWithExceptionAndTerminateProcessWhenProcessOutputsLineExceedingMaximumBufferSize ()
165226 {
166227 $ connector = new SshSocksConnector ('host ' );
0 commit comments