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

fix(#10182): hang during http server response #10197

Merged
merged 1 commit into from
Apr 15, 2021
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
27 changes: 27 additions & 0 deletions cli/tests/unit/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,30 @@ unitTest(
client.close();
},
);

unitTest(
{ perms: { net: true } },
async function httpServerRegressionHang() {
const promise = (async () => {
const listener = Deno.listen({ port: 4501 });
const conn = await listener.accept();
const httpConn = Deno.serveHttp(conn);
const event = await httpConn.nextRequest();
assert(event);
const { request, respondWith } = event;
const reqBody = await request.text();
assertEquals("request", reqBody);
await respondWith(new Response("response"));
httpConn.close();
listener.close();
})();

const resp = await fetch("http://127.0.0.1:4501/", {
method: "POST",
body: "request",
});
const respBody = await resp.text();
assertEquals("response", respBody);
await promise;
},
);
2 changes: 1 addition & 1 deletion runtime/js/40_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
zeroCopyBuf = null;
}

const responseBodyRid = Deno.core.opSync("op_http_response", [
const responseBodyRid = await Deno.core.opAsync("op_http_response", [
responseSenderRid,
resp.status ?? 200,
flattenHeaders(resp.headers),
Expand Down
34 changes: 23 additions & 11 deletions runtime/ops/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_async(rt, "op_http_request_next", op_http_request_next);
super::reg_async(rt, "op_http_request_read", op_http_request_read);

super::reg_sync(rt, "op_http_response", op_http_response);
super::reg_async(rt, "op_http_response", op_http_response);
super::reg_async(rt, "op_http_response_write", op_http_response_write);
super::reg_async(rt, "op_http_response_close", op_http_response_close);
}
Expand Down Expand Up @@ -312,23 +312,28 @@ struct RespondArgs(
Vec<String>,
);

fn op_http_response(
state: &mut OpState,
async fn op_http_response(
state: Rc<RefCell<OpState>>,
args: RespondArgs,
data: Option<ZeroCopyBuf>,
) -> Result<Option<ResourceId>, AnyError> {
let rid = args.0;
let status = args.1;
let headers = args.2;
let RespondArgs(rid, status, headers) = args;

let response_sender = state
.borrow_mut()
.resource_table
.take::<ResponseSenderResource>(rid)
.ok_or_else(bad_resource_id)?;
let response_sender = Rc::try_unwrap(response_sender)
.ok()
.expect("multiple op_http_respond ongoing");

let conn_resource = state
.borrow()
.resource_table
.get::<ConnResource>(response_sender.conn_rid)
.ok_or_else(bad_resource_id)?;

let mut builder = Response::builder().status(status);

debug_assert_eq!(headers.len() % 2, 0);
Expand All @@ -348,11 +353,12 @@ fn op_http_response(
let (sender, body) = Body::channel();
res = builder.body(body)?;

let response_body_rid = state.resource_table.add(ResponseBodyResource {
body: AsyncRefCell::new(sender),
cancel: CancelHandle::default(),
conn_rid: response_sender.conn_rid,
});
let response_body_rid =
state.borrow_mut().resource_table.add(ResponseBodyResource {
body: AsyncRefCell::new(sender),
cancel: CancelHandle::default(),
conn_rid: response_sender.conn_rid,
});

Some(response_body_rid)
};
Expand All @@ -364,6 +370,12 @@ fn op_http_response(
return Err(type_error("internal communication error"));
}

poll_fn(|cx| match conn_resource.poll(cx) {
Poll::Ready(x) => Poll::Ready(x),
Poll::Pending => Poll::Ready(Ok(())),
})
.await?;

Ok(maybe_response_body_rid)
}

Expand Down