Skip to content

Allow Process.waitUntilExit() to be called from any thread #3113

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

Merged
merged 3 commits into from
Nov 17, 2021

Conversation

pcbeard
Copy link
Contributor

@pcbeard pcbeard commented Nov 16, 2021

This PR relaxes which thread / runloop Process.waitUntilExit() can be called from. Before this change, a different thread would return prematurely from .waitUntilExit() with Process.isRunning still true. This change fixes the problem by observing whether the .runLoop property matches RunLoop.currentRunLoop, and if it doesn't then it no longer calls currentRunLoop.run(mode: .default, before: Date(timeIntervalSinceNow: 0.05)) which would always return false for this new thread. Instead, it simply calls currentRunLoop.run(until: Date(timeIntervalSinceNow: 0.05)) and keeps checking the .isRunning property.

This ensures that if .waitUntilExit() is a different run loop than
.run() was called on, .waitUntilExit() won't return before
self.isRunning becomes false.
This makes the os(Windows) version of .run() a little easier to read.
@pcbeard pcbeard changed the title Allow .waitUntilExit() to be called from any thread Allow Process.waitUntilExit() to be called from any thread Nov 16, 2021
@pcbeard pcbeard changed the title Allow Process.waitUntilExit() to be called from any thread Allow Process.waitUntilExit() to be called from any thread Nov 16, 2021
Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; Please wait for @millenomi to have a look at this as well.

repeat {

} while( self.isRunning == true && RunLoop.current.run(mode: .default, before: Date(timeIntervalSinceNow: 0.05)) )
//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to add a comment here that you forgot?

Copy link
Contributor Author

@pcbeard pcbeard Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In afa6b1e: Remove stray comment, update self.runLoop to enable earlier wakeup from manager thread.

@pcbeard
Copy link
Contributor Author

pcbeard commented Nov 17, 2021

@swift-ci test

@pcbeard pcbeard requested a review from parkera November 17, 2021 18:49
@compnerd compnerd merged commit 6a6b505 into swiftlang:main Nov 17, 2021
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.

3 participants