zsh-users
 help / color / mirror / code / Atom feed
* zsh dumping core because I don't grok TRAPEXIT
@ 2001-09-07 22:04 Sweth Chandramouli
  2001-09-08  5:50 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Sweth Chandramouli @ 2001-09-07 22:04 UTC (permalink / raw)
  To: ZSH Users

[-- Attachment #1: Type: text/plain, Size: 3847 bytes --]

	I had an alias defined at one point as follows:

alias rmutt="/usr/local/bin/mutt -Z || /usr/local/bin/mutt -f '=inbox'"

	, where the first invocation of mutt checks to see if any
of my "important" mailboxes have new mail, and opens mutt in the first
such mailbox if there is one, and the second invocation opens mutt in my
default inbox.  The problem I found myself running into was that I
often, while composing messages, use ^Z to suspend mutt (or, more
usually, vi) to go look at something else, and then return to mutt; for
some reason, if I do that, then when mutt finally returns, it returns
with an exit code of 23 rather than 0 even though everything worked
correctly--the end result is that, if there _is_ mail in one of my
important mailboxes, and I happen to suspend mutt during that session,
then when I try to quit mutt, it immediately reopens because the first
half of the condition above evaluates as false.  I tried to work around
this by changing the alias to a function definition as follows:

function rmutt () {
   TRAPEXIT () {
      case $? in
         0|23) : ;;
         *) /usr/local/bin/mutt -f '=inbox' ;;
      esac
   }
   /usr/local/bin/mutt -Z
}

	.  Now, whenever I run the rmutt function, there is a long
pause before it executes; once it is done running (that is, once I exit
mutt), any attempt to use command substitution in a function causes the
subshell to dump core, as follows:

(astaroth)/tmp/zsh_is_wonky: ll
total 0
(astaroth)/tmp/zsh_is_wonky: function wonky () { echo hello }
(astaroth)/tmp/zsh_is_wonky: wonky
hello
(astaroth)/tmp/zsh_is_wonky: ll
total 0
(astaroth)/tmp/zsh_is_wonky: function wonky () { echo `echo hello` }
hello
(astaroth)/tmp/zsh_is_wonky: ll
total 0
(astaroth)/tmp/zsh_is_wonky: rmutt
<...quit out of mutt...>
(astaroth)/tmp/zsh_is_wonky: ll
total 0
(astaroth)/tmp/zsh_is_wonky: function wonky () { echo hello }
(astaroth)/tmp/zsh_is_wonky: wonky
hello
(astaroth)/tmp/zsh_is_wonky: ll
total 0
(astaroth)/tmp/zsh_is_wonky: function wonky () { echo `echo hello` }
(astaroth)/tmp/zsh_is_wonky: wonky

(astaroth)/tmp/zsh_is_wonky: ll
total 2464
-rw-------   1 sweth    staff    1259376 Sep  7 17:20 core
(astaroth)/tmp/zsh_is_wonky:

	.  It doesn't matter if I use `...` or $(...) notation,
and I don't get this behaviour for command substitution that I enter
directly on the command-line.  Also of note, I can run the rmutt
function as many times as desired, and nothing directly untoward
happens; if I run rmutt, run a function that uses command substitution
(and thus dumps core as above), and then run rmutt again, however, my
main zsh process dumps core and I get booted off the system.  Finally,
sometimes (I haven't been able to figure out under what circumstances
exactly), rather than dumping core, the process in question will return
"fatal error: out of memory"; when I get that behaviour it persists
until the end of that shell, but is otherwise identical to the coredump
behaviour.  This is using:

(astaroth)/tmp/zsh_is_wonky: echo $ZSH_VERSION
3.1.9

	; I know that 4.0.2 is out now, but I just haven't had a
chance to install it yet.  Also, I finally realized that I
was making my problem much harder than I needed to, because I could get
rid of the TRAPEXIT and just do:

rmutt () {
   /usr/local/bin/mutt -Z;
   case $? in
      0|23) : ;;
      *) /usr/local/bin/mutt -f '=action' ;;
   esac
}

	; I had originally tried trying to trap the SIGTSTP, which
is why I became fixated on using a trap, but obviously that's not the
best solution by a long shot.  I'd still like to know how TRAPEXIT is
really supposed to work, though, and what is causing this odd behaviour.

	-- Sweth.

-- 
Sweth Chandramouli ; <svc@sweth.net>
President, Idiopathic Systems Consulting

[-- Attachment #2: Type: application/pgp-signature, Size: 236 bytes --]

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

* Re: zsh dumping core because I don't grok TRAPEXIT
  2001-09-07 22:04 zsh dumping core because I don't grok TRAPEXIT Sweth Chandramouli
@ 2001-09-08  5:50 ` Bart Schaefer
  2001-09-08  6:13   ` Sweth Chandramouli
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2001-09-08  5:50 UTC (permalink / raw)
  To: Sweth Chandramouli, ZSH Users

On Sep 7,  6:04pm, Sweth Chandramouli wrote:
}
} (astaroth)/tmp/zsh_is_wonky: echo $ZSH_VERSION
} 3.1.9
} 
} 	; I know that 4.0.2 is out now, but I just haven't had a
} chance to install it yet.

You need to install 4.0.2, or at least 3.1.9-dev-2+, because there were
several bug fixes in memory management of the exit trap handler shortly
after the 3.1.9 release.  This is almost certainly what is causing your
core dump.

} I'd still like to know how TRAPEXIT is really supposed to work

I'd like to know why mutt is exiting with 23.  It doesn't when I try it.

Anyway, TRAPEXIT is supposed to work pretty much like you thought it was.
It's the only trap-function that -always- behaves as if LOCAL_TRAPS is
set (which is how it got memory-management problems in the first place).
That is, the exit trap applies to all functions and subshells called
from the function that defines it, but it becomes undefined after it
is called when the defining function exits.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


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

* Re: zsh dumping core because I don't grok TRAPEXIT
  2001-09-08  5:50 ` Bart Schaefer
@ 2001-09-08  6:13   ` Sweth Chandramouli
  2001-09-08  7:12     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Sweth Chandramouli @ 2001-09-08  6:13 UTC (permalink / raw)
  To: ZSH Users

[-- Attachment #1: Type: text/plain, Size: 1479 bytes --]

On Sat, Sep 08, 2001 at 05:50:06AM +0000, Bart Schaefer wrote:
> } 	; I know that 4.0.2 is out now, but I just haven't had a
> } chance to install it yet.
> 
> You need to install 4.0.2, or at least 3.1.9-dev-2+, because there were
> several bug fixes in memory management of the exit trap handler shortly
> after the 3.1.9 release.  This is almost certainly what is causing your
> core dump.
	Ah.  It figures.  OK, installing 4.0.2 has been added to
the todo list for this week.

> } I'd still like to know how TRAPEXIT is really supposed to work
> 
> I'd like to know why mutt is exiting with 23.  It doesn't when I try it.
	I'll be sending a mail to mutt-users next to discuss 
that particular mystery.  :)

> Anyway, TRAPEXIT is supposed to work pretty much like you thought it was.
> It's the only trap-function that -always- behaves as if LOCAL_TRAPS is
> set (which is how it got memory-management problems in the first place).
> That is, the exit trap applies to all functions and subshells called
> from the function that defines it, but it becomes undefined after it
> is called when the defining function exits.
	OK.  The more I think about it, though, the less I see
when TRAPEXIT would really be useful.  How would

function TRAPEXIT {
   do_something
}
do_other_thing

	ever be different than

do_other_thing ; do_something

	?

	-- Sweth.

-- 
Sweth Chandramouli ; <svc@sweth.net>
President, Idiopathic Systems Consulting

[-- Attachment #2: Type: application/pgp-signature, Size: 236 bytes --]

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

* Re: zsh dumping core because I don't grok TRAPEXIT
  2001-09-08  6:13   ` Sweth Chandramouli
@ 2001-09-08  7:12     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2001-09-08  7:12 UTC (permalink / raw)
  To: Sweth Chandramouli, ZSH Users

On Sep 8,  2:13am, Sweth Chandramouli wrote:
}
} 	OK.  The more I think about it, though, the less I see
} when TRAPEXIT would really be useful.  How would
} 
} function TRAPEXIT {
}    do_something
} }
} do_other_thing
} 
} 	ever be different than
} 
} do_other_thing ; do_something

When do_other_thing is a shell function, and do_something wants to use
the local variables of that function.

Also, it's an encapsulation issue: inside do_other_thing may be the best
place to control which do_something to do.

Finally, and a point I forgot to mention:  TRAPEXIT preserves $?, so
it's actually like writing

	do_other_thing; x=$? ; do_something ; return $x

For example:

	getpass() {
	    TRAPEXIT() { stty echo }
	    stty -echo
	    read passwd || return 1
	    print
	}

Without the trap, you'd at the least have to write something like

	getpass() {
	    stty -echo
	    read passwd || { stty echo ; return 1 }
	    print
	    stty echo
	}

Note that you can also do

	TRAPINT TRAPQUIT TRAPEXIT () { stty echo }

to create three traps at once, although unless you `setopt localtraps'
only the TRAPEXIT will automatically remove itself.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


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

end of thread, other threads:[~2001-09-08  7:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-07 22:04 zsh dumping core because I don't grok TRAPEXIT Sweth Chandramouli
2001-09-08  5:50 ` Bart Schaefer
2001-09-08  6:13   ` Sweth Chandramouli
2001-09-08  7:12     ` 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).