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 node-fetch v3, if the response body stream is destroyed before the request completes, the request is aborted. This is useful when piping the body into another stream using pipeline():
If an error occurs, pipeline() destroys response.body, which in turn aborts the request.
Unfortunately, this doesn't work in node-fetch v2.
Reproduction
This script requests a large file and immediately destroys the response body stream:
import{setTimeout}from'node:timers/promises'importfetchfrom'node-fetch'console.log("PID:",process.pid)// Any large file will doconsturl='https://upload.wikimedia.org/wikipedia/commons/5/58/Sunset_2007-1.jpg'constresponse=awaitfetch(url)response.body.destroy()awaitsetTimeout(100000)
With node-fetch v2, a socket stays open. (I verify this with ss -tnp | grep pid=PID)
With node-fetch v3, this doesn't happen.
Your Environment
software
version
node-fetch
2.6.11
node
16.14.2
npm
8.5.0
Operating System
Ubuntu 22.04
Additional context
As a workaround, one can use an AbortController to abort the request when the response body is closed. Something like:
In node-fetch v3, if the response body stream is destroyed before the request completes, the request is aborted. This is useful when piping the body into another stream using pipeline():
If an error occurs, pipeline() destroys response.body, which in turn aborts the request.
Unfortunately, this doesn't work in node-fetch v2.
Reproduction
This script requests a large file and immediately destroys the response body stream:
import{setTimeout}from'node:timers/promises'importfetchfrom'node-fetch'console.log("PID:",process.pid)// Any large file will doconsturl='https://upload.wikimedia.org/wikipedia/commons/5/58/Sunset_2007-1.jpg'constresponse=awaitfetch(url)response.body.destroy()awaitsetTimeout(100000)
With node-fetch v2, a socket stays open. (I verify this with ss -tnp | grep pid=PID)
With node-fetch v3, this doesn't happen.
Your Environment
software version
node-fetch 2.6.11
node 16.14.2
npm 8.5.0
Operating System Ubuntu 22.04 Additional context
As a workaround, one can use an AbortController to abort the request when the response body is closed. Something like:
In node-fetch v3, if the response body stream is destroyed before the request completes, the request is aborted. This is useful when piping the body into another stream using
pipeline()
:If an error occurs,
pipeline()
destroysresponse.body
, which in turn aborts the request.Unfortunately, this doesn't work in node-fetch v2.
Reproduction
This script requests a large file and immediately destroys the response body stream:
With node-fetch v2, a socket stays open. (I verify this with
ss -tnp | grep pid=PID
)With node-fetch v3, this doesn't happen.
Your Environment
Additional context
As a workaround, one can use an AbortController to abort the request when the response body is closed. Something like:
But it would be nicer if node-fetch did that automatically instead.
The text was updated successfully, but these errors were encountered: