Skip to content

Proxied requests hang when the outbound socket never emits 'connect' (breaks msw-mocked tests) #166

Closed

Description

@cuongluu8

Environment

  • httpxy 0.5.5 (via http-proxy-middleware 4.2.0)
  • msw 2.15.0 (msw/node)
  • Node 22.x, vitest

Problem

Every proxied request hangs indefinitely when the outbound connection is intercepted by msw's socket-level mock (@mswjs/interceptors). This makes httpxy-based proxies untestable under the common vitest + msw setup, and is the reason we had to stay on http-proxy-middleware v3 (node-http-proxy engine) instead of adopting v4 (httpxy engine) — same app code, same tests: v3 passes, v4 hangs on all 13 proxied-request tests.

Root cause

In src/middleware/web-incoming.ts, piping the incoming request into the outbound request is deferred until the outbound socket emits connect:

proxyReq.on("socket", (socket) => {
  if (socket.pending) {
    socket.on("connect", () => (options.buffer || req).pipe(proxyReq));
  } else {
    (options.buffer || req).pipe(proxyReq);
  }
});

msw's mock socket stays pending and never emits connect, so the pipe never starts. Since the pipe is also what propagates end to proxyReq, the outbound request is never finalized — even body-less GETs hang, and the client request never completes.

node-http-proxy pipes immediately without waiting for the socket, which is why it works under msw: Node's http.ClientRequest already buffers writes issued before the socket connects, so the deferral isn't needed for correctness.

Suggested fix

Pipe immediately (as node-http-proxy does), relying on ClientRequest's internal buffering:

(options.buffer || req).pipe(proxyReq);

If the connect deferral guards against a specific issue, an alternative would be to also start the pipe on ready/first writable state, or fall back when connect never fires.

Repro sketch

import { createServer } from "node:http";
import { createProxyServer } from "httpxy";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";

const msw = setupServer(
  http.get("http://upstream.test/ping", () => HttpResponse.json({ ok: true }))
);
msw.listen();

const proxy = createProxyServer({ target: "http://upstream.test" });
createServer((req, res) => proxy.web(req, res)).listen(3000);

// curl http://localhost:3000/ping -> hangs forever
// (same code with node-http-proxy responds 200)

Happy to open a PR if the maintainers agree on the direction.

Activity

  1. cuongluu8 commented on Sep 4, 2026

    @cuongluu8
    Author

    Follow-up after digging into the history: the suggested fix in my original report ("pipe immediately") is too naive, so amending it here.

    The connect-gated pipe was introduced deliberately in 7f74a06 (PR #111, "defer pipe until socket connects") to fix a data-loss race with large/chunked POST bodies, and it landed alongside the HTTP/2 listener support — where writing before the session is ready is genuinely unsafe. It's guarded by the streamed 8KB POST regression test. So simply removing the gate would trade this bug for that one.

    A couple of observations toward a fix that holds both:

    • For plain HTTP/1 outbound requests, Node's ClientRequest already buffers writes issued before the socket connects — that's the contract node-http-proxy relied on for a decade. If the data-loss race was specific to the HTTP/2 path (or to writing to the raw socket rather than through ClientRequest), the gate could be conditional on that path instead of applying to every request.
    • The current failure mode when connect never fires is a silent hang until timeout rather than a fast error. That's what every in-process interception library hits: msw's interceptor cannot emit connect early by design — it decides mock-vs-passthrough only after consuming the request body, which httpxy won't send until connect. Mutual deadlock, so no interceptor-side fix is possible (mswjs/interceptors documents this catch-22).

    For context on impact: this is why http-proxy-middleware v4's own msw e2e suite has to force followRedirects: true on every proxy (swapping to the follow-redirects transport, which buffers) — and why downstream consumers testing proxies with msw/nock can't move to httpxy-based engines at all without rewriting their tests against real servers.

    Happy to open a PR making the gate conditional (immediate pipe for HTTP/1 ClientRequest, keep the deferral where the race actually lives) if that direction sounds right to you — it would keep the PR #111 regression test green while restoring interceptability.

  2. cuongluu8 commented on Sep 4, 2026

    @cuongluu8
    Author

    Verified downstream: built main (3e68d30) into a tarball and ran it under http-proxy-middleware 4.2.0 against our Express BFF's vitest + msw server suite — the 13 proxied-route tests that previously hung (including 8 KB POST bodies through mocked upstreams) all pass with zero test changes. Thanks for the quick fix — looking forward to the next release.

  3. pi0x commented on Sep 4, 2026

    @pi0x
    Contributor

    Thanks for the follow-up — fixed in 3e68d30, which does pipe unconditionally.

    Two corrections on the history, since they change the conclusion:

    The retry case from #111 is genuinely lost, and it can't be kept: under msw the mock socket is indistinguishable from a real connecting one (pending, connecting, readyState, _handle, _handle.fd all identical), so there's no condition to gate on. Regression coverage added with a mock socket that connects only after the request is written, so this stays fixed.

    Your followRedirects: true observation checks out, by the way — verified against the pre-fix code: plain hangs, followRedirects responds, because that path tees the body instead of waiting on connect.

    🤖 Written with AI assistance; claims above verified against the repo.

  4. cuongluu8 commented on Sep 15, 2026

    @cuongluu8
    Author

    When will this change be released? @pi0x

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions