Skip to content

Ignore WouldBlock I/O error kind #11

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ branch = "master"
rocket = "0.4.5"
mime = "0.3.12"
multipart = { version = "0.17", default-features = false, features = ["server"] }
backoff = "0.3"

[dev-dependencies]
rocket-include-static-resources = "0.9"
Expand Down
36 changes: 35 additions & 1 deletion src/multipart_form_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl MultipartFormData {

options.allowed_fields.sort_by_key(|e| e.field_name);

let mut multipart = Multipart::with_body(data.open(), boundary);
let mut multipart = Multipart::with_body(WouldBlockFreeRead(data.open()), boundary);

let mut files: HashMap<Arc<str>, Vec<FileField>> = HashMap::new();
let mut raw: HashMap<Arc<str>, Vec<RawField>> = HashMap::new();
Expand Down Expand Up @@ -399,3 +399,37 @@ impl Drop for MultipartFormData {
fn try_delete<P: AsRef<Path>>(path: P) {
if fs::remove_file(path.as_ref()).is_err() {}
}

// Work around an issue with non blocking I/O which is hard to fix all the through
// rocket + hyper-0.10. Rocket 0.5 seems not to suffer from that defect, probably due to some
// special treatment related to async I/O.
struct WouldBlockFreeRead<R: Read>(R);

const DELAY_BEFORE_WOULD_BLOCK_RETRY: std::time::Duration = std::time::Duration::from_millis(200);

impl<R: Read> Read for WouldBlockFreeRead<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
use backoff::retry;
let res = retry(
backoff::backoff::Constant::new(DELAY_BEFORE_WOULD_BLOCK_RETRY),
|| -> Result<usize, backoff::Error<std::io::Error>> {
match self.0.read(buf) {
Ok(read) => Ok(read),
Err(
err
@ std::io::Error {
..
},
) if err.kind() == std::io::ErrorKind::WouldBlock => {
Err(backoff::Error::Transient(err))
}
Err(err) => Err(backoff::Error::Permanent(err)),
}
},
);
match res {
Ok(res) => Ok(res),
Err(backoff::Error::Permanent(err)) | Err(backoff::Error::Transient(err)) => Err(err),
}
}
}