Skip to content

Commit

Permalink
set c::STARTF_USESTDHANDLES when PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Apr 20, 2024
1 parent 9de7584 commit 8352a3e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion library/std/src/sys/pal/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,21 @@ impl Command {

let mut si = zeroed_startupinfo();

// if STARTF_USESTDHANDLES is not used with PSEUDOCONSOLE,
// it is not guaranteed that all the desired stdio of the new process are
// really connected to the new console.
// The problem + solution is described here:
// https://github.com/microsoft/terminal/issues/4380#issuecomment-580865346
const PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE: usize = 0x20016;
let force_use_std_handles =
self.proc_thread_attributes.contains_key(&PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE);

// If at least one of stdin, stdout or stderr are set (i.e. are non null)
// then set the `hStd` fields in `STARTUPINFO`.
// Otherwise skip this and allow the OS to apply its default behaviour.
// This provides more consistent behaviour between Win7 and Win8+.
let is_set = |stdio: &Handle| !stdio.as_raw_handle().is_null();
if is_set(&stderr) || is_set(&stdout) || is_set(&stdin) {
if force_use_std_handles || is_set(&stderr) || is_set(&stdout) || is_set(&stdin) {
si.dwFlags |= c::STARTF_USESTDHANDLES;
si.hStdInput = stdin.as_raw_handle();
si.hStdOutput = stdout.as_raw_handle();
Expand Down

0 comments on commit 8352a3e

Please sign in to comment.