Skip to content

fix(proxy): a hot-restart child with fewer workers than its parent no longer crashes on a forwarded UDP datagram (#1126) - #1133

Merged
bpalermo merged 3 commits into
mainfrom
proxy/1126-forwarded-udp-worker-index
Oct 2, 2026
Merged

bpalermo merged 3 commits into
mainfrom
proxy/1126-forwarded-udp-worker-index

Conversation

@bpalermo

@bpalermo bpalermo commented Oct 2, 2026 •

Copy link
Copy Markdown
Owner

Closes #1126

What crashed

On 2026-10-01 the rollout that changed the node proxy from 4 Envoy workers to 2 (proxy.concurrency: 2) segfaulted the 2-worker hot-restart successor in HotRestartingChild::onForwardedUdpPacket on 4 of 5 nodes. The reverse change (2→4) did not crash.

Root cause

Line numbers are for the Envoy source this image builds: pin 726d7ac with the carried patches applied (bazel/patches/, through aether1074).

  1. The parent tags each forwarded datagram with its own worker index. Once the parent drains (HotRestartingParent::Internal::drainListeners, source/server/hot_restarting_parent.cc:289-293), a QUIC listener hands every packet it cannot dispatch to the hot-restart forwarder along with its worker_index_ (source/common/quic/active_quic_listener.cc:198-203). A raw UDP listener does the same (source/server/active_udp_listener.cc:125-130). Internal::handle writes that index into the message (hot_restarting_parent.cc:102). A 4-worker parent sends indices 0–3.
  2. The child indexes its worker table with that index without a bounds check. onSocketEventUdpForwarding passes req.worker_index() straight through (source/server/hot_restarting_child.cc:477). onForwardedUdpPacket calls listenerWorkerRouter(addr).deliver(worker_index, …) (hot_restarting_child.cc:141). Its own comment (:125-140) already says forwarding "will fail" if the worker count changes. UdpListenerWorkerRouterImpl::deliver (source/common/network/udp_listener_impl.cc:200-212) guards the index with an ASSERT only, and that is compiled out of release builds. It then reads workers_[dest_worker_index] from a std::vector sized to the child's concurrency (2).
  3. The faulting dereference. For index 2 or 3, workers_[i] reads heap memory past the end of a 16-byte vector buffer. That garbage is used as a UdpListenerCallbacks*, and worker->post(std::move(data)) (udp_listener_impl.cc:210) makes a virtual call through it: SIGSEGV. When the garbage word happens to be zero, the datagram is silently dropped instead, which is why the crash was intermittent per node. The proxy CI run on this PR's first commit (tests only, no fix) reproduced exactly this: udp_listener_impl_test … terminated by SIGSEGV (see Test evidence below).

What Mismatched worker index. expected 3, actual 1 is

It is a parent-side diagnostic, not the child's bounds check. ActiveQuicListener::destination (active_quic_listener.cc:260-270) logs it when kernel BPF routing delivered a packet to a worker other than the one its own selector picks, and then keeps the packet where it is. The QUIC worker selector does not encode a worker id. It computes the first 4 connection-ID bytes % concurrency, both in the CBPF program (source/common/quic/envoy_deterministic_connection_id_generator.cc:82, mod #socket_count) and in bpfEquivalentFunction (:129). The child applies its listener socket options at STATE_BOUND to the duplicated parent sockets too (source/common/listener_manager/listener_impl.cc:187). Those options include SO_ATTACH_REUSEPORT_CBPF built with the child's concurrency (active_quic_listener.cc:415-420). A reuseport BPF program belongs to the whole socket group, so the child replaces the parent's mod 4 steering with mod 2. From then on the parent's workers 0 and 1 receive every packet. A connection whose CID % 4 = 3 (or 2) arrives at worker 3 % 2 = 1 (or 0). That is precisely the logged pairs expected 3, actual 1 and expected 2, actual 0. The log is a symptom of the count change, not the crash site. The out-of-range index itself comes from parent workers 2 and 3 forwarding whatever they still read after the drain starts. That is either datagrams already queued on their sockets, or a listener whose child-side socket (and so the BPF re-attach) only appears after the drain began.

Neighbouring paths (same out-of-range assumption?)

  • TCP listen-socket handoff: bounded. getListenSocketsForChild only returns a socket for worker_index < parent concurrency (hot_restarting_parent.cc:220), and fd -1 otherwise. On a 2→4 change the child binds fresh SO_REUSEPORT sockets for workers 2 and 3 (listener_manager_impl.cc:375-390, fallback below). On a 4→2 change the child never asks for 2 and 3. The parent closes its unshared TCP sockets when its listeners stop (maybeCloseSocketsForListener, listener_manager_impl.cc:1386-1393), and connections already in those accept queues are reset (no tcp_migrate_req). That costs a few connections once, which is what the chart comment already warns about. It does not crash.
  • UDP sockets for workers the child does not have stay open in the parent until it exits: maybeCloseSocketsForListener skips UDP. Under the child's mod 2 program they receive no new datagrams. Datagrams still queued when the parent exits are lost (UDP: dropped, not reset).
  • The in-process path is unaffected. ActiveUdpListenerBase::onData asserts that its own destination() is below its concurrency (active_udp_listener.cc:67). Only the hot-restart forwarder can hand deliver() a foreign index.

The patch: proxy/bazel/patches/envoy-aether1126-forwarded-udp-worker-index.patch

It is applied after aether1074, and it is child-side only, so it takes effect from the first roll onto an image built from it. UdpListenerWorkerRouterImpl::deliver no longer reads past its table. An index this instance does not have is mapped onto index % workers, with a power-of-two rate-limited info log (udp worker index N is not one of this instance's M workers …). An in-range index behaves exactly as before, including the existing "no worker registered → drop" case. The UdpListenerWorkerRouter::deliver doc comment (envoy/network/listener.h) and the stale comment in onForwardedUdpPacket are updated. The patch is one if block, with no interface or mock changes. It covers QUIC and raw UDP (udp_proxy) alike, because both forward through the same router.

Why remap rather than drop. The forwarded datagrams are exactly the ones the parent could not dispatch: new handshakes, and packets of connections the child already accepted from a forwarded handshake. For the child to keep such a connection, the session has to sit on the worker the kernel will send its later packets to once the parent is gone. That worker is CID % child_concurrency. The parent's index for a packet it received through its own BPF is CID % parent_concurrency, and (CID % 4) % 2 == CID % 2. So when the parent's count is a multiple of the child's (4→2, 8→4, 4→1, …), index % concurrency is exactly the child's own connection-ID routing, and such connections survive the count change. For other counts (for example 3→2) it is merely safe: the datagram lands on a valid worker, and a connection set up that way may later be reset, as a dropped packet would have caused anyway. The general fix (re-run the child's CID selector on the forwarded packet) needs a new method on UdpListenerCallbacks plus mocks, so it is left to upstream.

Test evidence (red, then green)

Two tests are added to targets that //bazel/patches:carried_patch_tests already runs, and so the proxy CI job's bazel test //... runs them too:

  • @envoy//test/common/network:udp_listener_impl_test UdpListenerWorkerRouterImplTest.DeliverMapsAWorkerIndexItDoesNotHaveOntoItsOwnWorkers: a real 2-worker router. Indices 0 and 1 are delivered as addressed, a 4-worker parent's indices 2 and 3 land on workers 0 and 1, and a mapped index with no registered worker is dropped.

  • @envoy//test/server:hot_restarting_child_test HotRestartingChildTest.ForwardsPacketFromAParentWorkerTheChildDoesNotHave: the forwarded HotRestartMessage of a 4-worker parent's worker 3 reaches a 2-worker child's real router and is posted to worker 1.

  • Red: commit 5dc33be (the patch with only the test hunks), proxy CI amd64 leg: Testing @@envoy+//test/common/network:udp_listener_impl_test failed: (Exit 34): … command was terminated by SIGSEGV. That is the production crash, in the release-mode (ASSERT-less) build.

  • Green: commit 928e45c (the fix), proxy CI run 37044395786: amd64 and arm64 legs both pass, Executed 14 out of 14 tests: 14 tests pass. (//..., which includes //bazel/patches:carried_patch_tests and so both new tests; the red run had stopped at 10 tests pass and 4 were skipped).

Local build: I started bazel test @envoy//test/common/network:udp_listener_impl_test @envoy//test/server:hot_restarting_child_test in proxy/, but did not finish it. The workstation was at load average around 240 with several other Envoy builds running, and the run sat compiling test mocks for over an hour before it was stopped. The proxy CI job, which runs on BuildBuddy RBE, is the evidence here. Root workspace: bazel test //charts/... passes (53/53).

Docs

Is a concurrency change now safe for existing QUIC connections?

Not fully. The crash is fixed; connection survival is not guaranteed in either direction. This is an inference from the code, not measured.

  • 4→2: no crash. QUIC connections the parent still owns are hurt from the moment the child attaches its mod 2 program. A connection whose CID % 4 ≠ CID % 2 (about half of them) has its packets steered to a parent worker that does not own it. Before the drain, quiche answers with a stateless reset. After the drain, the packet is forwarded to the child, which does not know the connection either. That is the Mismatched worker index traffic. Handshakes accepted by the child during the drain survive, thanks to the remap.
  • 2→4: no crash, and no out-of-range index (2 < 4). The child's workers 2 and 3 get fresh sockets, which are not inherited and so not paused (only inherited sockets register with parentDrainedCallbackRegistrar, listener_manager_impl.cc:388). Under the child's mod 4 program, about half of the parent's existing QUIC connections (CID % 4 ∈ {2,3}) are steered to those sockets, and the child, which does not know them, would stateless-reset them. Separately, a handshake forwarded from parent worker i lands on child worker i (CID % 2), not on CID % 4.

So a proxy.concurrency change remains a one-time event that costs a share of the live QUIC connections (h3 clients reconnect) and a few queued TCP connections. Same-count restarts are unaffected.

Follow-up (not in this PR; the supervisor is #1127's area)

A supervisor-side guard: when the next epoch's --concurrency differs from the live parent's, do not HOT restart. Do a drain-then-fresh-start instead, or at least log it loudly. That would remove the QUIC share above as well. It is out of scope here: this PR changes no supervisor or Go code. (#1127, merged as #1134 while this PR was open, fixed the self-drain in agent/internal/proxy/hotrestart; it does not add this guard.)

⚠️ Upstream note (for the owner to report; nothing was posted upstream)

The same bug very likely exists upstream. onForwardedUdpPacket → UdpListenerWorkerRouterImpl::deliver with the parent's index, guarded only by an ASSERT, is in the envoyproxy/envoy#47743 forwarding code (merged 2026-09-30) and in the original hot-restart UDP forwarding it extends. A hot restart that lowers --concurrency while QUIC/UDP listeners forward is undefined behaviour in release builds. The child re-attaching the reuseport CBPF program group-wide (listener_impl.cc:187) during the handoff, which moves the parent's steering, is a related upstream behaviour worth raising in the same report. Reporting it is the maintainer's call.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

bpalermo and others added 3 commits October 2, 2026 14:38
…start child does not have (#1126)

Adds the carried patch envoy-aether1126-forwarded-udp-worker-index.patch with
its two tests only, ahead of the fix, so the proxy CI run on this commit shows
them failing against the unfixed Envoy:

- udp_listener_impl_test DeliverMapsAWorkerIndexItDoesNotHaveOntoItsOwnWorkers
- hot_restarting_child_test ForwardsPacketFromAParentWorkerTheChildDoesNotHave

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
…does not have onto index % concurrency (#1126)

The hot-restart parent forwards each UDP/QUIC datagram it cannot dispatch tagged
with ITS worker index; the child delivered it with
UdpListenerWorkerRouterImpl::deliver(), which indexed its per-worker table with
only an ASSERT. A child with fewer workers than its parent (proxy.concurrency
4 -> 2) read past the end of the table and segfaulted. deliver() now maps an
index this instance does not have onto index % workers, which for a parent
count that is a multiple of the child's is the worker the child's own
connection-ID routing picks.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
…udp-worker-index

# Conflicts:
#	docs/runbook.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant