-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Spawn new process with creation_flag(0x00000010) to get a new console on win7, the new process get IO.Exception. #105203
Comments
Can you describe the error more fully and also the intended use-case? The behavior you describe is at conflict with the behavior of |
Thanks for your replying, I want to make a process supervisor, which needs to spawn child process that has an individual console for input and output. As a result, I set the creation_flag as 0x00000010 [https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags]. The error is the child process will throw an IO.Exception (when my csharp console application try to console.write("sth.")) on win7, but works well on win10, with that creation_flag. And it works well on both win7 & win10 without that creation_flag, but the parent and child process share the parent's console. I checked the windows api's document and the source code of Command::spawn(), It seems like, if I do not need pipe output, Command::spawn() just inherit the parent's setting of stdio, which in my case, just the default stdio. So, I removed that flag, let windows set the default stdio for child process, for a temporarily fix in my use-case. |
Ah, so the It seems there are other issues touching on adjacent concerns, where the implicit defaults of
The behavior you want seems reasonable, but it may be more correct to expose an explicit API for the desired behavior instead of implicitly reasoning based on the content of the creation flags. |
The error message is
Yes.
I'm really sorry, after further test, I get this error from code
Yes. Thank you very much for these replies, I do agree there needs to have more explicit API for spawn a new process. |
I think a full fix here is to have a special new |
Ok, now that #103459 has merged, you can do this on the latest nightly but it isn't pretty because there's not yet a proper API for it. I've adapted the code in the OP to add a use std::os::windows::io::FromRawHandle;
use std::os::windows::process::CommandExt;
use std::process::{Command, Stdio};
fn default_stdio() -> Stdio {
unsafe { Stdio::from_raw_handle(std::ptr::null_mut()) }
}
let mut res = Command::new(args.path)
// create a new console for child process
.creation_flags(0x00000010)
.current_dir(dirp)
// use the default handles
.stdin(default_stdio())
.stdout(default_stdio())
.stderr(default_stdio())
.spawn()
.unwrap(); In case you aren't aware, you can use |
wow, thank you very much! It do works well on both win7 & win10. |
I tried this code:
It works well on win10, the new process has an individual console for stdout & stderr, keyboard for stdin.
But the same binary can't get the same result on win7, the new process throw a IO.Exception.
I tried to delete a line in std/src/sys/windows/process.rs line: 258 fn spawn()
// si.dwFlags = c::STARTF_USESTDHANDLES;
Which let the windows to set a default stdio for new process.
In this case, the binary works well on both win7 & win10.
Meta
Both stable and nightly version of the compiler has the same situation.
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: