selftests: net: py: capitalize defer queue and improve import
Subsystems (3)
Jakub Kicinski
maintainerPATCH 1/2 selftests: net: py: capitalize defer queue and improve import
Import utils and refer to the global defer queue that way instead of importing the queue. This will make it possible to assign value to the global variable. While at it capitalize the name, to comply with the Python coding style.
--- tools/testing/selftests/net/lib/py/ksft.py | 8 ++++---- tools/testing/selftests/net/lib/py/utils.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-)
tools/testing/selftests/net/lib/py/ksft.py +4 -4
@@ -8,7 +8,7 @@ import time import traceback from collections import namedtuple from .consts import KSFT_MAIN_NAME -from .utils import global_defer_queue +from . import utils KSFT_RESULT = None KSFT_RESULT_ALL = True
@@ -157,10 +157,10 @@ KSFT_DISRUPTIVE = True global KSFT_RESULT i = 0 - qlen_start = len(global_defer_queue) - while global_defer_queue: + qlen_start = len(utils.GLOBAL_DEFER_QUEUE) + while utils.GLOBAL_DEFER_QUEUE: i += 1 - entry = global_defer_queue.pop() + entry = utils.GLOBAL_DEFER_QUEUE.pop() try: entry.exec_only() except Exception:
tools/testing/selftests/net/lib/py/utils.py +2 -2
@@ -141,7 +141,7 @@ import time return self.process(terminate=terminate, fail=self.check_fail) -global_defer_queue = [] +GLOBAL_DEFER_QUEUE = [] class defer:
@@ -153,7 +153,7 @@ global_defer_queue = [] self.args = args self.kwargs = kwargs - self._queue = global_defer_queue + self._queue = GLOBAL_DEFER_QUEUE self._queue.append(self) def __enter__(self):
Signature · 1 line
2.52.0
Jakub Kicinski
maintainerPATCH 2/2 selftests: net: py: ensure defer() is only used within a test case
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.
--- tools/testing/selftests/net/lib/py/ksft.py | 7 +++++++ tools/testing/selftests/net/lib/py/utils.py | 3 +++ 2 files changed, 10 insertions(+)
tools/testing/selftests/net/lib/py/ksft.py +7 -0
@@ -153,6 +153,11 @@ KSFT_DISRUPTIVE = True 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 @@ KsftCaseFunction = namedtuple("KsftCaseFunction", comment = "" cnt_key = "" + _ksft_defer_arm(True) try: func(*args) except KsftSkipEx as e:
@@ -332,6 +338,7 @@ KsftCaseFunction = namedtuple("KsftCaseFunction", ksft_pr(f"Stopping tests due to {type(e).__name__}.") KSFT_RESULT = False cnt_key = 'fail' + _ksft_defer_arm(False) try: ksft_flush_defer()
tools/testing/selftests/net/lib/py/utils.py +3 -0
@@ -142,6 +142,7 @@ import time GLOBAL_DEFER_QUEUE = [] +GLOBAL_DEFER_ARMED = False class defer:
@@ -153,6 +154,8 @@ GLOBAL_DEFER_QUEUE = [] 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)
Signature · 1 line
2.52.0
Re: [PATCH net-next 2/2] selftests: net: py: ensure defer() is only used within a test case
Quoted 10 lines · Jakub Kicinski
I wasted a couple of hours recently after accidentally adding a
defer()from within a function which itself was called as part ofdefer(). This leads to an infinite loop ofdefer(). 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.Signed-off-by Jakub Kicinski <[email protected]>
I think we achieve the same without the extra globals though? Just drain the queue and walk through a copy of it?
defer_queue = utils.GLOBAL_DEFER_QUEUE
utils.GLOBAL_DEFER_QUEUE = []
for i, entry in enumerate(defer_queue):
...
if utils.GLOBAL_DEFER_QUEUE:
warning / exception
Quoted 57 lines
--- tools/testing/selftests/net/lib/py/ksft.py | 7 +++++++ tools/testing/selftests/net/lib/py/utils.py | 3 +++ 2 files changed, 10 insertions(+)tools/testing/selftests/net/lib/py/ksft.py +7
-0@@ -153,6 +153,11 @@ KSFT_DISRUPTIVE = True 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 @@ KsftCaseFunction = namedtuple("KsftCaseFunction", comment = "" cnt_key = "" + _ksft_defer_arm(True) try: func(*args) except KsftSkipEx as e:@@ -332,6 +338,7 @@ KsftCaseFunction = namedtuple("KsftCaseFunction", ksft_pr(f"Stopping tests due to {type(e).__name__}.") KSFT_RESULT = False cnt_key = 'fail' + _ksft_defer_arm(False) try: ksft_flush_defer()tools/testing/selftests/net/lib/py/utils.py +3
-0@@ -142,6 +142,7 @@ import time GLOBAL_DEFER_QUEUE = [] +GLOBAL_DEFER_ARMED = False class defer:@@ -153,6 +154,8 @@ GLOBAL_DEFER_QUEUE = [] 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)
Jakub Kicinski
maintainerRe: [PATCH net-next 2/2] selftests: net: py: ensure defer() is only used within a test case
Quoted 14 lines · On Fri, 9 Jan 2026 09:23:54 +0100 Petr Machata wrote:
Quoted 10 lines
I wasted a couple of hours recently after accidentally adding a
defer()from within a function which itself was called as part ofdefer(). This leads to an infinite loop ofdefer(). 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.Signed-off-by Jakub Kicinski <[email protected]>I think we achieve the same without the extra globals though? Just drain the queue and walk through a copy of it?
Quoted 6 lines
defer_queue= utils.GLOBAL_DEFER_QUEUE
utils.GLOBAL_DEFER_QUEUE = []
for i, entry inenumerate(defer_queue):
...
if utils.GLOBAL_DEFER_QUEUE:
warning / exception
That's what I had initially (IIUC), I was assigning None to the queue, and then [] only while inside a test case. It gets slightly hairy because either we need to pass in the queue into the flush function; or we have to restore the queue if something raises and exception during flush (in which case ksft_run() prints a warning and calls ksft_flush_defer() one more time).
That approach definitely worked, but unless I'm missing a trick it was higher complexity and LoC.
Petr Machata
Reviewed-byRe: [PATCH net-next 2/2] selftests: net: py: ensure defer() is only used within a test case
Quoted 22 lines · Jakub Kicinski
Quoted 14 lines · On Fri, 9 Jan 2026 09:23:54 +0100 Petr Machata wrote:
Quoted 10 lines
I wasted a couple of hours recently after accidentally adding a
defer()from within a function which itself was called as part ofdefer(). This leads to an infinite loop ofdefer(). 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.Signed-off-by Jakub Kicinski <[email protected]>I think we achieve the same without the extra globals though? Just drain the queue and walk through a copy of it?
Quoted 6 lines
defer_queue= utils.GLOBAL_DEFER_QUEUE
utils.GLOBAL_DEFER_QUEUE = []
for i, entry inenumerate(defer_queue):
...
if utils.GLOBAL_DEFER_QUEUE:
warning / exception
Quoted 6 lines
That's what I had initially (IIUC), I was assigning None to the queue, and then [] only while inside a test case. It gets slightly hairy because either we need to pass in the queue into the flush function; or we have to restore the queue if something raises and exception during flush (in which case
ksft_run()prints a warning and callsksft_flush_defer()one more time).
Hmm, yeah, the exception robustness will complicate it. OK, let's have it your way then :)
Quoted 6 lines · Jakub Kicinski
Import utils and refer to the global defer queue that way instead of importing the queue. This will make it possible to assign value to the global variable. While at it capitalize the name, to comply with the Python coding style.
Signed-off-by Jakub Kicinski <[email protected]>
Quoted 6 lines · Jakub Kicinski
Import utils and refer to the global defer queue that way instead of importing the queue. This will make it possible to assign value to the global variable. While at it capitalize the name, to comply with the Python coding style.
Signed-off-by Jakub Kicinski <[email protected]>
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Thu, 8 Jan 2026 14:52:56 -0800 you wrote:
Import utils and refer to the global defer queue that way instead of importing the queue. This will make it possible to assign value to the global variable. While at it capitalize the name, to comply with the Python coding style.
Signed-off-by Jakub Kicinski <[email protected]>[...]
Here is the summary with links:
- [net-next,1/2] selftests: net: py: capitalize defer queue and improve import
https://git.kernel.org/netdev/net-next/c/799a4912eea7 - [net-next,2/2] selftests: net: py: ensure defer() is only used within a test case https://git.kernel.org/netdev/net-next/c/7a1ff3545ade
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html