[lxc-devel] [PATCH 1/2] lxc-shutdown: Make all processes exit before timeout if shutdown works

Christian Seiler christian at iwakd.de
Sat Mar 30 14:45:38 UTC 2013


The following rationale is for using the -t option:

Currently, lxc-shutdown uses a subprocess for the timeout handling,
where a 'sleep $TIMEOUT' is executed, which will kill the main process
after the timeout has occurred, thus causing the main process to stop
the container hard with lxc-stop.

On the other hand, if the timeout is not reached, the main process
kills the subprocess. The trouble now is that if you kill a shell that
is running in the background, the kill will only take effect as soon as
the program currently running in the shell exits.

This in turn means that the subprocess will never terminate before
reaching the timeout. In an interactive shell, this does not matter,
since people will just not notice the process and lxc-shutdown returns
immediately. In a non-interactive enironment, however, there may be
circumstances that cause the calling program to wait until even that
subprocess is terminated, which means that shutdown will always take as
long as the timeout, even if the container shuts down quite a bit
earlier.

This change makes sure that also all subprocesses of the background
process are killed from the main process. This will immediately
terminate the background process, thus ensuring the desired behaviour.

Signed-off-by: Christian Seiler <christian at iwakd.de>
---
 src/lxc/lxc-shutdown.in |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/lxc/lxc-shutdown.in b/src/lxc/lxc-shutdown.in
index d82cebf..ee07f75 100644
--- a/src/lxc/lxc-shutdown.in
+++ b/src/lxc/lxc-shutdown.in
@@ -131,7 +131,7 @@ fi
 
 if [ $timeout != "-1" ]; then
     trap dolxcstop EXIT
-    alarm $$ $timeout &
+    alarm $$ $timeout 2>/dev/null &
     alarmpid=$!
 fi
 
@@ -141,7 +141,9 @@ done
 
 if [ $timeout != "-1" ]; then
     trap - EXIT
-    kill $alarmpid
+    # include subprocesses; otherwise, we may have to wait until sleep completes
+    # if called from a non-interactive context
+    kill $alarmpid $(ps --no-headers --ppid $alarmpid -o pid) 2>/dev/null || :
 fi
 
 echo "Container $lxc_name has shut down"
-- 
1.7.10.4





More information about the lxc-devel mailing list