Skip to content

Commit

Permalink
Fix stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed May 7, 2024
1 parent ccfdc59 commit b443050
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,6 @@ fn pyspy_main() -> Result<(), Error> {
if let Some(pid) = config.pid {
run_spy_command(pid, &config)?;
} else if let Some(ref subprocess) = config.python_program {
// Dump out stdout/stderr from the process to a temp file, so we can view it later if needed
let mut process_output = tempfile::NamedTempFile::new()?;

let mut command = std::process::Command::new(&subprocess[0]);
#[cfg(unix)]
Expand All @@ -620,13 +618,6 @@ fn pyspy_main() -> Result<(), Error> {

let mut command = command.args(&subprocess[1..]);

if !config.capture_output {
command = command
.stdin(std::process::Stdio::null())
.stdout(process_output.reopen()?)
.stderr(process_output.reopen()?)
}

let mut command = command
.spawn()
.map_err(|e| format_err!("Failed to create process '{}': {}", subprocess[0], e))?;
Expand All @@ -640,36 +631,23 @@ fn pyspy_main() -> Result<(), Error> {
}
});


#[cfg(target_os = "macos")]
{
// sleep just in case: https://jvns.ca/blog/2018/01/28/mac-freeze/
std::thread::sleep(Duration::from_millis(50));
}
let result = run_spy_command(command.id() as _, &config);

// check exit code of subprocess
std::thread::sleep(Duration::from_millis(1));
let success = match command.try_wait()? {
Some(exit) => exit.success(),
// if process hasn't finished, assume success
None => true,
};

// if we failed for any reason, dump out stderr from child process here
// (could have useful error message)
if !config.capture_output && (!success || result.is_err()) {
let mut buffer = String::new();
if process_output.read_to_string(&mut buffer).is_ok() {
eprintln!("{}", buffer);
}
}
let command_id = command.id();
std::thread::spawn(move || {
run_spy_command(command_id as _, &config);
});

command.wait();
// kill it so we don't have dangling processes
if command.kill().is_err() {
// I don't actually care if we failed to kill ... most times process is already done
// eprintln!("Error killing child process {}", e);
}
return result;
}

Ok(())
Expand Down

0 comments on commit b443050

Please sign in to comment.