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

Expose process windows_process_extensions_main_thread_handle on Windows #96725

Merged
merged 1 commit into from
May 10, 2022

Conversation

nico-abram
Copy link
Contributor

@nico-abram nico-abram commented May 5, 2022

I did not find any tests in https://github.com/rust-lang/rust/blob/7d3e03666a93bd2b0f78b3933f9305832af771a5/library/std/src/sys/windows/process/tests.rs that actually launch processes, so I haven't added tests for this. I ran the following locally, to check that it works as expected:

#![feature(windows_process_extensions_main_thread_handle)]

fn main() {
    use std::os::windows::process::{ChildExt, CommandExt};
    const CREATE_SUSPENDED: u32 = 0x00000004;

    let proc = std::process::Command::new("cmd")
        .args(["/C", "echo hello"])
        .creation_flags(CREATE_SUSPENDED)
        .spawn()
        .unwrap();

    extern "system" {
        fn ResumeThread(_: *mut std::ffi::c_void) -> u32;
    }
    unsafe {
        ResumeThread(proc.main_thread_handle());
    }

    let output = proc.wait_with_output().unwrap();
    let str_output = std::str::from_utf8(&output.stdout[..]).unwrap();
    println!("{}", str_output);
}

Without the feature attribute it wouldn't compile, and commenting the ResumeThread line makes it hang forever, showing that it works.

Tracking issue #96723

@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 5, 2022
@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 5, 2022
@retep998
Copy link
Member

retep998 commented May 5, 2022

Why not expose the thread handle instead of the thread id?
We expose the process handle directly instead of the process id, so why is the main thread different?

@ChrisDenton
Copy link
Member

Why not expose the thread handle instead of the thread id?

Agreed, although are there any downsides is holding a thread handle open?

@nico-abram
Copy link
Contributor Author

I didn't realize there was precedent for exposing the process handle directly, just saw the id() method and missed AsRawHandle. You're right that would be better. Modified the patch to use handles instead of ids

@nico-abram nico-abram changed the title Expose process main_thread_id on Windows Expose process windows_process_extensions_thread_id on Windows May 5, 2022
@ChrisDenton
Copy link
Member

Btw, there are tests that run processes in src\process\tests.rs. I think the distinction is that src\sys\windows\process\tests.rs is more for testing functions internal to sys\windows while src\process\tests.rs tests the std APIs. But don't quote me on that 😉.

@nico-abram nico-abram changed the title Expose process windows_process_extensions_thread_id on Windows Expose process windows_process_extensions_main_thread_handle on Windows May 5, 2022
@nico-abram
Copy link
Contributor Author

Added a test

@ChrisDenton ChrisDenton added O-windows Operating system: Windows T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 6, 2022
@ChrisDenton
Copy link
Member

Ok, so my concern was that keeping the thread handle open is an insta-stable change so I wanted to be sure this wouldn't cause breakage. However, I think I've satisfied myself that closing it at the same time as the process handle won't be an issue for existing code. The worst that can happen is some kernel data structures are needlessly kept around in the event that a child process exits its main thread but the process continues running other threads (which would itself be a very unusual situation).

My only other concern is that perhaps new APIs should be making use of the (currently unstable) BorrowedHandle<'_>, assuming it will be stabilized sometime soon-ish. But that could be added as a possibility on the tracking issue.

@ChrisDenton
Copy link
Member

I'll ask @rust-lang/libs-api if they're ok with this.

@retep998
Copy link
Member

retep998 commented May 9, 2022

The worst that can happen is some kernel data structures are needlessly kept around in the event that a child process exits its main thread but the process continues running other threads (which would itself be a very unusual situation).

Can this even happen? As far as I can tell, when the main thread exits Windows will exit the process anyway.

@m-ou-se
Copy link
Member

m-ou-se commented May 9, 2022

My only other concern is that perhaps new APIs should be making use of the (currently unstable) BorrowedHandle<'_>, assuming it will be stabilized sometime soon-ish. But that could be added as a possibility on the tracking issue.

Might be good to do that right away. Then we get some usage of BorrowedHandle before stabilizing it, to see if it works well. :)

@ChrisDenton
Copy link
Member

Can this even happen? As far as I can tell, when the main thread exits Windows will exit the process anyway.

It's possible! In Win32, a process only exits when all threads have exited or ExitProcess is called (which terminates all other threads),

@ChrisDenton
Copy link
Member

Might be good to do that right away. Then we get some usage of BorrowedHandle before stabilizing it, to see if it works well. :)

Good point. @nico-abram could you update the implementation to return BorrowedHandle

@nico-abram nico-abram force-pushed the win_tid branch 2 times, most recently from adaa03b to b5789a0 Compare May 10, 2022 01:13
@rust-log-analyzer

This comment has been minimized.

@nico-abram
Copy link
Contributor Author

Rebased on master and changed the API to use BorrowedHandle

Copy link
Member

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

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

Looks good! Just a few small points.

library/std/src/sys/windows/process.rs Outdated Show resolved Hide resolved
library/std/src/sys/windows/process.rs Outdated Show resolved Hide resolved
library/std/src/sys/windows/process/tests.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@ChrisDenton
Copy link
Member

Great, thanks! If you could squish the commits then I think this will be ready to be merged.

@nico-abram nico-abram force-pushed the win_tid branch 2 times, most recently from ecb9dff to 65b9586 Compare May 10, 2022 05:37
@nico-abram
Copy link
Contributor Author

Squished

@ChrisDenton
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented May 10, 2022

📌 Commit 5368ea7 has been approved by ChrisDenton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 10, 2022
@oli-obk
Copy link
Contributor

oli-obk commented May 10, 2022

@bors rollup

bors added a commit to rust-lang-ci/rust that referenced this pull request May 10, 2022
Rollup of 6 pull requests

Successful merges:

 - rust-lang#96717 (Handle mismatched generic param kinds in trait impls betterly)
 - rust-lang#96725 (Expose process windows_process_extensions_main_thread_handle on Windows)
 - rust-lang#96849 (Move some tests to more reasonable places)
 - rust-lang#96861 (Use Rust 2021 prelude in std itself.)
 - rust-lang#96879 (rustdoc: search result ranking fix)
 - rust-lang#96882 (Don't subst an AdtDef with its own substs)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f689f65 into rust-lang:master May 10, 2022
@rustbot rustbot added this to the 1.62.0 milestone May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants