Skip to content

Support timeout in Popen.communicate #1724

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

Merged
merged 1 commit into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/snippets/stdlib_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@
p = subprocess.Popen(["echo", "test"], stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate()
assert stdout.strip() == b"test"

p = subprocess.Popen(["sleep", "5"], stdout=subprocess.PIPE)
with assert_raises(subprocess.TimeoutExpired):
p.communicate(timeout=1)
2 changes: 1 addition & 1 deletion vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ adler32 = "1.0.3"
flate2 = { version = "1.0", features = ["zlib"], default-features = false }
libz-sys = "1.0.25"
gethostname = "0.2.0"
subprocess = "0.1.18"
subprocess = "0.2.2"
num_cpus = "1"
socket2 = { version = "0.3", features = ["unix"] }

Expand Down
19 changes: 14 additions & 5 deletions vm/src/stdlib/subprocess.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::RefCell;
use std::ffi::OsString;
use std::fs::File;
use std::io::ErrorKind;
use std::time::Duration;

use subprocess;
Expand Down Expand Up @@ -191,13 +192,21 @@ impl PopenRef {
vm: &VirtualMachine,
) -> PyResult<(Option<Vec<u8>>, Option<Vec<u8>>)> {
let bytes = match args.input {
OptionalArg::Present(ref bytes) => Some(bytes.get_value()),
OptionalArg::Present(ref bytes) => Some(bytes.get_value().to_vec()),
OptionalArg::Missing => None,
};
self.process
.borrow_mut()
.communicate_bytes(bytes)
.map_err(|err| convert_io_error(vm, err))
let mut communicator = self.process.borrow_mut().communicate_start(bytes);
if let OptionalArg::Present(timeout) = args.timeout {
communicator = communicator.limit_time(Duration::new(timeout, 0));
}
communicator.read().map_err(|err| {
if err.error.kind() == ErrorKind::TimedOut {
let timeout_expired = vm.try_class("_subprocess", "TimeoutExpired").unwrap();
vm.new_exception_msg(timeout_expired, "Timeout".to_string())
} else {
convert_io_error(vm, err.error)
}
})
}

fn pid(self, _vm: &VirtualMachine) -> Option<u32> {
Expand Down