Fix: Replace Process.waitUntilExit() with DispatchGroup to prevent hangs #86
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've been running into hangs with
waitUntilExit()in my CLI, which is annoying since we use it in pre-commit hooks :DThe issue is that
waitUntilExit()runs the current thread's runloop inNSDefaultRunLoopModewhile waiting for the process to exit. This causes problems with Swift concurrency's cooperative thread pool because:Basically,
waitUntilExit()assumes you're on a specific thread with a well-behaved runloop (like the main thread), but Swift concurrency doesn't give you those guarantees.The Fix
I replaced
waitUntilExit()with aDispatchGroup:terminationHandlerwait()inclose()to block until terminationThis is a simple blocking wait that doesn't depend on runloop infrastructure, so it works reliably regardless of which thread it's called from.
Ironically, I hit this same issue in my own code and solved it with Eskimo's advice, but that approach would've required a bigger refactor here. This minimal change resolves the hanging issue.
See also: Mike Ash's article on dangerous Cocoa calls