Skip to content

HTTP Keep-alive, "socket hang up" ECONNERR on a long-running second requestΒ #6113

@snarky-puppy

Description

@snarky-puppy

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

  1. "socket hang up" is misleading because it indicates the server closed the connection (in this bug the opposite is true)
  2. 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

tcpdump

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions