]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests: net: py: ensure defer() is only used within a test case
authorJakub Kicinski <kuba@kernel.org>
Thu, 8 Jan 2026 22:52:57 +0000 (14:52 -0800)
committerJakub Kicinski <kuba@kernel.org>
Sat, 10 Jan 2026 23:11:59 +0000 (15:11 -0800)
I wasted a couple of hours recently after accidentally adding
a defer() from within a function which itself was called as
part of defer(). This leads to an infinite loop of defer().
Make sure this cannot happen and raise a helpful exception.

I understand that the pair of _ksft_defer_arm() calls may
not be the most Pythonic way to implement this, but it's
easy enough to understand.

Reviewed-by: Petr Machata <petrm@nvidia.com>
Link: https://patch.msgid.link/20260108225257.2684238-2-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/selftests/net/lib/py/ksft.py
tools/testing/selftests/net/lib/py/utils.py

index 248cd1a723a3b81de5ef65bd46320c8ee45cd425..0a96f88bb60a2e1eef05065224b02dec8c58be2d 100644 (file)
@@ -153,6 +153,11 @@ def ktap_result(ok, cnt=1, case_name="", comment=""):
     print(res, flush=True)
 
 
+def _ksft_defer_arm(state):
+    """ Allow or disallow the use of defer() """
+    utils.GLOBAL_DEFER_ARMED = state
+
+
 def ksft_flush_defer():
     global KSFT_RESULT
 
@@ -315,6 +320,7 @@ def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
         comment = ""
         cnt_key = ""
 
+        _ksft_defer_arm(True)
         try:
             func(*args)
         except KsftSkipEx as e:
@@ -332,6 +338,7 @@ def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
                 ksft_pr(f"Stopping tests due to {type(e).__name__}.")
             KSFT_RESULT = False
             cnt_key = 'fail'
+        _ksft_defer_arm(False)
 
         try:
             ksft_flush_defer()
index 2dde34560d653b1f99b2ee2e1fbf6e503f4c3ccb..824f039d384c683986975c189422a85850d1d577 100644 (file)
@@ -142,6 +142,7 @@ class bkg(cmd):
 
 
 GLOBAL_DEFER_QUEUE = []
+GLOBAL_DEFER_ARMED = False
 
 
 class defer:
@@ -153,6 +154,8 @@ class defer:
         self.args = args
         self.kwargs = kwargs
 
+        if not GLOBAL_DEFER_ARMED:
+            raise Exception("defer queue not armed, did you use defer() outside of a test case?")
         self._queue = GLOBAL_DEFER_QUEUE
         self._queue.append(self)