gvisor icon indicating copy to clipboard operation
gvisor copied to clipboard

netstack: new TCP connections between containers on a bridge inside the sandbox are sporadically reset when the client closed first (Docker in GKE Sandbox)

Open Lorenzofini opened this issue 1 month ago • 3 comments

Description

Running Docker inside a GKE Sandbox pod (as in https://gvisor.dev/docs/tutorials/docker-in-gke-sandbox/), short-lived TCP connections between two containers on a user-defined bridge network are sporadically reset while being established, when the client is the side that closes each connection first.

Observed (same pod, same image, sequential connections):

Test Result
2,000 connections client -> server over the bridge, client closes first 10 ConnectionResetError (first at connection #140)
same, server closes first 0 errors
pgbench -C (new PostgreSQL connection per transaction; libpq sends Terminate and closes first) from one container to a postgres container on the bridge each client aborts after 16-400 connections: server closed the connection unexpectedly
same pgbench -C over loopback inside the postgres container 9,871 connections, 0 errors
long-lived connections over the same bridge (pgbench without -C) 505k transactions, 0 errors
  • The PostgreSQL log shows nothing for the failed attempts: the connection is reset before the server process sees it.
  • Removing the only NAT rules in the sandbox (POSTROUTING -o eth0 -j SNAT) does not help (it fails even sooner).
  • In the containers: tcp_timestamps=1, tcp_rfc1337=1, ip_local_port_range=16000 65535; there is no tcp_tw_reuse/tcp_fin_timeout, and runsc flags cannot be changed on GKE Sandbox.

Expected: no resets, as on a regular Docker host (the same workloads run clean there).

Impact: a 12-container integration-test stack whose services open a new asyncpg connection per operation (SQLAlchemy NullPool, ~20 connections/s to PostgreSQL) fails ~13% of its tests with ConnectionResetError / Connect call failed ('172.18.0.6', 5432); it is green on a regular Docker host.

Possibly related, but different: #15013 / #15019 (SYN in TIME_WAIT dropped with tcp.Forwarder, server closes first, SYN silently dropped) and #16102 (source port kept after close). Here the client closes first and the new connection gets an RST.

Steps to reproduce

Pod on GKE Autopilot with runtimeClassName: gvisor, capabilities SYS_ADMIN, NET_ADMIN, NET_RAW, running docker:27.5.1-dind started as in the tutorial (--iptables=false, iptables-legacy SNAT on eth0, plus --mtu=1460). Inside it:

server.py

import socket, os, threading
mode = os.environ["MODE"]
s = socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("0.0.0.0", 9000)); s.listen(512)
def h(c):
    try:
        c.recv(1)
        if mode == "server-first":
            c.sendall(b"x"); c.close()
        else:
            c.sendall(b"x")
            while c.recv(16): pass
            c.close()
    except Exception:
        c.close()
while True:
    c, _ = s.accept(); threading.Thread(target=h, args=(c,), daemon=True).start()

client.py

import socket, sys, collections, time
mode, n = sys.argv[1], int(sys.argv[2])
err = collections.Counter(); first = None; t = time.time()
for i in range(n):
    try:
        c = socket.create_connection(("srv", 9000), timeout=3)
        c.sendall(b"a")
        r = c.recv(1)
        if r != b"x": raise ConnectionError("empty reply")
        if mode == "server-first":
            while c.recv(16): pass
        c.close()
    except Exception as e:
        err[type(e).__name__] += 1
        if first is None: first = i
print(f"{mode:13s} {n} connections in {time.time()-t:.1f} s, errors {sum(err.values())} {dict(err)} first error at #{first}")
docker network create t1
for mode in client-first server-first; do
  docker rm -f srv 2>/dev/null
  docker run -d --name srv --network t1 -e MODE=$mode -v $PWD/server.py:/s.py:ro python:3.12-alpine python /s.py
  sleep 2
  docker run --rm --network t1 -v $PWD/client.py:/c.py:ro python:3.12-alpine python /c.py $mode 2000
done

Output:

client-first  2000 connections in 4.0 s, errors 10 {'ConnectionResetError': 10} first error at #140
server-first  2000 connections in 4.8 s, errors 0 {} first error at #None

runsc version

Not readable on GKE Sandbox. GKE 1.36.4-gke.2377000; per the GKE 1.36 changelog the bundled gVisor is 20260727.0_RC04.

docker version (if using docker)

Docker 27.5.1 (docker:27.5.1-dind) running inside the sandbox; the containers above are started by that dockerd.

uname

No response

kubectl (if using Kubernetes)

Server Version: v1.36.4-gke.2377000 (GKE Autopilot, REGULAR channel)
Nodes: Container-Optimized OS, kernel 6.12.110+, containerd 2.2.7, c4-standard-16

repo state (if built from source)

No response

runsc debug logs (if available)


Lorenzofini avatar Nov 03 '26 15:11 Lorenzofini

I reproduced ConnectionResetError outside GKE with Docker 27.5.1 inside public runsc 20260727.0, using your Python close sequence over the t1 bridge with srv as the hostname. The client saw ECONNRESET on 3/2,000 client-first exchanges and 2/2,000 server-first exchanges. The first failure in each run was recv(1) after sendall; other calls also reported resets. Per-operation results and setup.

On your GKE build, could you record which call first raises the exception (create_connection, sendall, or recv) and its errno? That would let us compare the failure you see with this public release. If you already have a trace, the client port for that failed exchange would help match it.

Posted by Codex on behalf of @tamird.

tamird avatar Nov 03 '26 19:11 tamird

Thanks for reproducing it. On GKE it is the same call.

First call that raises: recv(1) after sendall, ECONNRESET, in all 14 failures. create_connection and sendall never raised. The server never accepted any of those connections: it accepted exactly N − failures, with no errors on its side.

Same pod setup as in the report (GKE 1.36.4-gke.2377000, Docker 27.5.1 in the sandbox), 3 runs of 2,000 exchanges per mode, tcpdump on eth0 inside both containers:

run client-first server-first
1 0 0
2 3 1
3 9 1

Server-first fails here too (2/6,000), as in your run; I had 0 in the original report.

Trace for client port 41202 (client-first, run 2, exchange #305), client capture. MACs: …:03 is the client 172.18.0.3, …:02 is the server 172.18.0.2, and 02:42:30:1a:2a:99 is the bridge 172.18.0.1, i.e. the sandbox's root network namespace where dockerd runs (ip_forward=1, the only NAT rule is POSTROUTING -o eth0 -j SNAT).

.584538 02:42:ac:12:00:03 > 02:42:ac:12:00:02  172.18.0.3.41202 > 172.18.0.2.9000:  [S]  seq 3350805333
.584689 02:42:ac:12:00:02 > 02:42:ac:12:00:03  172.18.0.2.9000 > 172.18.0.3.41202:  [S.] seq 3764080588, ack 3350805334
.584694 02:42:30:1a:2a:99 > 02:42:ac:12:00:03  172.18.0.2.51792 > 172.18.0.3.41202: [S.] seq 3764080588, ack 3350805334   <- same SYN-ACK, routed, source port rewritten
.584705 02:42:ac:12:00:03 > 02:42:ac:12:00:02  172.18.0.3.41202 > 172.18.0.2.51792: [R]  seq 3350805334
.584721 02:42:30:1a:2a:99 > 02:42:ac:12:00:03  172.18.0.2.9000 > 172.18.0.3.41202:  [S.] seq 3990827282, ack 3350805334   <- another ISN, also from the bridge MAC
.584754 02:42:30:1a:2a:99 > 02:42:ac:12:00:02  172.18.0.3.41202 > 172.18.0.2.9000:  [R]  seq 3350805334                   <- the client's RST, routed, port translated back to 9000
.584756 02:42:ac:12:00:03 > 02:42:ac:12:00:02  172.18.0.3.41202 > 172.18.0.2.9000:  [.]  ack 3764080589
.584814 02:42:ac:12:00:03 > 02:42:ac:12:00:02  172.18.0.3.41202 > 172.18.0.2.9000:  [P.] seq 1:2, ack 3764080589
.584937 02:42:ac:12:00:02 > 02:42:ac:12:00:03  172.18.0.2.9000 > 172.18.0.3.41202:  [R]  seq 3764080589                   <- recv(1) gets ECONNRESET

What the captures show:

  1. Every frame between the two containers arrives twice, in both directions and in the successful exchanges too. One copy is switched by the bridge and has the sender container's MAC. The other is routed by the root namespace and has the bridge's MAC. In this run's client capture that is 34,560 frames from the containers and 34,560 from the bridge MAC. On a Linux host, frames between two ports of the same bridge are not handed to the bridge's IP layer to be forwarded; here they seem to be.

  2. All 14 failures, and none of the ~11,986 successful exchanges, contain a routed SYN-ACK with the source port rewritten (9000 → 51792 above). The sequence is then:

    • the client correctly resets that SYN-ACK;
    • its RST goes through the root namespace, is translated back to port 9000 and reaches the server with the right sequence number;
    • the server drops the half-open connection, so it never accepts it;
    • the client's ACK and data then get an RST.
  3. The same 4-tuple failed again in the next run, about 17 s later, after both containers had been removed and recreated: port 41202 (client-first #305, then server-first #1140) and port 57065 (client-first run 3 #668, then server-first run 3 #1276). In each of those two server-first runs it was the only failure. The only state that survives between runs is in the root namespace, which suggests a leftover conntrack/NAT entry for that tuple.

  4. Dropping the routed copies removes the failures. I ran iptables-legacy -I FORWARD -i br+ -o br+ -j DROP (and the same for docker0) in the root namespace:

    • no frames with the bridge MAC are left;
    • 0 failures in 12,000 exchanges (both modes);
    • pgbench -C with 16 clients for 90 s: 25,380 connections, 0 failures. Before, each client aborted after 16–400 connections.
    • Container egress through the SNAT is unaffected.

    We will probably use this as our workaround. The rule's packet counters stayed at 0 although it clearly matched.

Scripts and traces: https://gist.github.com/Lorenzofini/fee88fff2573bab6725af21da9ce4ab2

  • failed-exchanges.txt: all 14 failed exchanges, as seen from both containers (tcpdump -e);
  • successful-exchange-and-with-forward-drop.txt: a successful exchange, and one with the FORWARD rule;
  • client.py, server.py, run.sh: the instrumented scripts;
  • *-fails.jsonl: for each failure, the call, errno, client port and exchange number;
  • gvisor-bridge-reset-traces.zip: the raw pcaps of run 2 from both containers (snaplen 128, 1-byte payloads) and of the same run with the FORWARD rule (git clone the gist to get it).

Lorenzofini avatar Nov 06 '26 15:11 Lorenzofini

Thank you for capturing both ends. In all 14 failed exchanges, the client saw a copy of the server’s SYN-ACK from the bridge MAC with its source port changed. The client reset that port, and the server saw the same reset translated back to 9000 before it accepted the connection. That puts the failure in the handshake, before either side chooses who closes first.

d4be3ed28 stops delivering transit unicast to the bridge’s local IP stack, which removes the routed copy in this path. With just that change our July Docker fixture went from 2 resets in 4,000 exchanges to 0 in 4,000. Matched runs

The first public runsc release containing the fix is 20261012.0. @manninglucas, can you confirm whether GKE Sandbox v1.36.4-gke.2377000 includes that fix, or point us to the person who can?

Posted by Codex on behalf of @tamird.

tamird avatar Nov 06 '26 21:11 tamird