zsh-workers
 help / color / mirror / code / Atom feed
* Commands run from functions don't exit cleanly on terminal close (SIGHUP)?
@ 2012-10-24 14:03 Alan Pinstein
  2012-10-24 14:10 ` Alan Pinstein
  0 siblings, 1 reply; 7+ messages in thread
From: Alan Pinstein @ 2012-10-24 14:03 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Commands run from functions don't exit cleanly on terminal close (SIGHUP)?
  2012-10-24 14:03 Commands run from functions don't exit cleanly on terminal close (SIGHUP)? Alan Pinstein
@ 2012-10-24 14:10 ` Alan Pinstein
  2012-10-25 10:22   ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Alan Pinstein @ 2012-10-24 14:10 UTC (permalink / raw)
  To: zsh-workers

I need to make one clarification; the PPID ends up as 1 (orphaned) if no traps are installed; if the HUP trap is installed, the PPID remains correct, but it still doesn't exit.

Alan

On Oct 24, 2012, at 10:03 AM, Alan Pinstein wrote:

> 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Commands run from functions don't exit cleanly on terminal close (SIGHUP)?
  2012-10-24 14:10 ` Alan Pinstein
@ 2012-10-25 10:22   ` Peter Stephenson
  2012-10-25 11:48     ` Alan Pinstein
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2012-10-25 10:22 UTC (permalink / raw)
  To: zsh-workers

On Wed, 24 Oct 2012 10:10:42 -0400
Alan Pinstein <apinstein@mac.com> wrote:
> I need to make one clarification; the PPID ends up as 1 (orphaned) if
> no traps are installed; if the HUP trap is installed, the PPID remains
> correct, but it still doesn't exit.

In my case (Fedora 15 on x86_64), I'm seeing the programme exit if there
aren't any traps, so I've got different behaviour.  This could be as the
result of a race or indeed pretty much anything else...

With the HUP trap, it's not exiting, but that doesn't actually surprise
me:  you're handling the trap within the shell, so it's not being
propagated as a signal to PHP.

There are lots of variables, I'll try to experiment some more, in
particular finding differences between the behaviour in functions and
not (which wouldn't surprise me, there is some special handling).

pws


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Commands run from functions don't exit cleanly on terminal close (SIGHUP)?
  2012-10-25 10:22   ` Peter Stephenson
@ 2012-10-25 11:48     ` Alan Pinstein
  2012-10-25 13:22       ` Alan Pinstein
  0 siblings, 1 reply; 7+ messages in thread
From: Alan Pinstein @ 2012-10-25 11:48 UTC (permalink / raw)
  To: zsh-workers

I didn't even think about it being platform dependent. I am on Mac OS 10.7.5. I will try to test on a centos box today. 

Alan

On Oct 25, 2012, at 6:22 AM, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:

> On Wed, 24 Oct 2012 10:10:42 -0400
> Alan Pinstein <apinstein@mac.com> wrote:
>> I need to make one clarification; the PPID ends up as 1 (orphaned) if
>> no traps are installed; if the HUP trap is installed, the PPID remains
>> correct, but it still doesn't exit.
> 
> In my case (Fedora 15 on x86_64), I'm seeing the programme exit if there
> aren't any traps, so I've got different behaviour.  This could be as the
> result of a race or indeed pretty much anything else...
> 
> With the HUP trap, it's not exiting, but that doesn't actually surprise
> me:  you're handling the trap within the shell, so it's not being
> propagated as a signal to PHP.
> 
> There are lots of variables, I'll try to experiment some more, in
> particular finding differences between the behaviour in functions and
> not (which wouldn't surprise me, there is some special handling).
> 
> pws


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Commands run from functions don't exit cleanly on terminal close (SIGHUP)?
  2012-10-25 11:48     ` Alan Pinstein
@ 2012-10-25 13:22       ` Alan Pinstein
  2012-10-25 14:03         ` Peter Stephenson
  2012-10-25 14:54         ` Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: Alan Pinstein @ 2012-10-25 13:22 UTC (permalink / raw)
  To: zsh-workers

I tried from CentOS but got the same thing.

However it might be confounded by the fact that I have to SSH to the centos box...

Just to clarify, you're seeing it exit *before* the loop finishes? This is what I see in the "sig" file:

Thu Oct 25 08:20:05 CDT 2012
START
Thu Oct 25 08:20:26 CDT 2012
HUP
Thu Oct 25 08:20:36 CDT 2012
DONE

The HUP is received but doesn't affect the process until the php command exits.

So you're just seeing this?

Thu Oct 25 08:20:05 CDT 2012
START
Thu Oct 25 08:20:26 CDT 2012
HUP

Alan

On Oct 25, 2012, at 7:48 AM, Alan Pinstein wrote:

> I didn't even think about it being platform dependent. I am on Mac OS 10.7.5. I will try to test on a centos box today. 
> 
> Alan
> 
> On Oct 25, 2012, at 6:22 AM, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> 
>> On Wed, 24 Oct 2012 10:10:42 -0400
>> Alan Pinstein <apinstein@mac.com> wrote:
>>> I need to make one clarification; the PPID ends up as 1 (orphaned) if
>>> no traps are installed; if the HUP trap is installed, the PPID remains
>>> correct, but it still doesn't exit.
>> 
>> In my case (Fedora 15 on x86_64), I'm seeing the programme exit if there
>> aren't any traps, so I've got different behaviour.  This could be as the
>> result of a race or indeed pretty much anything else...
>> 
>> With the HUP trap, it's not exiting, but that doesn't actually surprise
>> me:  you're handling the trap within the shell, so it's not being
>> propagated as a signal to PHP.
>> 
>> There are lots of variables, I'll try to experiment some more, in
>> particular finding differences between the behaviour in functions and
>> not (which wouldn't surprise me, there is some special handling).
>> 
>> pws


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Commands run from functions don't exit cleanly on terminal close (SIGHUP)?
  2012-10-25 13:22       ` Alan Pinstein
@ 2012-10-25 14:03         ` Peter Stephenson
  2012-10-25 14:54         ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Peter Stephenson @ 2012-10-25 14:03 UTC (permalink / raw)
  To: zsh-workers

On Thu, 25 Oct 2012 09:22:24 -0400
Alan Pinstein <apinstein@mac.com> wrote:
> So you're just seeing this?
> 
> Thu Oct 25 08:20:05 CDT 2012
> START
> Thu Oct 25 08:20:26 CDT 2012
> HUP

With the HUP trap in place, as I said, it *doesn't* exit early, so I
won't see the combination of that message in the log and it exiting
early.  I don't have it set up here, I'll next be able to look when I'm
home.

pws


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Commands run from functions don't exit cleanly on terminal close (SIGHUP)?
  2012-10-25 13:22       ` Alan Pinstein
  2012-10-25 14:03         ` Peter Stephenson
@ 2012-10-25 14:54         ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2012-10-25 14:54 UTC (permalink / raw)
  To: Alan Pinstein, zsh-workers@zsh.org 

On Oct 25,  9:22am, Alan Pinstein wrote:
}
} I tried from CentOS but got the same thing.

I tried this ssh'd from CentOS to MacOS (Darwin Kernel Version 12.2.0).

Thu Oct 25 07:29:22 PDT 2012
START
Thu Oct 25 07:29:24 PDT 2012
DONE

So the HUP trap was not tripped, but php exited.  This isn't what I
expected, but on reflection I think it's correct:  php is the foreground
job, so if there is a HUP it goes to php when the connection closes.  The
shell just sees EOF, at which point it would send a HUP to any other jobs
that are still running, but there aren't any so it simply exits.

I get the same thing whether or not the NO_HUP option is set.  This I'm
less sure about, but IIRC the option just prevents the shell from killing
off background jobs when the shell exits, it doesn't actually block the
signal from foreground jobs.

However, note that I closed ssh via the "~." command rather than by
closing a window.  Are you running a local terminal whose command is
ssh, or are you using ssh to run a remote terminal and display it on
your local desktop?  Are you sure the local side of ssh is exiting
when the window closes?

You said your output is:

} Thu Oct 25 08:20:05 CDT 2012
} START
} Thu Oct 25 08:20:26 CDT 2012
} HUP
} Thu Oct 25 08:20:36 CDT 2012
} DONE

How are you getting 21 seconds to pass between START and HUP?  Shouldn't
the whole thing run for no more than 10 seconds?


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-10-25 14:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-24 14:03 Commands run from functions don't exit cleanly on terminal close (SIGHUP)? Alan Pinstein
2012-10-24 14:10 ` Alan Pinstein
2012-10-25 10:22   ` Peter Stephenson
2012-10-25 11:48     ` Alan Pinstein
2012-10-25 13:22       ` Alan Pinstein
2012-10-25 14:03         ` Peter Stephenson
2012-10-25 14:54         ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).