summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-09-25 09:23:55 -0600
committerGitHub <noreply@github.com>2023-09-25 17:23:55 +0200
commita27ee8f368dbac33141fdcb9a17d0e4ea907b8ef (patch)
tree4bc1bfa1e23b40bfb726cd5b179091393533dec2 /ext
parent83f20007aac0f9ebb0eb59b71a932e7a91d5d9a7 (diff)
fix(ext/http): ensure that resources are closed when request is cancelled (#20641)
Builds on top of #20622 to fix #10854
Diffstat (limited to 'ext')
-rw-r--r--ext/http/http_next.rs11
-rw-r--r--ext/http/response_body.rs34
-rw-r--r--ext/web/stream_resource.rs18
3 files changed, 58 insertions, 5 deletions
diff --git a/ext/http/http_next.rs b/ext/http/http_next.rs
index 21e138f86..7ccd9ec81 100644
--- a/ext/http/http_next.rs
+++ b/ext/http/http_next.rs
@@ -577,10 +577,13 @@ fn ensure_vary_accept_encoding(hmap: &mut HeaderMap) {
);
}
+/// Sets the appropriate response body. Use `force_instantiate_body` if you need
+/// to ensure that the response is cleaned up correctly (eg: for resources).
fn set_response(
slab_id: SlabId,
length: Option<usize>,
status: u16,
+ force_instantiate_body: bool,
response_fn: impl FnOnce(Compression) -> ResponseBytesInner,
) {
let mut http = slab_get(slab_id);
@@ -602,7 +605,10 @@ fn set_response(
if let Ok(code) = StatusCode::from_u16(status) {
*response.status_mut() = code;
}
+ } else if force_instantiate_body {
+ response_fn(Compression::None).abort();
}
+
http.complete();
}
@@ -634,6 +640,7 @@ pub fn op_http_set_response_body_resource(
slab_id,
resource.size_hint().1.map(|s| s as usize),
status,
+ true,
move |compression| {
ResponseBytesInner::from_resource(compression, resource, auto_close)
},
@@ -649,7 +656,7 @@ pub fn op_http_set_response_body_text(
status: u16,
) {
if !text.is_empty() {
- set_response(slab_id, Some(text.len()), status, |compression| {
+ set_response(slab_id, Some(text.len()), status, false, |compression| {
ResponseBytesInner::from_vec(compression, text.into_bytes())
});
} else {
@@ -665,7 +672,7 @@ pub fn op_http_set_response_body_bytes(
status: u16,
) {
if !buffer.is_empty() {
- set_response(slab_id, Some(buffer.len()), status, |compression| {
+ set_response(slab_id, Some(buffer.len()), status, false, |compression| {
ResponseBytesInner::from_bufview(compression, BufView::from(buffer))
});
} else {
diff --git a/ext/http/response_body.rs b/ext/http/response_body.rs
index 5c946a4d3..4f7e3b0a5 100644
--- a/ext/http/response_body.rs
+++ b/ext/http/response_body.rs
@@ -125,6 +125,16 @@ pub enum ResponseStream {
TestChannel(tokio::sync::mpsc::Receiver<BufView>),
}
+impl ResponseStream {
+ pub fn abort(self) {
+ match self {
+ ResponseStream::Resource(resource) => resource.stm.close(),
+ #[cfg(test)]
+ ResponseStream::TestChannel(..) => {}
+ }
+ }
+}
+
#[derive(Default)]
pub enum ResponseBytesInner {
/// An empty stream.
@@ -192,11 +202,25 @@ impl ResponseBytes {
let current = std::mem::replace(&mut self.inner, ResponseBytesInner::Done);
self.completion_handle.complete(success);
- current
+ if success {
+ current
+ } else {
+ current.abort();
+ ResponseBytesInner::Done
+ }
}
}
impl ResponseBytesInner {
+ pub fn abort(self) {
+ match self {
+ Self::Done | Self::Empty | Self::Bytes(..) => {}
+ Self::BrotliStream(stm) => stm.abort(),
+ Self::GZipStream(stm) => stm.abort(),
+ Self::UncompressedStream(stm) => stm.abort(),
+ }
+ }
+
pub fn size_hint(&self) -> SizeHint {
match self {
Self::Done => SizeHint::with_exact(0),
@@ -463,6 +487,10 @@ impl GZipResponseStream {
underlying,
}
}
+
+ pub fn abort(self) {
+ self.underlying.abort()
+ }
}
/// This is a minimal GZip header suitable for serving data from a webserver. We don't need to provide
@@ -645,6 +673,10 @@ impl BrotliResponseStream {
underlying,
}
}
+
+ pub fn abort(self) {
+ self.underlying.abort()
+ }
}
fn max_compressed_size(input_size: usize) -> usize {
diff --git a/ext/web/stream_resource.rs b/ext/web/stream_resource.rs
index e19954fdc..b35d4c302 100644
--- a/ext/web/stream_resource.rs
+++ b/ext/web/stream_resource.rs
@@ -364,6 +364,15 @@ impl ReadableStreamResource {
.read(limit)
.map(|buf| buf.unwrap_or_else(BufView::empty))
}
+
+ fn close_channel(&self) {
+ // Trigger the promise in JS to cancel the stream if necessarily
+ self.data.completion.complete(true);
+ // Cancel any outstanding read requests
+ self.cancel_handle.cancel();
+ // Close the channel to wake up anyone waiting
+ self.channel.close();
+ }
}
impl Resource for ReadableStreamResource {
@@ -376,8 +385,13 @@ impl Resource for ReadableStreamResource {
}
fn close(self: Rc<Self>) {
- self.cancel_handle.cancel();
- self.channel.close();
+ self.close_channel();
+ }
+}
+
+impl Drop for ReadableStreamResource {
+ fn drop(&mut self) {
+ self.close_channel();
}
}