Skip to content

Commit 7b16b4e

Browse files
authored
Rollup merge of rust-lang#136029 - ChrisDenton:py-job, r=jieyouxu
Bootstrap: Don't move ownership of job object I've been thinking about this since the last time I looked at bootstrap's use of job objects. We currently pass ownership of the job object to Python. I feel this is unneeded complexity. The rationale given (in a comment) is that it helps with `ctrl-c` on `x.py`. But using `ctrl-c` when running `x.py` will also cause `bootstrap.exe` to immediately exit so I don't find that convincing.
2 parents 06349ec + ab7793d commit 7b16b4e

File tree

2 files changed

+4
-50
lines changed

2 files changed

+4
-50
lines changed

Diff for: src/bootstrap/bootstrap.py

-3
Original file line numberDiff line numberDiff line change
@@ -1310,9 +1310,6 @@ def bootstrap(args):
13101310
args = [build.bootstrap_binary()]
13111311
args.extend(sys.argv[1:])
13121312
env = os.environ.copy()
1313-
# The Python process ID is used when creating a Windows job object
1314-
# (see src\bootstrap\src\utils\job.rs)
1315-
env["BOOTSTRAP_PARENT_ID"] = str(os.getpid())
13161313
env["BOOTSTRAP_PYTHON"] = sys.executable
13171314
run(args, env=env, verbose=build.verbose, is_bootstrap=True)
13181315

Diff for: src/bootstrap/src/utils/job.rs

+4-47
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ pub unsafe fn setup(build: &mut crate::Build) {
4242
#[cfg(windows)]
4343
mod for_windows {
4444
use std::ffi::c_void;
45-
use std::{env, io, mem};
45+
use std::{io, mem};
4646

47-
use windows::Win32::Foundation::{CloseHandle, DUPLICATE_SAME_ACCESS, DuplicateHandle, HANDLE};
47+
use windows::Win32::Foundation::CloseHandle;
4848
use windows::Win32::System::Diagnostics::Debug::{
4949
SEM_NOGPFAULTERRORBOX, SetErrorMode, THREAD_ERROR_MODE,
5050
};
@@ -53,9 +53,7 @@ mod for_windows {
5353
JOB_OBJECT_LIMIT_PRIORITY_CLASS, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,
5454
JobObjectExtendedLimitInformation, SetInformationJobObject,
5555
};
56-
use windows::Win32::System::Threading::{
57-
BELOW_NORMAL_PRIORITY_CLASS, GetCurrentProcess, OpenProcess, PROCESS_DUP_HANDLE,
58-
};
56+
use windows::Win32::System::Threading::{BELOW_NORMAL_PRIORITY_CLASS, GetCurrentProcess};
5957
use windows::core::PCWSTR;
6058

6159
use crate::Build;
@@ -95,49 +93,8 @@ mod for_windows {
9593
return;
9694
}
9795

98-
// If we've got a parent process (e.g., the python script that called us)
99-
// then move ownership of this job object up to them. That way if the python
100-
// script is killed (e.g., via ctrl-c) then we'll all be torn down.
101-
//
102-
// If we don't have a parent (e.g., this was run directly) then we
103-
// intentionally leak the job object handle. When our process exits
96+
// Note: we intentionally leak the job object handle. When our process exits
10497
// (normally or abnormally) it will close the handle implicitly, causing all
10598
// processes in the job to be cleaned up.
106-
let pid = match env::var("BOOTSTRAP_PARENT_ID") {
107-
Ok(s) => s,
108-
Err(..) => return,
109-
};
110-
111-
let parent = match OpenProcess(PROCESS_DUP_HANDLE, false, pid.parse().unwrap()).ok() {
112-
Some(parent) => parent,
113-
_ => {
114-
// If we get a null parent pointer here, it is possible that either
115-
// we have an invalid pid or the parent process has been closed.
116-
// Since the first case rarely happens
117-
// (only when wrongly setting the environmental variable),
118-
// it might be better to improve the experience of the second case
119-
// when users have interrupted the parent process and we haven't finish
120-
// duplicating the handle yet.
121-
return;
122-
}
123-
};
124-
125-
let mut parent_handle = HANDLE::default();
126-
// If this fails, well at least we tried! An example of DuplicateHandle
127-
// failing in the past has been when the wrong python2 package spawned this
128-
// build system (e.g., the `python2` package in MSYS instead of
129-
// `mingw-w64-x86_64-python2`). Not sure why it failed, but the "failure
130-
// mode" here is that we only clean everything up when the build system
131-
// dies, not when the python parent does, so not too bad.
132-
let _ = DuplicateHandle(
133-
GetCurrentProcess(),
134-
job,
135-
parent,
136-
&mut parent_handle,
137-
0,
138-
false,
139-
DUPLICATE_SAME_ACCESS,
140-
);
141-
CloseHandle(parent).ok();
14299
}
143100
}

0 commit comments

Comments
 (0)