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.
Description
I have a Swift CLI that utilizes ShellOut to interact with binary executables. When I use
shellOut
too many times, I sometimes hit an exception that looks something like:I've been able to determine this is due to the
Pipe
objects from ShellOut not being closed properly.I've included an example project that demonstrates the issue:
NSExceptionExample.zip
Steps:
cd NSExceptionExample
swift run NSExceptionExample
ps aux | grep NSExceptionExample
and take note of the PID.lsof -p <pid> | wc -l
swift
command runs long enough, you'll hit an exception like the above.If you change the example to use this fork and branch, you'll find that this file descriptor increase does not occur when repeating the above steps.
Note: I would love to add a unit test to ensure many commands can be run in the same process by
shellOut
, but I haven't found a good way to do so without creating an extremely long test or creating a very convoluted test harness. Let me know if you have any suggestions for a unit test.System Info
MacBook Pro M1
macOS 14.5
Xcode 15.3, Swift 5.10