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

Remove return value from pause #829

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#741](https://github.com/nix-rust/nix/pull/741))
- Added more standard trait implementations for various types.
([#814](https://github.com/nix-rust/nix/pull/814))
- Remove return type from `pause`.
([#829](https://github.com/nix-rust/nix/pull/829))

### Changed
- Use native `pipe2` on all BSD targets. Users should notice no difference.
Expand Down
10 changes: 4 additions & 6 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,14 +1316,12 @@ pub fn initgroups(user: &CStr, group: Gid) -> Result<()> {
Errno::result(res).map(|_| ())
}

/// Suspend the thread until a signal is received
/// Suspend the thread until a signal is received.
///
/// See also [pause(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html)
/// See also [pause(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html).
#[inline]
pub fn pause() -> Result<()> {
let res = unsafe { libc::pause() };

Errno::result(res).map(drop)
pub fn pause() {
unsafe { libc::pause() };
}

/// Suspend execution for an interval of time
Expand Down
2 changes: 1 addition & 1 deletion test/sys/test_wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn test_wait_signal() {

// Safe: The child only calls `pause` and/or `_exit`, which are async-signal-safe.
match fork().expect("Error: Fork Failed") {
Child => pause().unwrap_or_else(|_| unsafe { _exit(123) }),
Child => pause(),
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that you have to call _exit() instead of letting the thread naturally exit because of how Rust's standard cleanup isn't signal safe or something. @asomers Can you verify this?

Copy link
Member

Choose a reason for hiding this comment

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

Yep. The standard cleanup may try to access shared resources that were exclusively held by other threads at the time that fork happened. But those other threads don't exist in the child, so your thread will never get the resources, and deadlock will result. That's why we call _exit in child threads.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe I'm misunderstanding something but, isn't unwrap_or_else only called if pause fails and returns an error. Since that can't happend anymore _exit can never be called, and would have never be called even without the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've fixed this in #836, for some reason this pull request is now "from unknown repository". I don't know how to fix that, so instead I created an new pr.

Parent { child } => {
kill(child, Some(SIGKILL)).expect("Error: Kill Failed");
assert_eq!(waitpid(child, None), Ok(WaitStatus::Signaled(child, SIGKILL, false)));
Expand Down