tests: fix missing dataclasses module in python 3.6
[vpp.git] / test / scripts / run_in_venv_with_cleanup.sh
index 541f584..af32f87 100755 (executable)
@@ -2,6 +2,22 @@
 
 rv=0
 
+# Minimalist version of cleanup, used for signal handling.
+# Sends a SIGKILL to the entire process group, including ourselves.
+# Needs just two external commands, making it more
+# robust in case of resource issues.
+panic() {
+       echo "$0(pid $$): Caught a signal, emergency clean-up"
+       # use "pgid:1=" output format to get unpadded process group ID
+       group_id=`ps -p $$ -o pgid:1=`
+       echo "$0(pid $$): sending kill to process group ID:${group_id}"
+       kill -9 -- -${group_id}
+       # not reached
+}
+
+# Happy camper leisurely clean up - send the signal only to other
+# processes in the process group, and also check
+# that the processes exists before sending the signal.
 atexit() {
        group_id=`ps -p $$ -o pgid=`
        my_id=$$
@@ -14,10 +30,10 @@ atexit() {
                        kill -9 $id
                fi
        done
-       exit $rv
+       exit ${rv}
 }
 
-trap "atexit;" SIGINT SIGTERM
+trap "panic;" SIGINT SIGTERM
 
 FORCE_FOREGROUND=$1
 shift
@@ -30,9 +46,10 @@ then
        $*
 else
        $* &
-       wait
+       pid=$!
+       wait ${pid}
 fi
 
 rv=$?
 atexit
-exit $rv
+exit ${rv}