Search

Explore Fieldwork

Writing / Field note

localhost was not the forward

A healthy port-forward reached the wrong listener when localhost resolved to IPv6.

A lime signal splits into two glass loops: the upper loop carries the bright lime path while the lower loop glows amber.
Conceptual illustration of two loopback routes reaching different listeners.

A kubectl port-forward can be healthy and unreachable at the same time. The difference is which localhost you mean.

While seeding a staging database through a forwarded pgbouncer, both psql and a Node.js driver timed out on what looked like a working tunnel. The forward process was alive, the pod was reachable, and the same command had succeeded minutes earlier. Restarting the forward sometimes helped for one connection, then the next one hung again.

The first suspicion was the tunnel itself: SPDY streams through a load-balanced Kubernetes API do flap, and the forward logs showed intermittent lost connections. But that did not explain why a freshly verified forward refused the very first connection. The break came from a different layer entirely. On this machine, Docker Desktop holds an IPv6 wildcard listener on the same port number that had been chosen for the forward. kubectl bound its listener on the IPv4 loopback without error, because nothing occupied 127.0.0.1. The name localhost, however, resolves to ::1 first on macOS. Every client that asked for localhost was connected into Docker's unrelated listener instead of the forward, and whatever answered there simply black-holed the protocol.

The decisive check was direct: the forward worked through 127.0.0.1 and failed through localhost. Same command, same second, different address family. lsof showed which process owned the IPv6 listener.

The decision is mechanical and I now apply it everywhere: forwards bind and are consumed on explicit 127.0.0.1, never on localhost, and the port is checked on both families before use. The deeper lesson is older than the tool. localhost is a name with a resolution order, not a socket. Any setup that treats it as a single endpoint is one wildcard listener away from connecting to the wrong service while every health check looks correct.

What remains uncertain: whether the Docker listener intentionally accepts or silently drops these connections probably depends on its internal routing, and I did not need to finish that analysis. The correct fix was not to understand the black hole, but to stop falling into it.

Two loopbacks, two listeners

Select an address to see where the request goes.

Loopback address
Client address
::1
Result
The request reaches the other listener.
Client address
127.0.0.1
Result
The request reaches the port-forward.

Share this article

Continue reading

Why I keep an issue log ↗

All writing ↗