Skip to content

Commit 11d6772

Browse files
committed
Fix fatal error when SSH client exits before idle timer fires
1 parent 24f6702 commit 11d6772

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎src/SshSocksConnector.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,15 @@ public function idle()
194194

195195
if ($this->pending < 1 && $this->idleTimer === null && $this->listen !== null) {
196196
$listen =& $this->listen;
197+
$promise = $this->listen;
197198
$debug = $this->debug;
198-
$this->idleTimer = $this->loop->addTimer($this->idlePeriod, function () use (&$listen, $debug) {
199-
$listen->then(function (Process $process) {
199+
$this->idleTimer = $this->loop->addTimer($this->idlePeriod, function () use (&$listen, $promise, $debug) {
200+
$promise->then(function (Process $process) {
200201
$process->terminate();
201202
}, function () {
202203
// ignore to avoid reporting unhandled rejection
203204
});
204-
$listen->cancel();
205+
$promise->cancel();
205206
$listen = null;
206207

207208
// @codeCoverageIgnoreStart

‎tests/IntegrationSshSocksConnectorTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,31 @@ public function testConnectCancellationWillRejectWithException()
136136
Loop::run();
137137
}
138138

139+
public function testConnectCancellationWillRejectWithExceptionWhenProcessExitsBeforeIdleTimeout()
140+
{
141+
$connector = new SshSocksConnector('host');
142+
143+
$ref = new \ReflectionProperty($connector, 'cmd');
144+
if (PHP_VERSION_ID < 80100) {
145+
$ref->setAccessible(true);
146+
}
147+
$ref->setValue($connector, 'exec false; #');
148+
149+
// delay idle timer, so process exit is reported before idle timer fires
150+
$ref = new \ReflectionProperty($connector, 'idlePeriod');
151+
if (PHP_VERSION_ID < 80100) {
152+
$ref->setAccessible(true);
153+
}
154+
$ref->setValue($connector, 0.2);
155+
156+
$promise = $connector->connect('example.com:80');
157+
$promise->cancel();
158+
159+
$promise->then(null, $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException')));
160+
161+
Loop::run();
162+
}
163+
139164
public function testConnectWillRejectWithExceptionAndTerminateProcessWhenProcessOutputsLineExceedingMaximumBufferSize()
140165
{
141166
$connector = new SshSocksConnector('host');

0 commit comments

Comments
 (0)