You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In a +server.ts, returning a Response with body of type ReadableStream works....but does not close the ReadableStream. This causes the stream to stay open, never de-allocated by the GC.
Reproduction
exportconstGET=async()=>{constresponse=awaitfetch('https://example.com');if(!response.ok){error(404,'Not found');}consth=response.headers;constkeys=['content-type','content-length','last-modified','date','etag','age'];constheaders: Record<string,string>={};for(constkeyofkeys){constval=h.get(key);if(val){headers[key]=val;}}returnnewResponse(response.body,{// <-- this works fine, but doesn't close `response.body` automatically
headers
});}
I discovered this after seeing my app's memory usage trending up. After doing a heapdump, I saw thousands of unreleased Client object (i.e.: the fetch). To test my theory, I changed the code to:
+ const body = await response.arrayBuffer();+ return new Response(body, {- return new Response(response.body, {
headers
});
}
This works, which I expected because the arrayBuffer (and json()/text()) consumes the stream completely. However, if the stream is huge, this will pose a problem.
Question: What's the proper way to pass a stream to Response?
The text was updated successfully, but these errors were encountered:
Discussed in #12847
Originally posted by devcsrj September 12, 2024
Describe the bug
In a
+server.ts
, returning aResponse
with body of typeReadableStream
works....but does not close theReadableStream
. This causes the stream to stay open, never de-allocated by the GC.Reproduction
Logs
System Info
Severity
serious, but I can work around it
Additional Information
I discovered this after seeing my app's memory usage trending up. After doing a heapdump, I saw thousands of unreleased
Client
object (i.e.: thefetch
). To test my theory, I changed the code to:This works, which I expected because the
arrayBuffer
(andjson()
/text()
) consumes the stream completely. However, if the stream is huge, this will pose a problem.Question: What's the proper way to pass a stream to
Response
?The text was updated successfully, but these errors were encountered: