Skip to content

Commit f602b08

Browse files
committed
Fix orphaned SSH client process when previous process exits late
1 parent 11d6772 commit f602b08

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

‎src/SshSocksConnector.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ public function connect($uri)
105105
}
106106

107107
// no listening SSH client process started? start one and remember reference
108-
// if spawning SSH client process fails, clear reference for next start
108+
// if spawning SSH client process fails, clear reference for next start unless already replaced
109109
if ($this->listen === null) {
110110
$listen =& $this->listen;
111-
$this->listen = $this->listen()->then(null, function ($e) use (&$listen) {
112-
$listen = null;
111+
$this->listen = $promise = $this->listen()->then(null, function ($e) use (&$listen, &$promise) {
112+
if ($listen === $promise) {
113+
$listen = null;
114+
}
113115
throw $e;
114116
});
115117

‎tests/IntegrationSshSocksConnectorTest.php‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)