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

Do not inherit open FDs to SQLite worker by overwriting and closing #7

Merged
merged 1 commit into from
Jan 22, 2019

Conversation

clue
Copy link
Owner

@clue clue commented Jan 22, 2019

This somewhat obscure PR ensures that we do not inherit open file
descriptors (FDs) to the SQLite child worker process. This can cause all
sorts of errors in long running applications and really is not desired
here.

This is implemented by explicitly overwriting all superfluous FDs with
dummy file handles and then closing all of these in the implicit sh
child process before launching the actual php binary. PHP does not
support FD_CLOEXEC, O_CLOEXEC or SOCK_CLOEXEC and this appears to
be the best work around I could find (yes, I should probably write a
lengthy, somewhat technical blog post about this). Additionally, this PR
includes a test to verify this works on all supported platforms and this
could perhaps be used as a starting point for other libraries (YMMV).

This builds on top of clue/reactphp-ssh-proxy#2
and clue/reactphp-ssh-proxy#10
Refs reactphp/child-process#51

This somewhat obscure PR ensures that we do not inherit open file
descriptors (FDs) to the SQLite child worker process. This can cause all
sorts of errors in long running applications and really is not desired
here.

This is implemented by explicitly overwriting all superfluous FDs with
dummy file handles and then closing all of these in the implicit `sh`
child process before launching the actual php binary. PHP does not
support `FD_CLOEXEC`, `O_CLOEXEC` or `SOCK_CLOEXEC`  and this appears to
be the best work around I could find (yes, I should probably write a
lengthy, somewhat technical blog post about this). Additionally, this PR
includes a test to verify this works on all supported platforms and this
could perhaps be used as a starting point for other libraries (YMMV).

This builds on top of clue/reactphp-ssh-proxy#2
and clue/reactphp-ssh-proxy#10
@clue clue added the new feature New feature or request label Jan 22, 2019
@clue clue added this to the v1.0.0 milestone Jan 22, 2019
@clue clue merged commit 14da745 into clue:master Jan 22, 2019
@clue clue deleted the fds branch January 22, 2019 20:02
clue added a commit to clue-labs/reactphp-sqlite that referenced this pull request May 3, 2019
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant