Skip to contentSkip to conversation

selftests: net: exit cleanly on SIGTERM / timeout

PATCH Superseded Jakub Kicinski 4 participants 5 messages Apr 29, 2025 – May 3, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH v2 1/1 selftests: net: exit cleanly on SIGTERM / timeout

ksft runner sends 2 SIGTERMs in a row if a test runs out of time. Handle this in a similar way we handle SIGINT - cleanup and stop running further tests.

Because we get 2 signals we need a bit of logic to ignore the subsequent one, they come immediately one after the other (due to commit 9616cb34b08e ("kselftest/runner.sh: Propagate SIGTERM to runner child")).

This change makes sure we run cleanup (scheduled defer()s) and also print a stack trace on SIGTERM, which doesn't happen by default. Tests occasionally hang in NIPA and it's impossible to tell what they are waiting from or doing.

---
v2:
- remove declaration at the global scope
v1: https://lore.kernel.org/[email protected]

---
 tools/testing/selftests/net/lib/py/ksft.py | 25 +++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
tools/testing/selftests/net/lib/py/ksft.py +24 -1
index 3cfad0fd4570..1b815768bf8a 100644
@@ -3,6 +3,7 @@
 import builtins
 import functools
 import inspect
+import signal
 import sys
 import time
 import traceback
@@ -26,6 +27,10 @@ KSFT_DISRUPTIVE = True
     pass
 
 
+class KsftTerminate(KeyboardInterrupt):
+    pass
+
+
 def ksft_pr(*objs, **kwargs):
     print("#", *objs, **kwargs)
 
@@ -193,6 +198,17 @@ KSFT_DISRUPTIVE = True
     return env
 
 
+def _ksft_intr(signum, frame):
+    # ksft runner.sh sends 2 SIGTERMs in a row on a timeout
+    # if we don't ignore the second one it will stop us from handling cleanup
+    global term_cnt
+    term_cnt += 1
+    if term_cnt == 1:
+        raise KsftTerminate()
+    else:
+        ksft_pr(f"Ignoring SIGTERM (cnt: {term_cnt}), already exiting...")
+
+
 def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
     cases = cases or []
 
@@ -205,6 +221,10 @@ KSFT_DISRUPTIVE = True
                     cases.append(value)
                     break
 
+    global term_cnt
+    term_cnt = 0
+    prev_sigterm = signal.signal(signal.SIGTERM, _ksft_intr)
+
     totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
 
     print("TAP version 13")
@@ -229,11 +249,12 @@ KSFT_DISRUPTIVE = True
             cnt_key = 'xfail'
         except BaseException as e:
             stop |= isinstance(e, KeyboardInterrupt)
+            stop |= isinstance(e, KsftTerminate)
             tb = traceback.format_exc()
             for line in tb.strip().split('\n'):
                 ksft_pr("Exception|", line)
             if stop:
-                ksft_pr("Stopping tests due to KeyboardInterrupt.")
+                ksft_pr(f"Stopping tests due to {type(e).__name__}.")
             KSFT_RESULT = False
             cnt_key = 'fail'
 
@@ -248,6 +269,8 @@ KSFT_DISRUPTIVE = True
         if stop:
             break
 
+    signal.signal(signal.SIGTERM, prev_sigterm)
+
     print(
         f"# Totals: pass:{totals['pass']} fail:{totals['fail']} xfail:{totals['xfail']} xpass:0 skip:{totals['skip']} error:0"
     )
Signature · 1 line
2.49.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Jakub Kicinski

ksft runner sends 2 SIGTERMs in a row if a test runs out of time. Handle this in a similar way we handle SIGINT - cleanup and stop running further tests.

Because we get 2 signals we need a bit of logic to ignore the subsequent one, they come immediately one after the other (due to commit 9616cb34b08e ("kselftest/runner.sh: Propagate SIGTERM to runner child")).

Quoted 6 lines

This change makes sure we run cleanup (scheduled defer()s) and also print a stack trace on SIGTERM, which doesn't happen by default. Tests occasionally hang in NIPA and it's impossible to tell what they are waiting from or doing.

#2 lore raw
In reply to Jakub Kicinski
Quoted 2 lines · Jakub Kicinski
tools/testing/selftests/net/lib/py/ksft.py +2 -0
+class KsftTerminate(KeyboardInterrupt):
+    pass

...

tools/testing/selftests/net/lib/py/ksft.py KSFT_DISRUPTIVE = True · line 252

Quoted 5 lines
tools/testing/selftests/net/lib/py/ksft.py +1 -0
@@ -229,11 +249,12 @@ KSFT_DISRUPTIVE = True
             cnt_key = 'xfail'
         except BaseException as e:
             stop |= isinstance(e, KeyboardInterrupt)
+            stop |= isinstance(e, KsftTerminate)

The first isinstance() will return True for a KsftTerminate as it's a
subclass of KeyboardInterrupt, and thus the second line isn't needed.

maintainer
#3 lore raw
Quoted 4 lines · Edward Cree
Quoted 2 lines · Jakub Kicinski
tools/testing/selftests/net/lib/py/ksft.py +2 -0
+class KsftTerminate(KeyboardInterrupt):
+    pass

...

Quoted 8 lines

tools/testing/selftests/net/lib/py/ksft.py KSFT_DISRUPTIVE = True · line 252

Quoted 5 lines
tools/testing/selftests/net/lib/py/ksft.py +1 -0
@@ -229,11 +249,12 @@ KSFT_DISRUPTIVE = True
             cnt_key = 'xfail'
         except BaseException as e:
             stop |= isinstance(e, KeyboardInterrupt)
+            stop |= isinstance(e, KsftTerminate)

The first isinstance() will return True for a KsftTerminate as it's a
subclass of KeyboardInterrupt, and thus the second line isn't needed.

@Jakub: I'm using the selftests code to refresh my rather rusty python skills, I think it would be good to address the above and keep the codebase clean.

Thanks,

Paolo

author maintainer
#4 lore raw
Quoted 9 lines · On Fri, 2 May 2025 15:05:48 +0200 Paolo Abeni wrote:
Quoted 8 lines

tools/testing/selftests/net/lib/py/ksft.py KSFT_DISRUPTIVE = True · line 252

Quoted 5 lines
tools/testing/selftests/net/lib/py/ksft.py +1 -0
@@ -229,11 +249,12 @@ KSFT_DISRUPTIVE = True
             cnt_key = 'xfail'
         except BaseException as e:
             stop |= isinstance(e, KeyboardInterrupt)
+            stop |= isinstance(e, KsftTerminate)  

The first isinstance() will return True for a KsftTerminate as it's a
subclass of KeyboardInterrupt, and thus the second line isn't needed.

Quoted 3 lines

@Jakub: I'm using the selftests code to refresh my rather rusty python skills, I think it would be good to address the above and keep the codebase clean.

Right, right, I was just distracted the last 3 days, wasn't trying to ignore :) Will respin shortly, good catch indeed.