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

Fix null file descriptor being closed when used as redirection for standard fd of child processes #957

Merged
merged 4 commits into from
Jun 28, 2022

Conversation

MisterDA
Copy link
Contributor

So, this is my mess-up, probably because I ignored the error, and because my tests weren't comprehensive enough.

In my eagerness of cloexec-ing file descriptors, I added one too many O_CLOEXEC flag. When one wants to redirect a standard file descriptor of a child process (giving `Dev_null to ~stdin, ~stdout, or ~stderr arguments of functions in Lwt_process, my code introduced in 9de01c9 will actually close the file descriptor for the child process, which means that any read/write to this standard fd will fail. It is likely that the child process will exit with an error, the opposite of the intent, of course.

I've improved the tests, fixed the bug, simplified a bit the code and used explicit close-on-exec or keep-on-exec everywhere.

@MisterDA
Copy link
Contributor Author

Probably related to #956. You'll possibly want to merge #955 before too.

@MisterDA MisterDA force-pushed the cloexec-proc-dev-null-fixes branch from 7b22822 to 536e884 Compare June 27, 2022 16:02
test/unix/dummy.ml Outdated Show resolved Hide resolved
test/unix/dummy.ml Outdated Show resolved Hide resolved
test/unix/dummy.ml Outdated Show resolved Hide resolved
@MisterDA MisterDA force-pushed the cloexec-proc-dev-null-fixes branch from 536e884 to 0d1e09a Compare June 28, 2022 08:09
@MisterDA
Copy link
Contributor Author

Thanks for the review, applied and rebased your suggestions.

MisterDA added 4 commits June 28, 2022 12:04
- don't swallow stderr of the subprocess;
- use the Unix module directly in the dummy subprocess to avoid
  buffering and errors disappearing.
On Unix O_CLOEXEC on a file descriptor was introduced in
9de01c9. It is superfluous and buggy:
we're already in the forked process at this point! So setting it
cloexec between the actual fork and exec will close the file
descriptor in the sub-process, making all read and writes to it fail.

On Windows, it is wrong as well: we want the child process to inherit
the NUL file descriptor, so it mustn't be cloexec, and it must be
closed after the spawn in the parent process.

It wasn't caught in the tests because they weren't complete enough,
fixed in the parent commit.

The files descriptors must remain keep-on-exec, so we set this file
instead explicitly.
@MisterDA MisterDA force-pushed the cloexec-proc-dev-null-fixes branch from 0d1e09a to 15c65b5 Compare June 28, 2022 10:05
@raphael-proust
Copy link
Collaborator

Tested locally: the tests fail when the src changes are reverted.

@raphael-proust raphael-proust merged commit 3e1f367 into ocsigen:master Jun 28, 2022
@MisterDA MisterDA deleted the cloexec-proc-dev-null-fixes branch June 28, 2022 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants