From 576d0db372c3f4c9b01caecdbe2360a73de6d36d Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Sun, 20 Aug 2023 19:35:26 -0600 Subject: fix(ext/http): ensure request body resource lives as long as response is alive (#20206) Deno.serve's fast streaming implementation was not keeping the request body resource ID alive. We were taking the `Rc` from the resource table during the response, so a hairpin duplex response that fed back the request body would work. However, if any JS code attempted to read from the request body (which requires the resource ID to be valid), the response would fail with a difficult-to-diagnose "EOF" error. This was affecting more complex duplex uses of `Deno.fetch` (though as far as I can tell was unreported). Simple test: ```ts const reader = request.body.getReader(); return new Response( new ReadableStream({ async pull(controller) { const { done, value } = await reader.read(); if (done) { controller.close(); } else { controller.enqueue(value); } }, }), ``` And then attempt to use the stream in duplex mode: ```ts async function testDuplex( reader: ReadableStreamDefaultReader, writable: WritableStreamDefaultWriter, ) { await writable.write(new Uint8Array([1])); const chunk1 = await reader.read(); assert(!chunk1.done); assertEquals(chunk1.value, new Uint8Array([1])); await writable.write(new Uint8Array([2])); const chunk2 = await reader.read(); assert(!chunk2.done); assertEquals(chunk2.value, new Uint8Array([2])); await writable.close(); const chunk3 = await reader.read(); assert(chunk3.done); } ``` In older versions of Deno, this would just lock up. I believe after 23ff0e722e3c4b0827940853c53c5ee2ede5ec9f, it started throwing a more explicit error: ``` httpServerStreamDuplexJavascript => ./cli/tests/unit/serve_test.ts:1339:6 error: TypeError: request or response body error: error reading a body from connection: Connection reset by peer (os error 54) at async Object.pull (ext:deno_web/06_streams.js:810:27) ``` --- ext/http/00_serve.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'ext/http/00_serve.js') diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js index 265b79706..7307ab2d8 100644 --- a/ext/http/00_serve.js +++ b/ext/http/00_serve.js @@ -133,10 +133,6 @@ class InnerRequest { } close() { - if (this.#streamRid !== undefined) { - core.close(this.#streamRid); - this.#streamRid = undefined; - } this.#slabId = undefined; } -- cgit v1.2.3