Skip to content

Commit c7c01db

Browse files
committed
Avoid race condition for forked process in test suite
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 #7
1 parent 6a971a6 commit c7c01db

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

tests/FunctionalDatabaseTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,20 @@ public function testOpenMemoryDatabaseShouldNotInheritActiveFileDescriptors()
8585
// close server and ensure we can start a new server on the previous address
8686
// the pending SQLite process should not inherit the existing server socket
8787
fclose($server);
88-
$server = stream_socket_server('tcp://' . $address);
88+
89+
$server = @stream_socket_server('tcp://' . $address);
90+
if ($server === false) {
91+
// There's a very short race condition where the forked php process
92+
// first has to `dup()` the file descriptor specs before invoking
93+
// `exec()` to switch to the actual `ssh` child process. We don't
94+
// need to wait for the child process to be ready, but only for the
95+
// forked process to close the file descriptors. This happens ~80%
96+
// of times on single core machines and almost never on multi core
97+
// systems, so simply wait 5ms (plenty of time!) and retry again.
98+
usleep(5000);
99+
$server = stream_socket_server('tcp://' . $address);
100+
}
101+
89102
$this->assertTrue(is_resource($server));
90103
fclose($server);
91104

0 commit comments

Comments
 (0)