🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #20452

[PATCH net-next] selftests: net: py: preserve existing namespaces on creation failure

#20452
From: 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
Technical Details
Message-ID: <20261122154845.1272203-1-xujingjing@cmss.chinamobile.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-368730-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-368730-linux-kernel-netdev=dewy.ru
From: xujingjing@cmss.chinamobile.com (Jingjing Xu)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next] selftests: net: py: preserve existing namespaces on creation failure
Date: Sun, 22 Nov 2026 15:48:44 +0000
Organization: Usenet Network
Message-ID: <20261122154845.1272203-1-xujingjing@cmss.chinamobile.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3669593"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:HoldSBxkwLMabJKHqRVN0WTdwvE= sha256:WOjT406H+FOJveOP6vk3KyIT5EUeMzy3owL4T3Uh/4Q=
	sha1:ZpZRvl95x+DB4XulvZxagJIT7eI= sha256:gwPt+jx0XMQ0ilPYLq105vmaLy9bBVtTeDHkRYsEjtE=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 5A53A125D7A; Sun, 22 Nov 2026 18:49:05 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.75
X-Spam-Status: No, score=-2.75 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_INVALID=0.1, DKIM_SIGNED=0.1,
	HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
	URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=fail (1024-bit key)
	reason="fail (body has been altered)" header.d=cmss.chinamobile.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 7x0pPaCjhftQ;
	Sun, 22 Nov 2026 18:49:00 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-368730-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=cmss.chinamobile.com
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 234A0125D7F; Sun, 22 Nov 2026 18:48:52 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id D4DB4282EE; Sun, 22 Nov 2026 15:48:50 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795362530;
	b=GPkn9+SILhjvkfzRlgNPwwe2bx5SwLCA6UnErypVLy2LMaSmleQzKCkLJI0ZH24T4fJT
	 sNHQCDm9u7daRubCLgnW9gMKMEwP6qjvUm2cNiaiOo0NmKmyzEs6o07aq3T3ttA/DkjtA
	 EaiaB+Pl3iExWCKdI2XXBq2kKk/G6FG3Q5vCZxtjIISx46D7AwVQR1N2XZPmxIgPwYm9I
	 EW+86Nu0rxe/xkClOlKMx9VwO76G9+56lXuGxDQYwzWVJj6Kli903xZWYoMiidSifoLF3
	 N7U1Sjg7p+umjBfiI6pNJ0umqpah6TOw+vOTyHKsCMQsjikkXRFyE5YbyucsIFNfnzg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795362530;
	h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:X-RM-TagInfo:
	 X-RM-SPAM-FLAG:Received:X-RM-TRANSID:X-RM-TagInfo:X-RM-SPAM-FLAG:
	 Received:X-RM-TRANSID:From:To:Cc:Subject:Date:Message-ID:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=esH12vzr2t3nTD+acOaBkj79Ql9gTvcuxE2yoWswOSI=;
	b=v3w6XFetnKloxjXThrhUqTtF1mQe7TJZFb6B59wSUqe5RcoWv1PUs/zchwxNOLaXQTuE
	 ke8HoVVj3AEQJgPZIlPrtWpz8SUQ4Xd/PmJhy6cqHMNbZlfe/HB5zUXxkQ75EcgwGLatQ
	 d6kIC+SFIa9l4baWvXdzqcd6euhYsyiXLoaSrgQuYcy5Ru+H/ne57bkdsAOnspLhGgLvQ
	 3yT4V4V1E1EHl5WsdKbCac+onW4m/rlwWoydse0hZh9TmOWNTYpEF3bS/YzAgMV67HtSW
	 S0RiFw9VVKsIK4ziLNhPISCeFiJOyyooLh7R/pUfY/XB44YXM0XXkG7nE86L7W19gSQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=cmss.chinamobile.com header.i=@cmss.chinamobile.com header.a=rsa-sha256 header.s=default header.b=j4mivLUg;
	dmarc=pass header.from=cmss.chinamobile.com;
	spf=pass smtp.mailfrom=cmss.chinamobile.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id DD07A606A32C;
	Sun, 22 Nov 2026 15:48:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=cmss.chinamobile.com header.i=@cmss.chinamobile.com header.a=rsa-sha256 header.s=default header.b=j4mivLUg
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=111.22.67.137
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795362529;
	b=En00QGSlCmgTv9Px4m82yXI5IlCCWygLdwYVxM2cnheXRZuHsBWv7lSR8UG0sv9DnN2j
	 ZW26mzHLzAxfyZgnAIJtMiF4H6ZArWjI1DQr2/5oT9npkR/IMaOhOUXTOP/V9imoYNHWI
	 F6fqH7s5f+DsogVIdfMdtrqzdqVO+V1OVmgu4/hz1VOX65x5bgZiE4JilSbzP4XEl+OXH
	 mc+in4WY2HYRAV3PWwuYULZ641il3RuVwxzDUwOVq3T/hQHoqDVN6bu5uubYzrwkrnFBU
	 m38fA5Zzw+vMEI0rqvrUJQzv5xd89tTU72Rv7HEvfkW/OEGYgn5k7N/gHQE/RoF+j+A==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795362529;
	h=DMARC-Filter:DKIM-Signature:X-RM-TagInfo:X-RM-SPAM-FLAG:Received:
	 X-RM-TRANSID:X-RM-TagInfo:X-RM-SPAM-FLAG:Received:X-RM-TRANSID:From:
	 To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Transfer-Encoding;
	bh=esH12vzr2t3nTD+acOaBkj79Ql9gTvcuxE2yoWswOSI=;
	b=qWf9gR/VBBEHLUCoZKkpJIfEjYAQiajfTjixTDFVCfV0BsT8HyI/QFeH8lv41hWIYQMj
	 pyA4S+4tD73+aMMQEKx0+JIKh6CJYAYgkIHMpqQbQyKuYNHXazm1o09VoQjYidDv49g7j
	 5HdgRwJU+WcXCaCLIRAbz4SPwINoRPfPf5ktT6AnNVEDWpX9ql2H6o1i4UfnE19xTJPeo
	 ZPLySWuekpYhRxZrzvQPUJUc8htdTdBhHAOzRGT8w9LJEZ55DpyxM7EKlm/ERDOoSznGj
	 6EDtT1NDmG0dQ3+SEmMlkuZJN+FNHjVA+Uale85BZMpO9i+aC5WVXdVJbYMhWPFFd/Q==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=cmss.chinamobile.com;
	spf=pass smtp.mailfrom=cmss.chinamobile.com;
	dkim=pass header.d=cmss.chinamobile.com header.i=@cmss.chinamobile.com header.a=rsa-sha256 header.s=default header.b=j4mivLUg;
	arc=none smtp.remote-ip=111.22.67.137
X-Received: from cmccmta4.chinamobile.com (cmccmta4.chinamobile.com [111.22.67.137])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id E0300606A333;
	Sun, 22 Nov 2026 15:48:47 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E0300606A333
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmss.chinamobile.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmss.chinamobile.com
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
	d=cmss.chinamobile.com; s=default; l=0;
	h=from:subject:message-id:to:cc:mime-version;
	bh=47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=;
	b=j4mivLUgRulDXixCxmSaw/I5P6UzilBrb1gt9Jtp7Go/RmyrBaVrvxLePTy5D+bl6WyXdITend46s
	 vfgkwceGqVpv7ZR2fh+riTAqs1povUVJH56SNkJHUxPldC8gbFWBMpPC2kn7eHXBBZ49cG9qr914cv
	 zX8syYdQ8RXY3jbI=
X-RM-TagInfo: emlType=0                                       
X-RM-SPAM-FLAG: 00000000
X-Received: from spf.mail.chinamobile.com (unknown[10.188.0.87])
	by rmmx-syy-dmz-app04-12004 (RichMail) with SMTP id 2ee46b030edda13-d4093;
	Sun, 22 Nov 2026 23:48:45 +0800 (CST)
X-RM-TRANSID: 2ee46b030edda13-d4093
X-RM-TagInfo: emlType=0                                       
X-RM-SPAM-FLAG: 00000000
X-Received: from home-ubuntu-251 (unknown[180.106.213.53])
	by rmsmtp-syy-appsvr10-12010 (RichMail) with SMTP id 2eea6b030edcf26-72726;
	Sun, 22 Nov 2026 23:48:45 +0800 (CST)
X-RM-TRANSID: 2eea6b030edcf26-72726
X-Cc: linux-kselftest@vger.kernel.org,
	kuba@kernel.org,
	edumazet@kernel.org,
	pabeni@redhat.com,
	horms@kernel.org,
	shuah@kernel.org,
	willemb@google.com
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list