Skip to content

Commit bab5249

Browse files
committed
Fix reusing stale SSH client process after it exits unexpectedly
1 parent d485d1e commit bab5249

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

‎src/SshSocksConnector.php‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,8 @@ 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 unless already replaced
109108
if ($this->listen === null) {
110-
$listen =& $this->listen;
111-
$this->listen = $promise = $this->listen()->then(null, function ($e) use (&$listen, &$promise) {
112-
if ($listen === $promise) {
113-
$listen = null;
114-
}
115-
throw $e;
116-
});
109+
$this->listen = $this->listen();
117110

118111
// @codeCoverageIgnoreStart
119112
if ($this->debug) {
@@ -262,13 +255,20 @@ private function listen()
262255
$process->terminate();
263256
});
264257

265-
$process->on('exit', function ($code) use ($deferred, &$last) {
258+
// if SSH client process exits, clear reference for next start unless already replaced
259+
$listen =& $this->listen;
260+
$promise = $deferred->promise();
261+
$process->on('exit', function ($code) use ($deferred, &$last, &$listen, $promise) {
262+
if ($listen === $promise) {
263+
$listen = null;
264+
}
265+
266266
$deferred->reject(new \RuntimeException(
267267
$last,
268268
(int) $code
269269
));
270270
});
271271

272-
return $deferred->promise();
272+
return $promise;
273273
}
274274
}

‎tests/IntegrationSshSocksConnectorTest.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,43 @@ public function testConnectCancellationWillNotInvokeSocksConnectorWhenProcessBec
191191
Loop::run();
192192
}
193193

194+
public function testConnectWillStartNewProcessWhenPreviousProcessExitsAfterPortListenerMessage()
195+
{
196+
$connector = new SshSocksConnector('host');
197+
198+
$ref = new \ReflectionProperty($connector, 'cmd');
199+
if (PHP_VERSION_ID < 80100) {
200+
$ref->setAccessible(true);
201+
}
202+
$ref->setValue($connector, 'echo "debug1: channel 0: new [port listener]" >&2; exec sleep 0.1; #');
203+
204+
$deferred = new Deferred();
205+
$socks = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
206+
$socks->expects($this->once())->method('connect')->with('example.com:80')->willReturn($deferred->promise());
207+
208+
$ref = new \ReflectionProperty($connector, 'socks');
209+
if (PHP_VERSION_ID < 80100) {
210+
$ref->setAccessible(true);
211+
}
212+
$ref->setValue($connector, $socks);
213+
214+
$connector->connect('example.com:80');
215+
216+
$onRejected = $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException'));
217+
Loop::addTimer(0.5, function () use ($connector, $onRejected) {
218+
$ref = new \ReflectionProperty($connector, 'cmd');
219+
if (PHP_VERSION_ID < 80100) {
220+
$ref->setAccessible(true);
221+
}
222+
$ref->setValue($connector, 'echo "Connection closed by remote host" >&2; exit 255; #');
223+
224+
$promise = $connector->connect('example.com:80');
225+
$promise->then(null, $onRejected);
226+
});
227+
228+
Loop::run();
229+
}
230+
194231
public function testConnectWillTerminateNewProcessWhenPreviousProcessExitsLateAfterIdleTimeout()
195232
{
196233
$connector = new SshSocksConnector('host');

0 commit comments

Comments
 (0)