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

Add a convenience method .pid() for WaitStatus. #722

Merged
merged 1 commit into from
Aug 8, 2017

Conversation

marmistrz
Copy link
Contributor

No description provided.

let _m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");

match fork().unwrap() {
Child => {},
Copy link
Member

Choose a reason for hiding this comment

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

Don't allow the child to return. It's vulnerable to deadlocks in the test harness code. Instead, call _exit(0).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean that the test harness executes the function as a normal function, so the forked process will duplicate what test harness does?

Why _exit and not std::process::exit? The former is unsafe.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the forked process duplicates the entire test harness. But it doesn't duplicate any threads other than the one that called fork. So if one of those other threads held a mutex that is required by this thread during its teardown phase, then this thread will deadlock. The reason to use _exit instead of sys::process::exit is subtler, and it's because _exit doesn't do any C-level teardown. See fork(2).

Copy link
Contributor Author

@marmistrz marmistrz Aug 7, 2017

Choose a reason for hiding this comment

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

You mean the reasons like this: https://stackoverflow.com/questions/5422831/what-is-the-difference-between-using-exit-exit-in-a-conventional-linux-fo
(double buffer flushing or double execution of atexit handlers?)

Why is there no clean way of doing _exit without unsafe constructs if we're exposing fork as a public function?

Copy link
Member

Choose a reason for hiding this comment

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

Like that, but worse because the parent is a multi-threaded process. In a multi-threaded process, even memory allocation could theoretically deadlock, so until the child execs, it is limited to only calling async-signal-safe functions, which are a pretty restrictive set.

@asomers
Copy link
Member

asomers commented Aug 7, 2017

This all looks good to me, though you still need to squash. I'll give @Susurrus a day or so to comment before I merge it.

@marmistrz
Copy link
Contributor Author

@asomers Can you squash while merging? The GitHub web UI allows it.

@asomers
Copy link
Member

asomers commented Aug 7, 2017

@marmistrz I can't squash while merging, because nix doesn't use the GitHub web UI. We use bors instead. bors prevents test failures caused by merge conflicts, but unfortunately can't automatically squash.

CHANGELOG.md Outdated
@@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#672](https://github.com/nix-rust/nix/pull/672))
- Added protocol families in `AddressFamily` enum.
([#647](https://github.com/nix-rust/nix/pull/647))
- Added a convenience method `.pid()` to the `enum WaitStatus`
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead "Added the pid() method to WaitStatus for extracting the PID."

src/sys/wait.rs Outdated
/// Extracts the PID from the WaitStatus if the status is not StillAlive.
pub fn pid(&self) -> Option<Pid> {
use self::WaitStatus::*;
match self {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not certain if this StackOverflow comment is correct, but it would read nicer if you did match *self to get rid of the & on every match arm.

@marmistrz
Copy link
Contributor Author

Squashed.

@asomers
Copy link
Member

asomers commented Aug 8, 2017

bors r+

bors bot added a commit that referenced this pull request Aug 8, 2017
722: Add a convenience method .pid() for WaitStatus. r=asomers
@bors
Copy link
Contributor

bors bot commented Aug 8, 2017

@bors bors bot merged commit 0370de6 into nix-rust:master Aug 8, 2017
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