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
I am attempting to make a SvelteKit API endpoint that makes a request to another backend API and streams the response body of its own response body, using fetch and response.body, like so:
// `filterHeaders` is simply a function to filter only some desired headers./** @type {import('./$types').RequestHandler} */exportasyncfunctionGET(event){constrequestHeaders=filterHeaders(event.request.headers,['range']);constresponse=awaitfetch('http://example.com/some/example/endpoint',{signal: event.request.signal,headers: requestHeaders,});constresponseHeaders=filterHeaders(response.headers,['content-length','content-type','content-range','accept-ranges'],);returnnewResponse(response.body,{status: response.status,statusText: response.statusText,headers: responseHeaders,});}
While this code works, it seems to leak a file descriptor each time it is requested, especially in the context of HTTP range requests while playing video content.
I have tried many variations of this code, like using pipeThrough or pipeTo, using the pipeline function from the node:stream/promises module, or using axios to make the request and converting the body back to a ReadableStream, but all of them demonstrated the issue, which prompts to me that maybe its the @sveltejs/adapter-node that doesn't close the response stream properly.
After some time, this issue leads to my app being unable to fulfil new requests, and the node process must be restarted.
Reproduction
I have made a minimal demo app to demonstrate the issue here:
Describe the bug
I am attempting to make a SvelteKit API endpoint that makes a request to another backend API and streams the response body of its own response body, using
fetch
andresponse.body
, like so:While this code works, it seems to leak a file descriptor each time it is requested, especially in the context of HTTP range requests while playing video content.
I have tried many variations of this code, like using
pipeThrough
orpipeTo
, using thepipeline
function from thenode:stream/promises
module, or usingaxios
to make the request and converting the body back to aReadableStream
, but all of them demonstrated the issue, which prompts to me that maybe its the@sveltejs/adapter-node
that doesn't close the response stream properly.After some time, this issue leads to my app being unable to fulfil new requests, and the node process must be restarted.
Reproduction
I have made a minimal demo app to demonstrate the issue here:
https://github.com/Hirevo/sveltekit-stream-issue
The index page has steps of how to observe the issue, using
lsof
.Logs
No response
System Info
Severity
annoyance
Additional Information
From some of my research on this, I am aware that
undici
, which implementsfetch
in Node, does not release connection resources upon garbage collection.I made multiple attempts to close it myself, but I was unsuccessful.
The text was updated successfully, but these errors were encountered: