Why Chrome still uses the old IP after you edit your hosts file

In my work I often need to be able to point real domains to my local web server. So I start my local webserver, add 127.0.0.1 app.example.com to my hosts file and refresh the page. But as you have probably experienced, if you have first let Chrome connect to the real IP of app.example.com, and then edit your hosts file, Chrome still uses the real IP. And it’s annoyingly sticky.

No matter of “hard-refreshes” help. Most tips online involve forcing the OS to flush DNS, or Clear host cache on chrome://net-internals/#dns

Those are not the real issue though. The reason for all this is that Chrome keeps a pool of open connections to all hosts you’ve used any time in the last 5 minutes. It does this because opening HTTPS connections is expensive. The pool is keyed on hostname and port, not IP, so Chrome doesn’t even try to look up the host name again as long as it has a connection open in the pool ready to go.

The pool is not even per tab, but it is tied to your entire Chrome profile (so an incognito window will use the correct IP from your hosts file)

A used connection is kept for five minutes of idle time. It’s a constant in the Chromium source:

int64_t g_used_idle_socket_timeout_s = 300;  // 5 minutes

Note that this is idle time. So pressing F5 keeps the connection alive even longer.

I have also been unable to find a setting in Chrome to disable this pool.

The solution, if you accept doing it manually, is to go to chrome://net-internals/#sockets, and then click Flush socket pools.

This closes every open connection in your profile, including any unrelated long-running connections you might have in other tabs.

Doing this by hand on every switch gets old. Hosts Tray, a hosts file switcher I built, does it automatically: when you switch, it closes only the stale connections, so Chrome picks up the new IP right away and your other tabs stay connected. Here’s how that works.

Automating it

If you don’t want to do that by hand after every hosts edit, or don’t want every tab’s connections killed, there’s another way If you don’t appreciate the manual labor of flushing the socket pool inside Chrome manually each time you edit your hosts file, or you don’t want all connections in all of your open tabs killed and have to reconnect, you might want to an alternative.

I got you.

Chrome won’t let another program touch its pool. But if the connection is killed on the OS level, Chrome’s next request on that connection fails, so Chrome opens a new connection and in that process does a new lookup.

We can automate this in an external tool.

This is the plan:

  1. Before the change: look up the IP of the hostname(s) that will be changed/added/removed in hosts file.
  2. Do the change to the hosts file.
  3. On the OS level, find all open connections to the original IP(s) we got from (1) and kill them.

Killing the connections

On Windows we can use the GetTcpTable API call to list the connections and then, using the SetTcpEntry API call, we can set a connection to the state “delete”.

Pseudo Go code:

for _, row := range rows {
    port := ntohs(row.RemotePort)
    if row.State != tcpStateEstablished || !want[row.RemoteAddr] || (port != 80 && port != 443) {
        continue
    }
    row.State = tcpStateDeleteTCB
    procSetTcpEntry.Call(uintptr(unsafe.Pointer(&row)))
}

Note that this does require admin rights, and only works for IPv4.

On macOS there’s no such simple API call, but it does have a firewall, pf, and a firewall can answer packets with a reset.

We can add two rules:

pass out quick proto tcp from any to { 203.0.113.7 } port { 80, 443 } flags S/SA keep state
block return out quick proto tcp from any to { 203.0.113.7 } port { 80, 443 }

The first lets new connections through. It matches the opening SYN and tracks the connection from there. The second catches every other packet to that address, which can only belong to a connection that existed before the rules did, and answers it with a reset.

Connections that pf was already tracking would slip past, so we can drop those states first with pfctl -k. The rules go into an anchor under com.apple/*, which the stock pf.conf already evaluates, so nobody’s firewall config is edited. After six minutes, Chrome’s pool should have flushed so we can remove the rules again.

This also requires admin rights.

Still not 100%

There are a few things this still doesn’t cover.

My tool

I built Hosts Tray, a hosts switcher for Windows and macOS with a built-in local HTTPS proxy and automatic certificates. It does all of the above when you switch.