HTTP Keep-alive, "socket hang up" ECONNERR on a long-running second request
Describe the bug
On keep-alive connections, if a timeout value is given to the first request but not on subsequent requests, the subsequent requests will eventually throw a "socket hang up" error.
To Reproduce
const http = require('http');
const axios = require('axios');
describe('socket hang up', () => {
let server;
beforeAll(done => {
server = http.createServer(async (req, res) => {
if (req.url === '/wait') {
await new Promise(resolve => setTimeout(resolve, 5000));
}
res.end('ok');
});
server.listen(3000, done);
});
afterAll(done => {
server.close(done);
});
it('will fail with "socket hang up"', async () => {
const baseURL = `http://localhost:3000`;
await axios.get('/1', {baseURL, timeout: 1000});
await axios.get(`/wait`, {baseURL, timeout: 0});
}, 15000);
});
Code snippet
No response
Expected behavior
- "socket hang up" is misleading because it indicates the server closed the connection (in this bug the opposite is true)
- One would not expect that a timeout would keep running after a request has completed. This is a (quite major IMO) footgun.
Axios Version
0.22.0 - 1.6.2
Adapter Version
HTTP
Browser
No response
Browser Version
No response
Node.js Version
20.9.0
OS
OSX 14
Additional Library Versions
No response
Additional context/Screenshots
We're also affected by this bug.
Cheers! I am affected with same bug, but can only reproduce it at Node 20.10, fallback to Node 18.18 works fine.
The cause of this issue is the way follow-redirects implements timeout. It has problems when dealing with keep-alive connections. A simple fix is to remove the timeout event handler when the request finishes, but I'm not sure this will work for all use-cases, as that library is mostly geared towards earlier node versions.
Same issue here. Is there any workaround except a dumb retry?
Same issue here. Is there any workaround except a dumb retry?
@NaZaRKIN123 Something like this would work:
axios.defaults.httpAgent = new Agent({ keepAlive: false });
I can confirm that the workaround provided by @snarky-puppy worked for us 🙏
Thanks for the detail investigation. The keepAlive is needed on my project in order to reduce latency
In case we disable follow-redirects, would that be solved?
+1
+1
+1
+1
Cheers! I am affected with same bug, but can only reproduce it at Node 20.10, fallback to Node 18.18 works fine.
This is because of #6536. Short summary: Node.js changed its default Keep-Alive behavior after Node.js 18.