-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
wasi-http: can we call the blocking_write_and_flush
method of the OutgoingRequest
body multiple times?
#9653
Comments
Your program getting hung forever on the second Can you provide a .wasm that reproduces this with the wasmtime cli and we can work on getting it fixed? |
Agree. Here is a program that reproduces this issue: Source code: use wasi::{
http::{
outgoing_handler,
types::{Fields, Method, OutgoingBody, RequestOptions, Scheme},
},
io::streams,
};
wasi::cli::command::export!(Example);
struct Example;
impl wasi::exports::cli::run::Guest for Example {
fn run() -> Result<(), ()> {
let fields = Fields::new();
let outgoing_request = outgoing_handler::OutgoingRequest::new(fields);
outgoing_request.set_method(&Method::Post).unwrap();
outgoing_request.set_scheme(Some(&Scheme::Https)).unwrap();
outgoing_request.set_authority(Some("httpbin.org")).unwrap();
outgoing_request.set_path_with_query(Some("/post")).unwrap();
let outgoing_body = outgoing_request.body().unwrap();
let body = [0; 5000];
let request_body = outgoing_body.write().unwrap();
let chunks = body.chunks(4096);
for chunk in chunks {
request_body
.blocking_write_and_flush(chunk)
.expect("writing response");
println!("writing response");
}
drop(request_body);
println!("finished");
OutgoingBody::finish(outgoing_body, None).unwrap();
let options = RequestOptions::new();
let future_response = outgoing_handler::handle(outgoing_request, Some(options)).unwrap();
let incoming_response = match future_response.get() {
Some(result) => result.unwrap(),
None => {
let pollable = future_response.subscribe();
pollable.block();
future_response
.get()
.expect("incoming response available")
.unwrap()
}
}
.unwrap();
drop(future_response);
let incoming_body = incoming_response.consume().unwrap();
drop(incoming_response);
let input_stream = incoming_body.stream().unwrap();
let input_stream_pollable = input_stream.subscribe();
let mut body = Vec::new();
loop {
input_stream_pollable.block();
let mut body_chunk = match input_stream.read(1024 * 1024) {
Ok(c) => c,
Err(streams::StreamError::Closed) => break,
Err(e) => panic!("input_stream read failed: {e:?}"),
};
if !body_chunk.is_empty() {
body.append(&mut body_chunk);
}
}
println!("body: {}", String::from_utf8(body).unwrap());
Ok(())
}
}
I'm not sure if my assumption is correct. This issue could be easily resolved by using an |
Thanks for providing the reproduction. I just spent time looking into this. The purpose of the bounds on the OutgoingBody OutputStream writer's buffer size and channel depth is to allow the host to maintain backpressure from the HTTP connection to the wasi stream. Backpressure only works when buffering is finite, so an unbounded_channel would break that. I agree we should go back and make the amount of buffering configurable as you saw from the TODOs removed in #9670. However, since buffering will always be finite, raising the bound doesn't resolve the issue with your guest code here, it just lifts the threshold where it hits. Currently, your guest makes the incorrect assumption that the wasi-http implementation will buffer the entire outgoing body prior to sending the request. The wasmtime wasi-http implementation will presently buffer up to 1 chunk of 1MB, but those limits are allowed to vary between implementations (and will more easily, once we land the configurability in #9670). Your guest must tolerate buffering as little as 1 chunk of 4k (the minimum guaranteed by a call to However, backpressure cannot be relieved until the request is sent. You should restructure your guest to first send the You can't be blamed from having made this mistake, since none of the docs really cover it (afaik), our own use wasi::{
http::{
outgoing_handler,
types::{Fields, Method, OutgoingBody, RequestOptions, Scheme},
},
io::streams,
};
wasi::cli::command::export!(Example);
struct Example;
impl wasi::exports::cli::run::Guest for Example {
fn run() -> Result<(), ()> {
let fields = Fields::new();
let outgoing_request = outgoing_handler::OutgoingRequest::new(fields);
outgoing_request.set_method(&Method::Post).unwrap();
outgoing_request.set_scheme(Some(&Scheme::Https)).unwrap();
outgoing_request.set_authority(Some("httpbin.org")).unwrap();
outgoing_request.set_path_with_query(Some("/post")).unwrap();
let outgoing_body = outgoing_request.body().unwrap();
println!("sending request");
let options = RequestOptions::new();
let future_response = outgoing_handler::handle(outgoing_request, Some(options)).unwrap();
let body = [0; 5000];
let request_body = outgoing_body.write().unwrap();
let chunks = body.chunks(4096);
for chunk in chunks {
request_body
.blocking_write_and_flush(chunk)
.expect("writing request body");
println!("wrote {} of request body", chunk.len());
}
drop(request_body);
OutgoingBody::finish(outgoing_body, None).unwrap();
println!("finished with request body");
let incoming_response = match future_response.get() {
Some(result) => result.unwrap(),
None => {
let pollable = future_response.subscribe();
pollable.block();
future_response
.get()
.expect("incoming response available")
.unwrap()
}
}
.unwrap();
drop(future_response);
let incoming_body = incoming_response.consume().unwrap();
drop(incoming_response);
let input_stream = incoming_body.stream().unwrap();
let input_stream_pollable = input_stream.subscribe();
let mut body = Vec::new();
loop {
input_stream_pollable.block();
let mut body_chunk = match input_stream.read(1024 * 1024) {
Ok(c) => c,
Err(streams::StreamError::Closed) => break,
Err(e) => panic!("input_stream read failed: {e:?}"),
};
if !body_chunk.is_empty() {
body.append(&mut body_chunk);
}
}
println!("body: {}", String::from_utf8(body).unwrap());
Ok(())
}
}
fn main() {} % wasmtime run -Shttp target/wasm32-wasip2/debug/issue9653.wasm
sending request
wrote 4096 of request body
wrote 904 of request body
finished with request body
body: {
"args": {},
"data": "\u0000\u0000\u0000\u0000\u0000\...snipped...",
"files": {},
"form": {},
"headers": {
"Host": "httpbin.org",
"Transfer-Encoding": "chunked",
"X-Amzn-Trace-Id": "Root=1-67477cb9-73a49f517d15aafc1afb91e7"
},
"json": null,
"origin": "98.232.174.29",
"url": "https://httpbin.org/post"
}
|
My scenario is simple: I want to send a request with a large body, but the
blocking_write_and_flush
method can only write 4096 bytes at a time. Naturally, I decided to call this method multiple times to write the entire body:However, I found that the program gets stuck forever on the second call to
blocking_write_and_flush
. After debugging, I found that it actually gets stuck during theready
check in the second call:wasmtime/crates/wasi-http/src/body.rs
Lines 662 to 669 in 5af8930
wasmtime/crates/wasi-http/src/body.rs
Line 472 in 5af8930
At this point, the writer has no capacity left, but my writing process hasn't finished, so the reader hasn't started consuming the data (?).
For my issue, I could stop using this method and instead combine
check-write
,subscribe
,write
, andflush
manually to solve it. However, I'm curious whether we are inclined to allow or not allow this behavior. Because the current behavior is strange—the program doesn't report an error, it just hangs indefinitely.The text was updated successfully, but these errors were encountered: