Skip to content

1.4.0: http.ClientRequest timeout never arms while socket is connecting to an unreachable host (regression) #41133

Closed

Description

@tomjaimz

What version of Bun is running?

1.4.0

What platform is your computer?

Linux x64 (confirmed reproducible; not yet tested on macOS/Windows)

What steps can reproduce the bug?

Minimal repro using only node:http — no third-party dependencies:

import http from 'node:http';

const start = Date.now();
const req = http.request('http://127.0.0.1:1/v1/metrics', { method: 'POST', timeout: 3000 }, (res) => {
  console.log('got response', res.statusCode, 'at +', Date.now() - start, 'ms');
});
req.on('timeout', () => {
  console.log('timeout event fired at +', Date.now() - start, 'ms');
  req.destroy(new Error('timeout'));
});
req.on('error', (err) => {
  console.log('error event:', err.message, 'at +', Date.now() - start, 'ms');
  process.exit(0);
});
req.end('{}');

setTimeout(() => {
  console.log('watchdog: still not settled at +', Date.now() - start, 'ms');
  process.exit(1);
}, 10000);

Target http://127.0.0.1:1 is a loopback address on port 1 — nothing listens there, so the connection is refused (chosen specifically to rule out DNS as a variable).

What is the expected behavior?

The timeout event should fire at ~3000ms (matching the timeout: 3000 option), the same way it does on Bun 1.3.14 (confirmed: fires at exactly ~3001ms there) and in Node.js.

What do you see instead?

On Bun 1.4.0, the timeout event never fires. The request hangs indefinitely — confirmed past a 10s watchdog with zero output after the initial log line, reproduced deterministically across multiple runs.

Separately confirmed this is specific to node:http's timeout option, not a general Bun networking regression: a bare fetch() + AbortSignal.timeout(3000) against the same refused target correctly rejects with TimeoutError at ~3003ms on 1.4.0.

Additional information

We hit this via @opentelemetry/otlp-exporter-base, whose HTTP transport (http-transport-utils.js) uses classic http.request + req.on('timeout') (not fetch) to export metrics — a shutdown()/forceFlush() call against an unreachable OTLP endpoint hangs indefinitely instead of timing out and rejecting, only on Bun 1.4.0. Not observed on Bun 1.3.14 with the same code.

Looked through the 1.3.14→1.4.0 diff for context and found active, related churn in the low-level socket error/EOF routing during this release cycle — notably #39600 and its same-cycle follow-up #39621, the latter specifically about a refused-connection ("port nothing listens on") scenario reporting the wrong error code due to a stale SO_ERROR read on a reused fd. Neither matches this repro's exact scenario (no prior socket activity or fd reuse involved here), so I can't point to a specific causing commit, but it's circumstantial evidence this general area was unstable during the same release.

