Skip to content

Commit d0f5dd3

Browse files
authored
Unrolled build for rust-lang#133888
Rollup merge of rust-lang#133888 - ChrisDenton:job, r=jieyouxu Improve bootstrap job objects This attempts to fix a few comments on bootstrap job objects. I also fixed an issue where if duplicating the job object handle into the python process failed, it would close the job object. This would then result in the job object closing all attached processes, which at that point includes the current process. The fix is to simply never close the job object handle at any point after the current process is assigned to it.
2 parents 0e98766 + 898d751 commit d0f5dd3

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

src/bootstrap/bootstrap.py

+2
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,8 @@ def bootstrap(args):
11841184
args = [build.bootstrap_binary()]
11851185
args.extend(sys.argv[1:])
11861186
env = os.environ.copy()
1187+
# The Python process ID is used when creating a Windows job object
1188+
# (see src\bootstrap\src\utils\job.rs)
11871189
env["BOOTSTRAP_PARENT_ID"] = str(os.getpid())
11881190
env["BOOTSTRAP_PYTHON"] = sys.executable
11891191
run(args, env=env, verbose=build.verbose, is_bootstrap=True)

src/bootstrap/src/utils/job.rs

+15-26
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ pub unsafe fn setup(build: &mut crate::Build) {
1515
///
1616
/// Most of the time when you're running a build system (e.g., make) you expect
1717
/// Ctrl-C or abnormal termination to actually terminate the entire tree of
18-
/// process in play, not just the one at the top. This currently works "by
18+
/// processes in play. This currently works "by
1919
/// default" on Unix platforms because Ctrl-C actually sends a signal to the
20-
/// *process group* rather than the parent process, so everything will get torn
21-
/// down. On Windows, however, this does not happen and Ctrl-C just kills the
22-
/// parent process.
20+
/// *process group* so everything will get torn
21+
/// down. On Windows, however, Ctrl-C is only sent to processes in the same console.
22+
/// If a process is detached or attached to another console, it won't receive the
23+
/// signal.
2324
///
2425
/// To achieve the same semantics on Windows we use Job Objects to ensure that
2526
/// all processes die at the same time. Job objects have a mode of operation
@@ -87,15 +88,7 @@ mod for_windows {
8788
);
8889
assert!(r.is_ok(), "{}", io::Error::last_os_error());
8990

90-
// Assign our process to this job object. Note that if this fails, one very
91-
// likely reason is that we are ourselves already in a job object! This can
92-
// happen on the build bots that we've got for Windows, or if just anyone
93-
// else is instrumenting the build. In this case we just bail out
94-
// immediately and assume that they take care of it.
95-
//
96-
// Also note that nested jobs (why this might fail) are supported in recent
97-
// versions of Windows, but the version of Windows that our bots are running
98-
// at least don't support nested job objects.
91+
// Assign our process to this job object.
9992
let r = AssignProcessToJobObject(job, GetCurrentProcess());
10093
if r.is_err() {
10194
CloseHandle(job).ok();
@@ -124,14 +117,19 @@ mod for_windows {
124117
// (only when wrongly setting the environmental variable),
125118
// it might be better to improve the experience of the second case
126119
// when users have interrupted the parent process and we haven't finish
127-
// duplicating the handle yet. We just need close the job object if that occurs.
128-
CloseHandle(job).ok();
120+
// duplicating the handle yet.
129121
return;
130122
}
131123
};
132124

133125
let mut parent_handle = HANDLE::default();
134-
let r = DuplicateHandle(
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(
135133
GetCurrentProcess(),
136134
job,
137135
parent,
@@ -140,15 +138,6 @@ mod for_windows {
140138
false,
141139
DUPLICATE_SAME_ACCESS,
142140
);
143-
144-
// If this failed, well at least we tried! An example of DuplicateHandle
145-
// failing in the past has been when the wrong python2 package spawned this
146-
// build system (e.g., the `python2` package in MSYS instead of
147-
// `mingw-w64-x86_64-python2`). Not sure why it failed, but the "failure
148-
// mode" here is that we only clean everything up when the build system
149-
// dies, not when the python parent does, so not too bad.
150-
if r.is_err() {
151-
CloseHandle(job).ok();
152-
}
141+
CloseHandle(parent).ok();
153142
}
154143
}

0 commit comments

Comments
 (0)