SSH connection hanging on logout

Rachit Siamwalla rachit at ensim.com
Thu May 10 11:36:07 EST 2001


If this is a feature, not a bug, then, my (stupid?) question(s) are this:

1. Telnet doesn't have the same problem. (yes, telnet isn't exactly the same
thing, but... this is related to what John Bowman's patch does)
2. F-secure SSH doesn't have the same problem.

Also, I believe that this problem was attempted to workedaround sometime in
2.3.0p1 timeframe by if the connection was closed, ssh will close and exit
immediately (don't quote me on this, this info was gleaned through
observation, not reading the actual code). However, this triggered the
unfortunate bug in that:

ssh myserver echo 0

will not actually print anything out, because the close and exit was too
soon. I am not a pty expert, but I wonder how f-secure ssh managed to get
around this issue (it doesn't have either problems).

-rchit

-----Original Message-----
From: Jason Stone [mailto:jason at shalott.net]
Sent: Saturday, May 05, 2001 4:54 AM
To: openssh-unix-dev at mindrot.org
Subject: Re: SSH connection hanging on logout


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> About the hang-on-exit bug: this is the TODO item which shows up when you
> run "ssh server 'sleep 20 & exit'".
> 
> * The shell starts up, and starts its own session.  As a side-effect, it
>   gets its own process group.
> * The sshd server attempts to read from the master side of the pty, and
>   while there are still process with the pty open, no EOF is produced.
> * The sleep command exits, closes its descriptor, sshd detects the EOF,
and
>   the connection gets closed.

Or, put another way, this is a feature, not a bug - sshd has no way of
knowing that "sleep 20" isn't going to eventually produce some output that
you'll want to see, so it stays alive until the background command exits.

The real "bug" is users trying to use the shell's '&' builtin to run
daemon processes.  If you want a command to really be backgrounded (ie, to
daemonize), use something other than '&', something that will make the
command close the pty and either start its own process group or else
become a child of init.  Eg:

perl -e 'fork && exit; close STDIN; close STDOUT; close STDERR; \
setpgrp(0,$$); exec "sleep 20";'

(Watch out for the quoting if you try this on the commandline....)


> Known-good workarounds:
> * bash: shopt huponexit on
> * tcsh: none

* zsh: setopt HUP
(this is usually the default)


If you use zsh, you might also try something like this in your .zshrc:

daemonize(){
        COMMAND="$@"
        perl -e 'fork && exit; close STDIN; close STDOUT; close STDERR; \
                setpgrp(0,$$); exec "'$COMMAND'";' }
}

You would then run "daemonize sleep 20" and the sleep 20 would be run in
the background and not hang the sshd when you exit.

This will almost certainly work in other bourne-compatible shells as well.


- -Jason

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (FreeBSD)
Comment: See https://private.idealab.com/public/jason/jason.gpg

iD8DBQE68+nhswXMWWtptckRAjaVAJ0bbN7PPe0jLC80SPZjDNAvBFuC2wCaA4ep
1IteXaTPMxe2TsKrsLmg20A=
=mEVt
-----END PGP SIGNATURE-----



More information about the openssh-unix-dev mailing list