Avoid prematurely closing file descriptors when redirecting IO #1201
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Janet raises an error if
os/execute
is called with the same stream being used to redirect both STDOUT and STDERR:This problem currently affects JPM when installing with
--silent
(e.g.jpm --silent install judge
). This PR prevents this error by avoiding the premature closing of file descriptors inos_execute_impl
.Discussion
In 4a40e57, @bakpakin made changes to
os_execute_impl
to address #881. For the purposes of this PR, the relevant lines are:janet/src/core/os.c
Lines 1143 to 1163 in c3f4dc0
The problem is that if
new_out
andnew_err
are the same file descriptor, callingposix_spawn_file_actions_addclose
fornew_out
beforepos_spawn_file_actions_adddup2
fornew_err
will cause an error whenposix_spawn
is later called.Alternatives
Another possible solution to this problem would be to establish a practice of not using the same file descriptor when redirecting IO. That seems to violate the principle of least surprise and so is not the approach taken by this PR.