Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid race condition for forked process in test suite #14

Merged
merged 1 commit into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion tests/FunctionalSshProcessConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,20 @@ public function testConnectPendingWillNotInheritActiveFileDescriptors()
// close server and ensure we can start a new server on the previous address
// the pending SSH connection process should not inherit the existing server socket
fclose($server);
$server = stream_socket_server('tcp://' . $address);

$server = @stream_socket_server('tcp://' . $address);
if ($server === false) {
// There's a very short race condition where the forked php process
// first has to `dup()` the file descriptor specs before invoking
// `exec()` to switch to the actual `ssh` child process. We don't
// need to wait for the child process to be ready, but only for the
// forked process to close the file descriptors. This happens ~80%
// of times on single core machines and almost never on multi core
// systems, so simply wait 5ms (plenty of time!) and retry again.
usleep(5000);
$server = stream_socket_server('tcp://' . $address);
}

$this->assertTrue(is_resource($server));
fclose($server);

Expand Down
20 changes: 19 additions & 1 deletion tests/FunctionalSshSocksConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,25 @@ public function testConnectPendingWillNotInheritActiveFileDescriptors()
// close server and ensure we can start a new server on the previous address
// the pending SSH connection process should not inherit the existing server socket
fclose($server);
$server = stream_socket_server('tcp://' . $address);

$server = @stream_socket_server('tcp://' . $address);
if ($server === false) {
// There's a very short race condition where the forked php process
// first has to `dup()` the file descriptor specs before invoking
// `exec()` to switch to the actual `ssh` child process. We don't
// need to wait for the child process to be ready, but only for the
// forked process to close the file descriptors. This happens ~80%
// of times on single core machines and almost never on multi core
// systems, so simply wait 5ms (plenty of time!) and retry again twice.
usleep(5000);
$server = @stream_socket_server('tcp://' . $address);

if ($server === false) {
usleep(5000);
$server = stream_socket_server('tcp://' . $address);
}
}

$this->assertTrue(is_resource($server));
fclose($server);

Expand Down