-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
docs: document platform-specific program behavior in Command #152233
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
Conversation
|
r? @jhpratt rustbot has assigned @jhpratt. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
|
|
This just seems to be python specific behaviour, no? I'm not sure it's useful for rust documentation to include documentation on python's platform differences. |
No. Issue is general. Applies to any program that:
Python is just an example. I updated the documentation to say:
Same principle applies to node/ruby/cat/bc.... any stdin-reading program. Python just happened to be the case that exposed this in Servo, so it's a concrete, relatable example. Using it to be illustrative, not exhaustive. See: servo/servo#42340 EDIT: Will make slight tweak to comments to make this clearer. |
While Command itself is OS-agnostic, spawned programs behavior may differ by platform. Example, invoking program with no arguments may hang on Windows (waits for stdin) but exit immediately on Unix. This documents issue and suggests using flags like --version to ensure consistent behavior when probing for program availability. Discovered while debugging a cross-platform build system that hung on Windows ARM64 when detecting Python availability.
288e65f to
d55672f
Compare
|
Is node affected? That surprises me if so. I'm doubly surprised about
But that's just as true on Unix platforms as it is on Windows. There's no particular reason why a Windows application cannot detect non-TTY stdin and exit early. The fact that python doesn't seems like a quirk of python. |
Yes to node and yes to use std::process::Command;
use std::time::{Duration, Instant};
fn test_program(name: &str, timeout_secs: u64) {
println!("Testing '{}'...", name);
let start = Instant::now();
let mut child = match Command::new(name).spawn() {
Ok(c) => c,
Err(e) => { println!(" '{}' failed: {}", name, e); return; }
};
loop {
match child.try_wait() {
Ok(Some(status)) => {
println!(" '{}' exited in {:?}", name, start.elapsed());
return;
}
Ok(None) => {
if start.elapsed() > Duration::from_secs(timeout_secs) {
child.kill().ok();
println!(" '{}' HUNG (killed after {}s)", name, timeout_secs);
return;
}
std::thread::sleep(Duration::from_millis(100));
}
Err(e) => { println!(" '{}' error: {}", name, e); return; }
}
}
}
fn main() {
test_program("python", 5); // or python3 on Unix
test_program("node", 5);
test_program("cat", 5); // GNU coreutils via Git for Windows
}Results:
You're right that programs could detect non-TTY stdin on Windows. But observed behavior is that common programs don't; they wait for console input. On Unix, (verified on WSL) same programs receive immediate EOF from pipe and exit. TL;DR: Not Python quirk but rather a cross-platform footgun when using NOTE: |
|
You don't say how you're running this on Windows. If running directly from the console then by default a tty would be attached. The original servo code was using |
Ooo! You're right. Apologies My original test was flawed. Used spawn() which inherited console TTY. Reran with proper Command::new(name)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
Only Python hangs! node + cat both detected EOF and exit on Windows. I stand corrected. This is indeed Python-specific behavior, not a general platform issue. Will close this PR as I agree, documentation change isn't warranted. Also, thanks for pushing back and making me verify properly. Servo fix (using --version) is still correct, but it's working around the Python quirk, not platform behavior. |
While
Commanditself is designed to be OS-agnostic, spawned programs behavior may differ by platform. Example, invoking program with no arguments may hang on Windows (if waits for stdin) but exit immediately on Unix.This adds new section to
Commanddocumentation noting behavior and suggesting use of flags like--versionto ensure consistent behavior when probing for program availability.Context:
Discovered while debugging cross-platform build system (Servo) that hung on Windows ARM64 when detecting Python availability. Fix was use
python --versioninstead ofpythonwith no args.This addresses workingjubilee's suggestion in #152143 (comment):