From: Daniel Borkmann <daniel@iogearbox.net>
To: netdev@vger.kernel.org
Cc: kuba@kernel.org, dw@davidwei.uk, pabeni@redhat.com, razor@blackwall.org
Subject: [PATCH net-next v2 1/3] tools/ynl: Make YnlFamily closeable as a context manager
Date: Tue, 14 Apr 2026 00:08:04 +0200 [thread overview]
Message-ID: <20260413220809.604592-2-daniel@iogearbox.net> (raw)
In-Reply-To: <20260413220809.604592-1-daniel@iogearbox.net>
YnlFamily opens an AF_NETLINK socket in __init__ but has no way
to release it other than leaving it to the GC. YnlFamily holds a
self reference cycle through SpecFamily's self.family = self
in its super().__init__() call, so refcount GC cannot reclaim
it and the socket stays open until the cyclic GC runs.
If a test creates a guest netns, instantiates a YnlFamily inside
it via NetNSEnter(), performs some test case work via Ynl, and
then deletes the netns, then the 'ip netns del' only drops the
mount binding and cleanup_net in the kernel never runs, so any
subsequent test case assertions that objects got cleaned up would
fail given this only gets triggered later via cyclic GC run.
Add an explicit close() that closes the netlink socket and wire
up the __enter__/__exit__ so callers can scope the instance
deterministically via 'with YnlFamily(...) as ynl: ...'.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
---
tools/net/ynl/pyynl/lib/ynl.py | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/tools/net/ynl/pyynl/lib/ynl.py b/tools/net/ynl/pyynl/lib/ynl.py
index 9c078599cea0..f63c6f828735 100644
--- a/tools/net/ynl/pyynl/lib/ynl.py
+++ b/tools/net/ynl/pyynl/lib/ynl.py
@@ -731,6 +731,16 @@ class YnlFamily(SpecFamily):
bound_f = functools.partial(self._op, op_name)
setattr(self, op.ident_name, bound_f)
+ def close(self):
+ if self.sock is not None:
+ self.sock.close()
+ self.sock = None
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc, tb):
+ self.close()
def ntf_subscribe(self, mcast_name):
mcast_id = self.nlproto.get_mcast_id(mcast_name, self.mcast_groups)
--
2.43.0
next prev parent reply other threads:[~2026-04-13 22:08 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-13 22:08 [PATCH net-next v2 0/3] Follow-ups to nk_qlease net selftests Daniel Borkmann
2026-04-13 22:08 ` Daniel Borkmann [this message]
2026-04-14 5:57 ` [PATCH net-next v2 1/3] tools/ynl: Make YnlFamily closeable as a context manager Nikolay Aleksandrov
2026-04-13 22:08 ` [PATCH net-next v2 2/3] selftests/net: Split netdevsim tests from HW tests in nk_qlease Daniel Borkmann
2026-04-14 5:58 ` Nikolay Aleksandrov
2026-04-13 22:08 ` [PATCH net-next v2 3/3] selftests/net: Add additional test coverage " Daniel Borkmann
2026-04-14 5:59 ` Nikolay Aleksandrov
2026-04-14 2:12 ` [PATCH net-next v2 0/3] Follow-ups to nk_qlease net selftests Jakub Kicinski
2026-04-14 7:33 ` Daniel Borkmann
2026-04-14 7:51 ` Daniel Borkmann
2026-04-14 15:50 ` patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260413220809.604592-2-daniel@iogearbox.net \
--to=daniel@iogearbox.net \
--cc=dw@davidwei.uk \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=razor@blackwall.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox