🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Jingjing Xu Sun, 22 Nov 2026 15:48
[PATCH net-next] selftests: net: py: preserve existing namespaces on creation failure
#20452
Author: Jingjing Xu
Date: Sun, 22 Nov 2026 15:48
63 lines
10922 bytes
NetNS records its name before running ip netns add. If creation fails
because that name already exists, destruction of the partially built
object deletes the pre-existing handle that it never acquired.

Initialize the owned name to None and record it only after creation
succeeds. Successful creation and context cleanup keep their behavior.

Run this as root from tools/testing/selftests/net. Its mount and network
namespaces are private; it prints False before the fix and True after:

  unshare -mn --propagation private sh -ec '
    mount -t tmpfs tmpfs /run; mkdir /run/netns
    exec python3 -
  ' <<'PYCODE'
  import gc, os
  from lib.py import CmdExitFailure, NetNS, ip
  ip("netns add review-existing")
  try: NetNS("review-existing")
  except CmdExitFailure: pass
  gc.collect()
  print(os.path.exists("/run/netns/review-existing"))
  PYCODE

Found by AI-assisted inspection and reproduced with real ip netns calls.
Eight ownership/lifecycle checks go from six to eight passing, including
preservation of the original inode and marker interface. Explicit,
automatic and empty-name creation, body exceptions and repeated cleanup
were checked. Current in-tree callers normally use generated names;
this is not a claim of frequent collisions. Removing a named handle does
not necessarily destroy all namespace references. No kernel modification
or fault injection was used, and no hardware suite was run.

Fixes: 1880f272d2f9 ("selftests: drv-net: construct environment for running tests which require an endpoint")
Signed-off-by: Jingjing Xu <xujingjing@cmss.chinamobile.com>
---
 tools/testing/selftests/net/lib/py/netns.py | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/net/lib/py/netns.py b/tools/testing/selftests/net/lib/py/netns.py
index 526f6aa80077..981be5f842ff 100644
--- a/tools/testing/selftests/net/lib/py/netns.py
+++ b/tools/testing/selftests/net/lib/py/netns.py
@@ -15,11 +15,11 @@ libc = ctypes.cdll.LoadLibrary('libc.so.6')
 
 class NetNS:
     def __init__(self, name=None):
-        if name:
-            self.name = name
-        else:
-            self.name = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
-        ip('netns add ' + self.name)
+        self.name = None
+        if not name:
+            name = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
+        ip('netns add ' + name)
+        self.name = name
 
     def __del__(self):
         if self.name:
base-commit: 2208ab00f4f213ade91ab775e7f8c4dacdcc856e
-- 
2.53.0
Re: [PATCH net-next] selftests: net: py: preserve existing namespaces on creation failure
#24931
Author: patchwork-bot+netdevbpf
Date: Tue, 01 Dec 2026 03:00
24 lines
8676 bytes
Hello:

This patch was applied to netdev/net-next.git (main)
by Willem de Bruijn <willemb@google.com>:

On Sun, 22 Nov 2026 15:48:44 +0000 you wrote:
> NetNS records its name before running ip netns add. If creation fails
> because that name already exists, destruction of the partially built
> object deletes the pre-existing handle that it never acquired.
> 
> Initialize the owned name to None and record it only after creation
> succeeds. Successful creation and context cleanup keep their behavior.
> 
> [...]

Here is the summary with links:
  - [net-next] selftests: net: py: preserve existing namespaces on creation failure
    https://git.kernel.org/netdev/net-next/c/4d93feebb538

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads