Skip to content

network: restore the calling thread's namespace after execInNetworkNamespace - #47776

Merged
mattklein123 merged 1 commit into
envoyproxy:mainfrom
bpalermo:netns-restore-thread-self
Sep 30, 2026
Merged

mattklein123 merged 1 commit into
envoyproxy:mainfrom
bpalermo:netns-restore-thread-self

Conversation

@bpalermo

Copy link
Copy Markdown
Contributor

Commit Message

network: restore the calling thread's namespace after execInNetworkNamespace

Additional Description

Network::Utility::execInNetworkNamespace records the namespace to return to by opening
/proc/self/ns/net. setns(CLONE_NEWNET) applies to the calling thread only, and /proc/self
refers to the thread group leader, so that path names the main thread's current namespace, not the
calling thread's.

The helper runs on the main thread for listener socket creation
(ProdListenerComponentFactory::createListenSocket) and on whichever thread owns the dispatcher for
client connections whose source address carries a network_namespace_filepath
(DispatcherImpl::createClientConnection), i.e. on workers. When a worker's restore coincides with
the main thread being inside another namespace (creating a listener there, or a health check
connection from there), the worker is "restored" into that namespace and stays in it until its next
call to the helper, creating its sockets in the wrong namespace in the meantime. The failure is rare
and self-healing, which makes it hard to attribute from the outside.

Fix: open /proc/thread-self/ns/net, the calling thread's own namespace (Linux 3.17+). The
comment explains the semantics so it is not reverted to /proc/self.

Risk Level

Low. One path change; behavior is identical whenever main and calling thread are in the same
namespace, which is the only case the previous code handled correctly.

Testing

ExecInNetnsTest.Basic asserts the namespace recorded before the switch, and restored after it,
is /proc/thread-self/ns/net. A real two-thread reproduction needs namespace privileges, so it is
not part of the unit tests.

Docs Changes

N/A

Release Notes

Added a bug fix fragment.

Platform Specific Features

Linux only (the helper is compiled only on Linux).

…mespace

setns(CLONE_NEWNET) applies to the calling thread only, but the helper recorded
the namespace to return to from /proc/self/ns/net, which is the thread group
leader's namespace. A worker thread that switched namespaces to create a
connection while the main thread was itself inside another namespace was
restored into that namespace instead of its own. Use /proc/thread-self/ns/net.

Signed-off-by: Bruno Palermo <[email protected]>
@repokitteh-read-only

Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #47776 was opened by bpalermo.

see: more, trace.

@bpalermo

Copy link
Copy Markdown
Contributor Author

/retest

@bpalermo
bpalermo marked this pull request as ready for review September 28, 2026 10:04
@mattklein123
mattklein123 merged commit 7d6ca32 into envoyproxy:main Sep 30, 2026
25 checks passed
bpalermo added a commit to bpalermo/aether that referenced this pull request Oct 10, 2026
…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]>
@bpalermo
bpalermo deleted the netns-restore-thread-self branch October 13, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants