-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting Premature close
errors unexpectedly but very reliably in specific circumstances
#1576
Comments
I believe this edge case is also happening for us (see discussion I started #1614). It occurs on less than 1% of the requests, but when it happens, it is usually reproducible for a period of time, until either the inputs change or the data in the response changes (due to a back-end data source change). In our environment:
@jmurty Your theory about specific content lengths triggering this makes sense. Are your API responses gzipped as well? I'd like to rule that out as a culprit. We are going to push a production update soon that adds the keep-alive agent and a backoff retry when this error occurs, to see if that helps. I don't know enough about network sockets and packets to know if keep-alive or waiting a second or two before trying again has any effect on the content length or how the packets are processed on the next request. |
Hi @cyruscollier I don't have any more insight into this issue since we "solved" it by switching to the Axios client at the time I opened this issue, but I can confirm:
For what it's worth request retries didn't help in our case. We saw the problem when fetching slow-changing data for specific users, and for these problem requests we would get exactly the same error no matter how many times we retried or when. I could even reproduce the problem by running the same GET request manually in a small script on the server. |
Thanks for the additional info, @jmurty. It certainly sounds like the same problem. I hope we won't have to swap Node Fetch for Axios for connecting to our API, but that'll be our next course of action for sure. |
I have one example where the 3/2 assumption does not hold true. On a current request, the LAST_CHUNK marker is split like so
So both tests to calculate I patched my version and this does the trick. If the last buffer has size smaller then 5, then some bytes of that marker are expected to be in the previous chunk. So we create a small buffer with some last bytes from the previous chunk concatenated with the current buffer and do that test on that. const onData = buf => {
properLastChunkReceived = Buffer.compare(buf.slice(-5), LAST_CHUNK) === 0;
// Sometimes final 0-length chunk and end of message code are in separate packets
if (!properLastChunkReceived && previousChunk) {
if (buf.length < 5) {
properLastChunkReceived = Buffer.compare(Buffer.from([...previousChunk.slice(-5), ...buf]).slice(-5), LAST_CHUNK) === 0;
}
}
previousChunk = buf;
}; |
We also have the same problem, on a specific content (maybe due to to it's length ?) we have ~1% of the GET request that are failing while the request is working (as we can diagnose from the loadbalancer logs). We tested the @Treverix patch and it's working well now, thanks! |
If you folks are using an |
We see that for some fetches of certain lengths after certain amount of parallelism, we see "Premature close" errors as documented in this issue: node-fetch/node-fetch#1576 We are applying the suggested fix in this PR: Treverix/node-fetch@625fd38?diff=split We do so by using patch-package to path our node-fetch-cjs, an then inlining a copy of node-fetch-cjs into our built files.
In our case, errors also get thrown when the request is aborted. |
Hi,
Thanks for this great library!
We have been getting
Premature close
errors on our production server when calling API endpoints.These errors are entirely repeatable for the same API request with a given input and output – for example looking up details for a specific user – while API requests to the same endpoint with different inputs/outputs work as expected.
The errors occur only on our production Linux server (of course) and are not reproducible on our development machines (macOS), at least not for the specific users that trigger the error on the server.
I have not dug deeply enough to be sure, but it smells to me like faulty handling of chunked responses that happen to trigger a boundary condition due to the packet size or chunk read size on our server.
I am suspicious of the code here
node-fetch/src/index.js
Lines 391 to 399 in 043a5fc
The same API requests that trigger
Premature close
with node-fetch on the production server succeed if we use the Axios HTTP client library instead, and if we make the requests on the command line withcurl
.Reproduction
Steps to reproduce the behavior:
Premature close
errorExpected behavior
Complete the request and return the response body.
Your Environment
Additional context
Requests to the same API endpoints and data request/responses work correctly on dev machines (macOS) and with alternative HTTP clients (Axios, curl)
The text was updated successfully, but these errors were encountered: