Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

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>

+10
+7
tools/testing/selftests/net/lib/py/ksft.py
··· 153 153 print(res, flush=True) 154 154 155 155 156 + def _ksft_defer_arm(state): 157 + """ Allow or disallow the use of defer() """ 158 + utils.GLOBAL_DEFER_ARMED = state 159 + 160 + 156 161 def ksft_flush_defer(): 157 162 global KSFT_RESULT 158 163 ··· 320 315 comment = "" 321 316 cnt_key = "" 322 317 318 + _ksft_defer_arm(True) 323 319 try: 324 320 func(*args) 325 321 except KsftSkipEx as e: ··· 338 332 ksft_pr(f"Stopping tests due to {type(e).__name__}.") 339 333 KSFT_RESULT = False 340 334 cnt_key = 'fail' 335 + _ksft_defer_arm(False) 341 336 342 337 try: 343 338 ksft_flush_defer()
+3
tools/testing/selftests/net/lib/py/utils.py
··· 142 142 143 143 144 144 GLOBAL_DEFER_QUEUE = [] 145 + GLOBAL_DEFER_ARMED = False 145 146 146 147 147 148 class defer: ··· 154 153 self.args = args 155 154 self.kwargs = kwargs 156 155 156 + if not GLOBAL_DEFER_ARMED: 157 + raise Exception("defer queue not armed, did you use defer() outside of a test case?") 157 158 self._queue = GLOBAL_DEFER_QUEUE 158 159 self._queue.append(self) 159 160