Skip to content

udp: flows from one source IP collapse into a single flow, replies go to the first client's port, and the flow never idles out #1732

Closed

Description

@P4ndaFR

Summary

With a UDP listener + UDP cluster configured with the default UdpClusterConfig (so affinity_key unset → SOURCE_IP), Sōzu:

  1. merges every client coming from the same source IP into a single flow, whatever their source port, over a single connected upstream socket;
  2. sends every backend reply to the source port of the first client that created the flow, not to the client that sent the datagram. Once that first client socket is gone, no client from that IP ever gets a reply again;
  3. never expires that flow: it survived >15 min of complete silence and ~30 min overall, with front_timeout/back_timeout = 30 s on the listener.

The net effect is that UDP load balancing is broken for any client IP after its first flow. It is especially bad behind NAT/CGNAT, where many clients share one IP (e.g. WebRTC/LiveKit, DNS resolvers).

This seems to contradict the design in #1273 ("Virtual 4-tuple flow sessions", idle timeout teardown, "one connected socket per flow (the connected fd is the NAT-return demux key)").

Environment

  • Sōzu 2.2 (configured through sozu-command-lib 2.2.0 from a control-plane agent, via the command socket)
  • Linux, Sōzu running in a dedicated network namespace
  • UDP listener on VIP:9090, front_timeout = 30, back_timeout = 30, max_rx_datagram_size = 1500, max_flows = 0
  • Cluster: load_balancing = HRW, udp = UdpClusterConfig::default() (all fields None)
  • 3 backends (Kubernetes NodePort; the backend is a trivial UDP echo server that replies to the datagram's source address)

Reproduction

  1. Send one datagram from client socket C0 (source port P0) → you get a reply. Close C0.
  2. From the same IP, open two new sockets A (port Pa) and B (port Pb) and alternate datagrams.
  3. Neither A nor B ever gets a reply. Waiting >> front_timeout (tested with 150 s and ~15 min of silence) doesn't help.

Evidence

tcpdump inside the Sōzu netns. The client IP is anonymized as 198.51.100.7 and the VIP as 203.0.113.8; the other addresses are real. The client sockets are A = port 50376 and B = port 50644:

17:10:38.294726 pub  In  IP 198.51.100.7.50376 > 203.0.113.8.9090: UDP, length 2     <- client A
17:10:38.294810 wg   Out IP 10.112.0.8.40217   > 10.112.0.10.31170: UDP, length 2    <- to backend
17:10:38.297044 wg   In  IP 10.112.0.10.31170  > 10.112.0.8.40217: UDP, length 38    <- backend reply OK
17:10:38.297133 pub  Out IP 203.0.113.8.9090   > 198.51.100.7.61203: UDP, length 38  <- WRONG port (should be 50376)
17:10:40.299681 pub  In  IP 198.51.100.7.50644 > 203.0.113.8.9090: UDP, length 2     <- client B
17:10:40.299797 wg   Out IP 10.112.0.8.40217   > 10.112.0.10.31170: UDP, length 2    <- same upstream socket
17:10:40.302070 wg   In  IP 10.112.0.10.31170  > 10.112.0.8.40217: UDP, length 38
17:10:40.302128 pub  Out IP 203.0.113.8.9090   > 198.51.100.7.61203: UDP, length 38  <- WRONG port (should be 50644)
... (same pattern for 90 datagrams over 3 minutes, 0 replies reach A or B)

61203 is the port of a client socket that sent a single datagram ~30 min earlier and was closed right after. It was the only client that ever got a reply.

ss -uanpe in the netns shows one upstream socket for the whole listener/cluster:

ESTAB  0 0  10.112.0.8:40217   10.112.0.10:31170  users:(("10",pid=2835,fd=12))   # Sōzu worker
UNCONN 0 0  203.0.113.8:9090   0.0.0.0:*          users:(("10",pid=2835,fd=11))   # Sōzu worker
UNCONN 0 0  *:9090             *:*                users:(("10",pid=2835,fd=13))   # Sōzu worker

That same upstream socket 10.112.0.8:40217 was used for every datagram from this client IP for ~30 min. The backend-side capture confirms that every datagram arrives from 10.112.0.8:40217 and that the backend replies to it.

The second VIP of the same load balancer shows the same symptoms: a single upstream socket (10.112.0.9:55004) for all clients of one IP, seen from the backend side, and no reply reaching the client.

Expected

  • The flow is keyed on the client 4-tuple, or at least the reply is sent to the source address of the client datagram that is being answered. SOURCE_IP affinity should only influence backend selection (HRW/Maglev hash), not merge distinct client sockets into one flow whose return address is frozen.
  • An idle flow is torn down after the idle timeout, so its upstream socket is closed and a new flow (new upstream socket) is created for the next datagram.

Notes / questions

  • Is affinity_key = SOURCE_IP supposed to key the flow table, or only the backend hash? If it's the flow table, the default makes UDP unusable behind NAT, and the reply address should at least be updated to the latest client source.
  • On our side we plan to work around it by setting affinity_key = SOURCE_IP_PORT explicitly. The idle-expiry problem (point 3) probably exists independently of the key.

Activity

  1. P4ndaFR commented on Oct 1, 2026

    @P4ndaFR
    Author

    Update after redeploying (Sōzu worker restarted, so the flow table was fresh): points 1 and 2 are no longer reproducible, but point 3 (idle flows never torn down) is confirmed.

    Points 1 & 2: OK now

    From a single client IP, two sockets A and B alternating, plus 30 fresh sockets (one datagram each), against 203.0.113.8:9090:

    • every reply reaches the right client socket (0 lost out of 34 flows);
    • each client socket gets its own upstream connected socket (distinct 10.112.0.8:<port> seen by the backends), spread over all backends.

    So with this setup flows are keyed on the client ip:port. (I can't tell from the outside whether that comes from an explicit affinity_key = SOURCE_IP_PORT on our side or from a change in Sōzu itself. The question in the original report about what SOURCE_IP is supposed to key still stands.)

    Point 3: idle flows are never closed

    Listener front_timeout = 30, back_timeout = 30, max_flows = 0.

    Sequence:

    1. A/B test: 2 flows.
    2. 30 fresh sockets, one datagram each: 30 flows.
    3. One socket sends a datagram, stays idle 75 s, then sends again: both replies come back, and the backend sees the same upstream source port both times, so the flow survived 75 s of idle. That's 1 flow.

    That's 33 flows in total. A few minutes after the last datagram (all flows idle well past 30 s), ss -uanpe in the Sōzu netns shows 33 connected upstream sockets, i.e. every flow ever created since the restart:

    ESTAB 0 0 10.112.0.8:48153 10.112.0.5:31170  users:(("10",pid=948,fd=15)) ino:3985   <- step 1, socket A
    ESTAB 0 0 10.112.0.8:40665 10.112.0.12:31170 users:(("10",pid=948,fd=16)) ino:3986   <- step 1, socket B
    ESTAB 0 0 10.112.0.8:46591 10.112.0.12:31170 users:(("10",pid=948,fd=17)) ino:8848   <- step 2 ...
    ESTAB 0 0 10.112.0.8:44216 10.112.0.11:31170 users:(("10",pid=948,fd=18)) ino:8849
    ...                                                                  (fd 19 .. 46)
    ESTAB 0 0 10.112.0.8:49086 10.112.0.10:31170 users:(("10",pid=948,fd=47)) ino:43173  <- step 3
    

    (33 lines, fds 15 to 47, none closed.)

    Impact

    Now that each client socket gets its own flow, the leak grows with each client socket instead of with each client IP. With max_flows = 0, the worker keeps accumulating connected UDP sockets until it hits RLIMIT_NOFILE. That affects any UDP workload where clients use ephemeral ports per request (DNS) or many short sessions (WebRTC/TURN).

    Expected: an idle flow is reaped after the idle timeout and its upstream socket is closed, as described in #1273 (three-knob teardown, idle timeout, timer wheel with generation tokens).

  2. added 3 commits that reference this issue on Oct 1, 2026
    eabfa45
    a446221
    8614c7e
  3. FlorentinDUBOIS commented on Oct 1, 2026

    @FlorentinDUBOIS
    Collaborator

    Update: #1746 is merged into main (fcdec35).

    • Points 1 and 2 (flows from one source IP collapsing into one flow, replies going to the first client's port): fixed by fix(udp): key flows on the client source address, not on the affinity key #1746. Flows are now keyed on the client source address (IP and port). This changes the lib API (breaking for embedders).
    • Point 3 (flows never idling out): fixed on main by 596b3ec. Idle flows are torn down according to the listener front_timeout / back_timeout. There is no configuration workaround on 2.2.x.
    • Neither fix is in a release yet; both will ship in the next one.
    • An optional per-source flow cap for UDP clusters (max_connections_per_ip / max_connections_per_subnet, opt-in per cluster) is in progress in feat(udp): cap concurrent flows per source address and subnet #1770.
  4. FlorentinDUBOIS commented on Oct 2, 2026

    @FlorentinDUBOIS
    Collaborator

    Follow-up: the opt-in per-source UDP flow cap is merged in #1770 (99e6aee2). A UDP cluster's own max_connections_per_ip / max_connections_per_subnet now cap concurrent flows per source address and subnet; the global defaults do not apply to UDP. Not yet in a release.

  5. added a commit that references this issue on Oct 2, 2026
    fcdec35
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