Repository navigation
hot restart: never let the parent and child main threads wait on each other - #47834
Conversation
… other
During a hot restart the parent forwards UDP/QUIC packets it cannot
dispatch to the child over a unix datagram socket, from its main thread,
with a blocking sendmsg(). The child's main thread waits for the parent's
replies (stats merge every flush interval, listen socket hand-offs) with a
blocking recvmsg() and does not read forwarded packets while doing so.
Once the child's socket fills up, the parent blocks in sendmsg() and never
answers the child's request; both main threads then wait on each other for
good, with admin, xDS, timers and signal handling dead in both processes.
Parent: forwarded packets go through a bounded non-blocking send queue on
the UDP forwarding stream, flushed at once and retried on a short timer
while the child's socket is full. Nothing on that path blocks, sleeps or
asserts any more: a full queue drops packets, a child that is gone drops
the queue. New server.hot_restart_udp_forwarding_{datagrams,retries,dropped}
counters report it.
Child: while waiting for a parent reply it keeps servicing the UDP
forwarding socket, probes the parent for liveness and gives up after a
bounded deadline. A parent that stopped answering is written off (no more
stats merges, new listeners bind their own sockets, terminate is still
sent); a parent whose socket is gone completes the drain as terminate would.
RpcStream: the domain sockets get send/receive timeouts, a blocking receive
returns nothing after its slice instead of asserting, and a blocked send
fails after its timeout instead of hanging. The wire protocol is unchanged.
Signed-off-by: Bruno Palermo <[email protected]>
…ng-udp-forwarding Signed-off-by: Bruno Palermo <[email protected]>
|
/retest |
…ng-udp-forwarding Signed-off-by: Bruno Palermo <[email protected]> # Conflicts: # test/server/hot_restarting_child_test.cc
…, now with main) into hot-restart-listener-add-hardening-main Signed-off-by: Bruno Palermo <[email protected]>
|
/retest |
…ng-udp-forwarding Signed-off-by: Bruno Palermo <[email protected]>
…, now with main) into hot-restart-listener-add-hardening-main Signed-off-by: Bruno Palermo <[email protected]>
|
/retest |
1 similar comment
|
/retest |
Signed-off-by: Bruno Palermo <[email protected]>
|
/retest |
1 similar comment
|
/retest |
Signed-off-by: Bruno Palermo <[email protected]>
|
Assigning to @ravenblackx because I think they originally wrote this. |
| // A parent that is already gone cannot be told, and one that is wedged will not listen. | ||
| main_rpc_stream_.sendHotRestartMessage(parent_address_, wrapped_request, | ||
| /*allow_failure=*/true); | ||
| parent_terminated_ = true; |
There was a problem hiding this comment.
This bit seems a little problematic - if the parent is not wedged but is "buffer full" (or times out), the terminate message won't be delivered but the child sets parent_terminated_ anyway, and we'll be left with two running instances.
But it's still an improvement over the status quo (where in the same circumstances the child will just be wedged so we'd have two running instances that also aren't even doing anything) and I don't see a readily available airtight solution.
And in our environment the wrapper that launches these things would eventually hard-kill the parent process.
There was a problem hiding this comment.
Agreed, thanks. Follow-up in #48319 (stacked on this one, draft for now) makes it better, not airtight: the child resends Terminate while the parent's socket is full, without blocking, and only takes over the inherited UDP listeners once the parent has actually exited, both capped at 2 s. A parent still wedged at the cap keeps running; the child logs that as an error, and stopping it stays with the launcher, as today.
|
/assign-from envoyproxy/senior-maintainers Looks good to me, but because it touches core code it also needs approval from a senior maintainer.
Yes, the UDP packet forwarding part, but I wouldn't want 'credit' for the original datagram "rpcs" and the various pre-existing deadlock and crashout options! :D |
|
envoyproxy/senior-maintainers assignee is @htuch |
…the two carried patches it contains are dropped (#980) (#1643) The pin is one fact in three places and moves as one: envoy and envoy_api (proxy/MODULE.bazel) and the envoyproxy/bazel-registry commit (proxy/.bazelrc, 5db29c09de17218d24f58c665be38208284754d8), written by scripts/bump-envoy-pin.sh. rules_python follows what the envoy module requests (2.2.0 -> 2.3.3). The lockfile is refreshed. Dropped, because Envoy 189f198 contains them: - envoy-47743-udp-hot-restart-forwarding-netns (envoyproxy/envoy#47743, a644fc670e): the carried hunks reverse-apply cleanly to the snapshot. - envoy-aether1022-exec-in-netns-thread-self (envoyproxy/envoy#47776, 7d6ca3273c): the snapshot opens /proc/thread-self/ns/net. The carried fallback for kernels older than 3.17 and its per-thread model test go with it. Kept, in the same order: - 47740 (still open upstream): unchanged content, hunk positions refreshed. - aether1050: replaced by the diff of its upstream pull request, envoyproxy/envoy#47834 at 72408727eb, docs and changelog dropped. Same source as before plus three counters, server.hot_restart_udp_forwarding_{datagrams,retries,dropped}; the test hunks no longer carry the 47743 test changes. - aether1054: rebased over envoyproxy/envoy#47991, which rewrote ConnectionManagerImpl::onDrain() to drain idle connections proactively in the last third of the drain time. That arms nothing when the drain time leaves no room after the drain timeout (the chart's 10s with the default 5s), so the carried idle HTTP/3 drain stays, armed beside it from armIdleHttp3DrainTimer(). Upstream's ProactiveDrain* tests notify the drain before a codec exists and turn the carried timer off; the carried gradual case now asserts both timers. - aether1054b, aether1054c, aether1074, aether1126: unchanged content. proxy/.bazelrc: envoyproxy/envoy#47806 removed the --@envoy//bazel:libc++ flag and the force_libcpp define, so both leave the libc++ config; nothing else upstream removed from its own .bazelrc is required to go, and the rest is kept as built before. Refs #980 Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr Co-authored-by: Claude Opus 5.5 <[email protected]>
| } | ||
|
|
||
| std::unique_ptr<HotRestartMessage> HotRestartingChild::waitForParentReply() { | ||
| const absl::Time start = absl::Now(); |
There was a problem hiding this comment.
Nit: absl::Now() is the realtime clock, so a clock step moves this deadline. std::chrono::steady_clock (what monotonicTime() uses) is the usual choice for a timeout.
There was a problem hiding this comment.
Done: the deadline now uses std::chrono::steady_clock. This runs before the server's time system exists, so it reads the clock directly (with the NO_CHECK_FORMAT(real_time) annotation).
There was a problem hiding this comment.
nit: the reason in the new comment doesn't hold. restarter_.initialize(*dispatcher_, *this) (server.cc:439) runs before any call to waitForParentReply(), and that dispatcher's timeSource() is the server's time system. HotRestartingChild::initialize() could keep that time source and use monotonicTime() here, which removes both NO_CHECK_FORMAT(real_time) lines. Otherwise please fix the comment.
There was a problem hiding this comment.
You're right, the child is initialized with the main dispatcher before it ever waits. It now keeps the dispatcher's time source and uses monotonicTime(); the NO_CHECK_FORMAT(real_time) lines are gone.
|
LGTM modulo the minor comments. |
…he failure paths waitForParentReply() measured its deadline with absl::Now(), the wall clock; use the steady clock so a clock step cannot move it. Add tests for the listen socket and admin shutdown requests that cannot be sent, the admin shutdown request that gets no reply, the error log when a forwarded packet fails while waiting, and the parent queueing without a retry timer after shutdown(). Signed-off-by: Bruno Palermo <[email protected]>
|
@htuch @ravenblackx Addressed the comments: the reply deadline now uses a monotonic clock, and the new failure paths have tests. The terminate-request improvement is in a follow-up, #48319 (draft, stacked on this one). PTAL |
htuch
left a comment
There was a problem hiding this comment.
LGTM, but one corner case worth discussing surfaced by Claude. @ravenblackx good to ship with this fixed?
| const Api::SysCallSizeResult recv_result = os_sys_calls.recvmsg(domain_socket_, &message, 0); | ||
| if (block == Blocking::No && recv_result.return_value_ == -1 && | ||
| recv_result.errno_ == SOCKET_ERROR_AGAIN) { | ||
| if (recv_result.return_value_ == -1 && recv_result.errno_ == SOCKET_ERROR_AGAIN) { |
There was a problem hiding this comment.
With SO_RCVTIMEO/SO_SNDTIMEO set, Linux never restarts an interrupted recvmsg()/sendmsg(), even for SA_RESTART handlers (signal(7)); the call fails with EINTR. Envoy's SIGTERM, SIGINT, SIGUSR1 and SIGHUP handlers are installed by libevent with SA_RESTART, so before this PR the interrupted call was restarted. Now EINTR here reaches the RELEASE_ASSERT below, and in sendHotRestartMessageImpl() the final RELEASE_ASSERT(false, ...). restarter/hot-restarter.py forwards SIGUSR1 (log reopen) to both processes; if it lands while the child waits for a stats reply, the child aborts. Please treat EINTR like the timeout here, retry it in the send loop, and add tests with the mock returning EINTR.
There was a problem hiding this comment.
Good catch, thanks. EINTR from recvmsg() is now treated like the receive timeout (the wait loop carries on), and sendmsg() retries it. Tests added with the mock returning EINTR.
| } | ||
|
|
||
| std::unique_ptr<HotRestartMessage> HotRestartingChild::waitForParentReply() { | ||
| const absl::Time start = absl::Now(); |
There was a problem hiding this comment.
nit: the reason in the new comment doesn't hold. restarter_.initialize(*dispatcher_, *this) (server.cc:439) runs before any call to waitForParentReply(), and that dispatcher's timeSource() is the server's time system. HotRestartingChild::initialize() could keep that time source and use monotonicTime() here, which removes both NO_CHECK_FORMAT(real_time) lines. Otherwise please fix the comment.
…s time source With send and receive timeouts set on the domain socket, a signal interrupts a blocking sendmsg() or recvmsg() with EINTR even for handlers installed with SA_RESTART (signal(7)). Retry the send, and treat an interrupted receive like the receive timeout, instead of asserting. Measure the reply deadline with the main dispatcher's time source, which the child already has when it first waits. Signed-off-by: Bruno Palermo <[email protected]>
|
/retest |
No objections from me. |
…ng-udp-forwarding Signed-off-by: Bruno Palermo <[email protected]>
|
/retest |
1 similar comment
|
/retest |
…er1054c and aether1074 (proposal 045) - envoy/envoy_api 1.40.0.envoy -> 1.41.0-dev.20261028.011e396.envoy, registry a1264b53 -> 36d481b01b (the commit that added the snapshot). - Dropped patches: aether1054c (envoyproxy/envoy#47868, 2eff6e8ccf) and aether1074 (envoyproxy/envoy#47893, 77b512527c), both in the snapshot. - aether1050 refreshed from envoyproxy/envoy#47834's head against its merge base, which already contains #47068: the +/- lines are unchanged, only the test file's context moved. aether1054 refreshed by hand: two one-line insertions whose neighbouring runtime guards were removed upstream. 47740 and aether1126 apply unchanged. - .bazelversion 8.7.0 -> 9.3.1 (Envoy's own; the 1.40.0 closure does not load under Bazel 9, so the move cannot land first). .bazelrc follows upstream: merge_fixed_and_default_shell_env removed, system_include_paths and the rules_rust linker_messages flags added, the toolchains_llvm compiler-rt and libunwind overrides dropped (the envoy module links compiler-rt + libunwind since envoyproxy/envoy#48641). - proxy-wasm-cpp-host .envoy -> .envoy.1, rules_cc 0.2.25 -> 0.2.27, rules_python 2.3.3 -> 2.4.5: what the new closure selects. - The MODULE.bazel comment that still called #47893 open is rewritten. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YacWmjoxWSZRJC38XYxuZx
…er1054c and aether1074 (proposal 045) (#1733) * proxy: Envoy 1.41.0-dev.20261028.011e396 under Bazel 9.3.1; drop aether1054c and aether1074 (proposal 045) - envoy/envoy_api 1.40.0.envoy -> 1.41.0-dev.20261028.011e396.envoy, registry a1264b53 -> 36d481b01b (the commit that added the snapshot). - Dropped patches: aether1054c (envoyproxy/envoy#47868, 2eff6e8ccf) and aether1074 (envoyproxy/envoy#47893, 77b512527c), both in the snapshot. - aether1050 refreshed from envoyproxy/envoy#47834's head against its merge base, which already contains #47068: the +/- lines are unchanged, only the test file's context moved. aether1054 refreshed by hand: two one-line insertions whose neighbouring runtime guards were removed upstream. 47740 and aether1126 apply unchanged. - .bazelversion 8.7.0 -> 9.3.1 (Envoy's own; the 1.40.0 closure does not load under Bazel 9, so the move cannot land first). .bazelrc follows upstream: merge_fixed_and_default_shell_env removed, system_include_paths and the rules_rust linker_messages flags added, the toolchains_llvm compiler-rt and libunwind overrides dropped (the envoy module links compiler-rt + libunwind since envoyproxy/envoy#48641). - proxy-wasm-cpp-host .envoy -> .envoy.1, rules_cc 0.2.25 -> 0.2.27, rules_python 2.3.3 -> 2.4.5: what the new closure selects. - The MODULE.bazel comment that still called #47893 open is rewritten. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YacWmjoxWSZRJC38XYxuZx * proxy: carry aether1733, envoy --mode validate no longer segfaults on a QUIC listener #1733's proxy workflow (run 41308410926, both arches) failed //agent/test/envoy_validate:envoy_validate_test: `envoy --mode validate` of node_bootstrap.json, edge_bootstrap.json and the ADS reconnect test's bootstrap died with SIGSEGV in Quic::ActiveQuicListenerFactory::initializeWorkerRouting(). envoyproxy/envoy#45504, in the 011e396 snapshot, moved the QUIC listener's socket options there (behind envoy.restart_features.defer_worker_routing_init, default true) and applies them to every worker's listen socket. Validation mode creates no sockets (ValidationListenerComponentFactory::createListenSocket returns nullptr), so *socket dereferences null. Upstream main still has it. The proxy-supervisor's check before every hot restart on a changed bootstrap is `envoy --mode validate`, so this would have blocked every bootstrap change in production. The patch skips a null socket, which only validation mode produces, as validation did before #45504. Regression test: envoy_validate_test in the proxy workflow, red on 8ce601a. Not reported upstream (on hold). The "FilterStateMatcherValidationError.Key: value length must be at least 1 characters" line in the same log is TestAgentCaptureListenerAndTableValidate's expected rejection ("refused as it must be"); that test passed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YacWmjoxWSZRJC38XYxuZx * proxy: aether1733's production exposure is a future static QUIC listener; refresh the extension count - The aether1733 comment (MODULE.bazel, README) overstated the exposure: the chart's bootstraps carry static clusters only (listeners come over LDS), so the supervisor's pre-restart `--mode validate` does not crash today. A future bootstrap with a static QUIC listener would fail it, and every hot restart. The crash needs concurrency > 1 (the BPF reuseport option); raw-UDP listeners are unaffected. - extensions_build_config.bzl: 354 upstream extensions - 35 dropped = 319 at 011e396. The five new ones are named; tls.certificate_selectors.dynamic_modules is dropped by the existing pattern, and the four that compile in (priority_load_shed, host_allowlist, uri_template matcher, ai_protocol_manager in-memory buffer) are unused by aether and reach none of the wasm, dynamic-module or Rust machinery, so the policy keeps them. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YacWmjoxWSZRJC38XYxuZx --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Commit Message: hot restart: never let the parent and child main threads wait on each other
Additional Description:
Fixes #47833.
During a hot restart the parent forwards UDP/QUIC packets it cannot dispatch to the child over the
_udpunix datagram socket, from its main thread, with a blockingsendmsg(). The child's main thread waits for the parent's replies (stats merge every flush, listen socket hand-off) with a blockingrecvmsg()and reads no forwarded packets while it does. Once the child's_udpsocket fills up, each main thread is parked on the other for good: admin, xDS, timers and signals are dead in both processes and the parent never receives its terminate request. See the issue for stacks.Parent: forwarding never blocks.
HotRestartingParent::sendHotRestartMessageappends the message's datagrams (all or none) to a bounded (1 MiB) send queue on the UDP stream and flushes it withsendmsg(MSG_DONTWAIT).EAGAIN/ENOBUFSleave the rest queued and arm a 2 ms retry timer; a full queue drops the new packet (QUIC retransmits);ECONNREFUSED(child gone) drops the queue. The blocking-mode toggle, the 10×1 sECONNREFUSEDsleep-retry and theRELEASE_ASSERTare off this path, so a child that dies mid-drain no longer stalls and then crashes the parent. Three new counters report it:server.hot_restart_udp_forwarding_datagrams,_retries,_dropped.Child: waiting for a reply keeps the forwarding flowing, and is bounded. The three request/reply exchanges go through
waitForParentReply(): receive in 10 ms slices (SO_RCVTIMEO) and between slices drain the_udpsocket with the sameonSocketEventUdpForwarding()the file event uses. This alone breaks the cycle against an unpatched parent, which matters for the first roll onto a build with this change. The wait is bounded by a 30 s deadline and a 1 s liveness probe (TestConnection, which parents already handle by ignoring it):sendParentTerminateRequeststill fires at the end of the drain period. The late reply is never consumed, so a mismatched reply type cannot trip the existing asserts.drainParentListenersandsendParentTerminateRequestsend withallow_failure, so a parent that is gone no longer makes the child sleep 10 s and abort.RpcStream. Both domain sockets get
SO_SNDTIMEO= 1 s andSO_RCVTIMEO= 10 ms at bind. A blocking receive returns null after its slice instead of asserting (a partial message stays buffered, the stream state is untouched); a blocking send returnsTimedOutinstead of hanging, so a parent reply to a child that stopped reading is abandoned after 1 s per datagram.sendHotRestartMessagekeeps its bool contract;trySendHotRestartMessagereportsSent/ConnectionRefused/TimedOutfor the probe.The wire protocol and
HOT_RESTART_VERSIONare unchanged: the change hot-restarts across an unpatched parent and an unpatched child. The child'sECONNREFUSEDretry-then-assert on its own requests (the startup handshake) keeps its current semantics.Testing.
hot_restarting_child_test.ccServicesUdpForwardingWhileWaitingForParentStatsmodels the dependency in the syscall mock: the stats reply becomes readable on the main socket only after the forwarded packet has been read from the_udpsocket (the parent's blocked send completing). Without the fix the child's blocking receive getsEAGAINand dies onrecvmsg() returned -1, errno = 11; with it the packet is delivered and the reply merged. Companion cases cover the listen-socket hand-off interleaving, the deadline, gone-on-refused-probe (drains complete), probe timeout, request send timeout. Parent tests cover the non-blocking socket, queue-and-retry ordering, multi-datagram atomicity,ECONNREFUSEDdrop with no timer/sleep/assert, queue-full drop, and the counters. Base tests cover the timed-out send and receive.//test/integration:hotrestart_testpasses.Verified against a deployment that reproduced the wedge under forced main-thread stalls during the drain window (several wedges per handful of restarts before, none after across forced, unforced and longer-stall runs), including a mixed-version first roll from an unpatched parent and the parent stats merge working again with
--skip-hot-restart-parent-statsoff.On a runtime guard. I did not add one: the socket timeouts and the non-blocking queue are structural, and a flag that restores mutual blocking restores the deadlock. Happy to guard the child-side write-off behavior (deadline/probe) if reviewers prefer. The constants (30 s reply deadline, 1 s probe, 10 ms slice, 1 MiB queue) are easy to adjust.
Risk Level: medium (hot restart control channel only)
Testing: unit tests (new), hotrestart integration test, real deployment as described
Docs Changes:
server.hot_restart_udp_forwarding_*statisticsRelease Notes: bug_fixes
Platform Specific Features: N/A