Skip to content

Commit

Permalink
Avoid race condition for forked process in test suite
Browse files Browse the repository at this point in the history
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.

Builds on top of clue#7
  • Loading branch information
clue committed May 3, 2019
1 parent 6a971a6 commit c7c01db
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tests/FunctionalDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,20 @@ public function testOpenMemoryDatabaseShouldNotInheritActiveFileDescriptors()
// close server and ensure we can start a new server on the previous address
// the pending SQLite 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

0 comments on commit c7c01db

Please sign in to comment.