Activity

  1. tomjaimz commented on Sep 1, 2026

    @tomjaimz
    Author

    Correction (2026-09-01)

    My original repro target (http://127.0.0.1:1) was misleading — on this specific machine (WSL2 with mirrored networking), an unbound loopback port is silently dropped by the Windows host rather than actually refused (no RST is sent). So my repro was testing a SYN black hole, not a refused connection. In a genuinely refused-connection scenario (verified in an isolated network namespace), ECONNREFUSED fires instantly on every Bun version I tested (1.3.14 and 1.4.0) — no bug there. Sorry for the noise on that framing.

    The real, narrower, verified regression: http.ClientRequest's timeout — via either the { timeout: N } constructor option or an explicit req.setTimeout(N) call — does not arm while the socket is still connecting to an unreachable (black-holed / silently-dropped) target, on Bun 1.4.0. Once the socket is connected (e.g. a server that accepts the TCP connection but never replies), the same timeout mechanism works correctly on 1.4.0. A raw net.connect(...).setTimeout(N) (no http.request wrapper) also correctly arms its pre-connect timeout on 1.4.0 — so this looks specific to ClientRequest's socket-assignment path, not the underlying socket layer.

    Corrected repro — target a black-holed/unreachable address rather than an actually-refused one (e.g. a non-routable IP like 10.255.255.1, or a host with an iptables DROP rule on the port) so the reproduction doesn't depend on OS-specific "refused vs. dropped" semantics:

    import http from 'node:http';
    const start = Date.now();
    const req = http.request('http://10.255.255.1:1/', { method: 'GET', timeout: 3000 }, (res) => {
      console.log('got response', res.statusCode, 'at +', Date.now() - start, 'ms');
    });
    req.on('timeout', () => {
      console.log('timeout event fired at +', Date.now() - start, 'ms');
      req.destroy(new Error('timeout'));
    });
    req.on('error', (err) => {
      console.log('error event:', err.message, 'at +', Date.now() - start, 'ms');
      process.exit(0);
    });
    req.end();
    setTimeout(() => { console.log('watchdog: still not settled'); process.exit(1); }, 10000);

    Expected (Node.js): timeout fires at ~3000ms.
    Bun 1.4.0: never fires, hangs past the watchdog.

    Also worth noting as a separate, secondary issue: on Bun 1.3.14, the timeout event does fire correctly against a black-holed target, but calling req.destroy(new Error(...)) from the timeout handler never causes the error event to fire afterward — so 1.3.14 isn't a fully clean baseline either, just affected differently.

  2. changed the title [-]1.4.0: node:http 'timeout' event never fires for a refused connection (http.request timeout option)[/-] [+]1.4.0: http.ClientRequest timeout never arms while socket is connecting to an unreachable host (regression)[/+] on Sep 1, 2026
  3. robobun commented on Sep 1, 2026

    @robobun
    Collaborator

    Reproduced on 1.4.0, 1.4.1, and main. One correction: a genuinely refused connection fires the error event with ECONNREFUSED in milliseconds on every version. The hang happens when the connect stalls (the SYN is dropped, common behind a firewall) while the request body is queued. Socket._onTimeout in node:net suppresses the timeout whenever a write is pending, and it never re-arms the timer, so the timeout can never fire.

    Fix in progress: #35813. It matches Node behavior: suppress only while the write queue makes progress, and fire on a stall.

  4. robobun commented on Sep 12, 2026

    @robobun
    Collaborator

    Update: the fix for this issue now lives in #42399 by @thatdoogieguy. I closed #35813 in favour of it, because both PRs change the same guard in Socket.prototype._onTimeout. I built main with #42399 and #42404 merged. On that build http.request({ timeout }) fires the timeout event at the requested interval while the socket is still connecting. I asked the author to add Fixes #41133 to that PR.

  5. robobun commented on Nov 17, 2026

    @robobun
    Collaborator

    This is fixed on main.

    What I ran, on Linux x64: the script from the report and the corrected script from the first comment, both unchanged. For the first script, a listener with a full backlog makes 127.0.0.1:1 drop every SYN. The corrected script ran in a network namespace where 10.255.255.1 drops every packet. In both cases the connect stays in progress.

    • Bun v1.4.0 and v1.4.4: neither script prints a timeout line. Each run ends at the 10 s watchdog.
    • main (bd0219ed6, the canary build): both scripts print timeout event fired at about 3005 ms, then error event: timeout, in three runs out of three. Node v26.3.0 prints the same.

    One related case stays as it is, because Node does the same: req.setTimeout() does not cover the connect phase. Node starts that timer on the connect event of the socket (setSocketTimeout).

    The OTLP exporter sets its timeout this way, on a request with its own agent. I ran forceFlush() of @opentelemetry/exporter-metrics-otlp-http 0.301.2 to the same address, with timeoutMillis: 3000. It ends when the kernel gives up the connect, after about 129 s on main and on Node v26.3.0. With httpAgentOptions: { keepAlive: true, timeout: 3000 } it ends with Request timed out at about 3000 ms on both. On v1.4.4 that option has no effect.

    No release has the fix yet. The canary build has it (bun upgrade --canary), and it ships in the next release.
    The fix is #47836.

    If this still fails for you on the latest Bun, comment here with the version and the output, and the issue can be reopened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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