Skip to content
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

Add CommandExt::wait_with_output(self, timeout) #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jirutka
Copy link

@jirutka jirutka commented May 2, 2016

@alexcrichton I’ll try to write smoke tests for this if you say that you wanna merge it.

@alexcrichton
Copy link
Owner

Unfortunately I don't think it's actually possible to do this atomically, at which point this probably shouldn't be exposed. If the wait times out then you probably don't want to capture output, and otherwise if you wait with a timeout then the process can deadlock because you're not reading the output (it needs to happen concurrently essentially)

@jirutka
Copy link
Author

jirutka commented May 2, 2016

So how else can it be solved? I need to execute an user provided script that returns some result, but kill it when it hangs for whatever reason.

@alexcrichton
Copy link
Owner

You'd basically need to define your own semantics for what happens when the process times out. Do you throw away the input/output or send a kill signal? If you want to just kill it you can probably spawn two threads to read stdin/stdout, use the main thread to call wait_timeout, and then once you kill the child the two other threads will return.

@jirutka
Copy link
Author

jirutka commented May 3, 2016

If the process times out, then I don’t care about std{out,err} (however it may be useful to print stderr to log), just want to kill the process (it’s only a “safety net”). Spawning two threads just to read std{out,err} sounds like an overkill.

@alexcrichton
Copy link
Owner

The "overkill" here is required for correctness. This may work locally but as part of a library you can't read stdout/stderr only after the process exits.

@woodgear
Copy link

how about add this as a feature?

@dylni
Copy link

dylni commented Mar 8, 2020

FYI, I created a crate to do this:
https://crates.io/crates/process_control

You can use Child::with_output_timeout to get output from a process with a timeout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants