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

selftests: net: py: handle interrupt during cleanup

Following up on the old discussion [1]. Let the BaseExceptions out of
defer()'ed cleanup. And handle it in the main loop. This allows us to
exit the tests if user hit Ctrl-C during defer().

Link: https://lore.kernel.org/20251119063228.3adfd743@kernel.org # [1]
Reviewed-by: Simon Horman <horms@kernel.org>
Reviewed-by: Petr Machata <petrm@nvidia.com>
Link: https://patch.msgid.link/20251128004846.2602687-1-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+16 -2
+16 -2
tools/testing/selftests/net/lib/py/ksft.py
··· 163 163 entry = global_defer_queue.pop() 164 164 try: 165 165 entry.exec_only() 166 - except BaseException: 166 + except Exception: 167 167 ksft_pr(f"Exception while handling defer / cleanup (callback {i} of {qlen_start})!") 168 168 tb = traceback.format_exc() 169 169 for line in tb.strip().split('\n'): ··· 333 333 KSFT_RESULT = False 334 334 cnt_key = 'fail' 335 335 336 - ksft_flush_defer() 336 + try: 337 + ksft_flush_defer() 338 + except BaseException as e: 339 + tb = traceback.format_exc() 340 + for line in tb.strip().split('\n'): 341 + ksft_pr("Exception|", line) 342 + if isinstance(e, KeyboardInterrupt): 343 + ksft_pr() 344 + ksft_pr("WARN: defer() interrupted, cleanup may be incomplete.") 345 + ksft_pr(" Attempting to finish cleanup before exiting.") 346 + ksft_pr(" Interrupt again to exit immediately.") 347 + ksft_pr() 348 + stop = True 349 + # Flush was interrupted, try to finish the job best we can 350 + ksft_flush_defer() 337 351 338 352 if not cnt_key: 339 353 cnt_key = 'pass' if KSFT_RESULT else 'fail'