From 94cbb8a3c29b3827448fd885849577a43efdee38 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Tue, 21 May 2024 11:45:33 -0600 Subject: [PATCH] fix(ext/web): fix potential leak of unread buffers (#23923) Because the buffers are `MaybeUninit`, and the owner of the `BoundedBufferChannel` is not obligated to read each and every bit of data, we may find that some buffers were not automatically dropped if unread by the time the `BoundedBufferChannelInner` is dropped. Possible repro: ``` Deno.serve(() => new Response(new ReadableStream({ start(controller) { controller.enqueue(new Uint8Array(100_000_000)) } }))); ``` ```bash while true; do curl localhost:8000 | dd count=1; done ``` --- ext/web/stream_resource.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ext/web/stream_resource.rs b/ext/web/stream_resource.rs index 54ebf775ac..78487883b6 100644 --- a/ext/web/stream_resource.rs +++ b/ext/web/stream_resource.rs @@ -66,6 +66,13 @@ impl Default for BoundedBufferChannelInner { } } +impl Drop for BoundedBufferChannelInner { + fn drop(&mut self) { + // If any buffers remain in the ring, drop them here + self.drain(std::mem::drop); + } +} + impl std::fmt::Debug for BoundedBufferChannelInner { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_fmt(format_args!(