Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Commands run from functions don't exit cleanly on terminal close (SIGHUP)?



I am having issues with ssh sessions not exiting when I closed the terminal window containing them.

I originally noticed this bug while using screen; I believe this bug/problem affects anyone that uses ssh/autossh/tmux/mxrvt/etc from inside a function. It causes screen to hang due to multiple concurrent connections that are orphaned on the client side.

I have reduced the problem down to whether or not the ssh command is executed directly in the shell, or from inside a function.

I isolated the issue from ssh as well; it seems that any command run from inside of a function doesn't cleanly exit when it receives a SIGHUP.

I managed to at least receive the signal before the child exits via TRAPS_ASYNC, but I can't figure out how to make the command die when the trap runs. I've tried "exit" and "return".

The function below reproduces the scenario easily.

willnotdie() {
     echo 'try closing the terminal window while this runs (lasts 10s), notice that the php command is orphaned and does not die'
     echo 'ps ax -o time,ppid,command | grep -v grep| grep "PPID\|php"'
     echo 'if you run the PHP code in this script directly on the CLI, then it exists cleanly on terminal close'

     setopt LOCAL_TRAPS LOCAL_OPTIONS TRAPS_ASYNC
     echo -n "" > ./sig
     (date && echo START) >> ./sig
     trap '(date && echo HUP && exit 1) >> ./sig' HUP
     trap '(date && echo INT) >> ./sig' INT
     php -r '$i = time() + 10; while (true) { if (time() > $i) break; }'
     (date && echo DONE) >> ./sig
}

# monitor the progress with this
while (true) do
ps ax -o time,ppid,command | grep -v grep| grep "PPID\|php"
sleep 1
done

I would appreciate any help.

Thanks in advance.

Alan



Messages sorted by: Reverse Date, Date, Thread, Author