zsh-workers
 help / color / mirror / Atom feed
* [BUG] ignored trap and subshell
@ 2021-08-24 16:23 Vincent Lefevre
  2021-08-24 17:29 ` Bart Schaefer
  2021-08-25 10:10 ` Peter Stephenson
  0 siblings, 2 replies; 6+ messages in thread
From: Vincent Lefevre @ 2021-08-24 16:23 UTC (permalink / raw)
  To: zsh-workers

The zshmisc(1) man page says:

    ( list )
        Execute list in a subshell.  Traps set by the trap builtin are
        reset to their default values while executing list.

However, with "emulate sh", ignored traps should still be ignored
in the subshell according to POSIX, and zsh 5.8 fails do conform:

$ zsh -c 'emulate sh; trap "" INT; trap; echo A; ( trap; echo B; sleep 3; ); echo $?'
trap -- '' INT
A
B
^C130

where Ctrl-C immediately interrupts the sleep.

Compare to sh:

$ sh -c 'trap "" INT; trap; echo A; ( trap; echo B; sleep 3; ); echo $?'
trap -- '' INT
A
trap -- '' INT
B
^C0

where the sleep isn't interrupted by Ctrl-C.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


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

* Re: [BUG] ignored trap and subshell
  2021-08-24 16:23 [BUG] ignored trap and subshell Vincent Lefevre
@ 2021-08-24 17:29 ` Bart Schaefer
  2021-08-24 17:33   ` Bart Schaefer
  2021-08-25 10:10 ` Peter Stephenson
  1 sibling, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2021-08-24 17:29 UTC (permalink / raw)
  To: Zsh hackers list

On Tue, Aug 24, 2021 at 9:24 AM Vincent Lefevre <vincent@vinc17.net> wrote:
>
> However, with "emulate sh", ignored traps should still be ignored
> in the subshell according to POSIX, and zsh 5.8 fails do conform:

"emulate sh" is NOT the same as "ARGV0=sh", and is never going to be.
We make no representation that "emulate sh" will enable all the same
standards behaviors as using "sh" as the command name.

That said, it might be possible to add control of this behavior to the
POSIX_TRAPS option, in which case it would be covered by "emulate sh".


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

* Re: [BUG] ignored trap and subshell
  2021-08-24 17:29 ` Bart Schaefer
@ 2021-08-24 17:33   ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2021-08-24 17:33 UTC (permalink / raw)
  To: Zsh hackers list

On Tue, Aug 24, 2021 at 10:29 AM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> On Tue, Aug 24, 2021 at 9:24 AM Vincent Lefevre <vincent@vinc17.net> wrote:
> >
> > However, with "emulate sh", ignored traps should still be ignored
> > in the subshell according to POSIX, and zsh 5.8 fails do conform:
>
> "emulate sh" is NOT the same as "ARGV0=sh", and is never going to be.

However, it appears this behavior isn't covered in regular sh emulation either:

% ARGV0=sh Src/zsh -c 'trap "" INT; trap; echo A; ( trap; echo B;
sleep 3; ); echo $?'
trap -- '' INT
A
B
^C130

So we should look into this.


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

* Re: [BUG] ignored trap and subshell
  2021-08-24 16:23 [BUG] ignored trap and subshell Vincent Lefevre
  2021-08-24 17:29 ` Bart Schaefer
@ 2021-08-25 10:10 ` Peter Stephenson
  2021-08-25 10:47   ` Peter Stephenson
  1 sibling, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2021-08-25 10:10 UTC (permalink / raw)
  To: zsh-workers

> On 24 August 2021 at 17:23 Vincent Lefevre <vincent@vinc17.net> wrote:
> The zshmisc(1) man page says:
> 
>     ( list )
>         Execute list in a subshell.  Traps set by the trap builtin are
>         reset to their default values while executing list.
> 
> However, with "emulate sh", ignored traps should still be ignored
> in the subshell according to POSIX, and zsh 5.8 fails do conform:

I think this is straightforward.  POSIXTRAPS is clearly the option for the job.

pws

diff --git a/Src/exec.c b/Src/exec.c
index 49ff88b80..79d8064b6 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -1033,7 +1033,8 @@ entersubsh(int flags, struct entersubsh_ret *retp)
 
     if (!(flags & ESUB_KEEPTRAP))
 	for (sig = 0; sig < SIGCOUNT; sig++)
-	    if (!(sigtrapped[sig] & ZSIG_FUNC))
+	    if (!(sigtrapped[sig] & ZSIG_FUNC) &&
+		!(isset(POSIXTRAPS) && (sigtrapped[sig] & ZSIG_IGNORED)))
 		unsettrap(sig);
     monitor = isset(MONITOR);
     job_control_ok = monitor && (flags & ESUB_JOB_CONTROL) && isset(POSIXJOBS);


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

* Re: [BUG] ignored trap and subshell
  2021-08-25 10:10 ` Peter Stephenson
@ 2021-08-25 10:47   ` Peter Stephenson
  2021-08-25 16:07     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2021-08-25 10:47 UTC (permalink / raw)
  To: zsh-workers


> On 25 August 2021 at 11:10 Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> > On 24 August 2021 at 17:23 Vincent Lefevre <vincent@vinc17.net> wrote:
> > The zshmisc(1) man page says:
> > 
> >     ( list )
> >         Execute list in a subshell.  Traps set by the trap builtin are
> >         reset to their default values while executing list.
> > 
> > However, with "emulate sh", ignored traps should still be ignored
> > in the subshell according to POSIX, and zsh 5.8 fails do conform:
> 
> I think this is straightforward.  POSIXTRAPS is clearly the option for the job.

And some documentation; the place Vincent mentioned looks like the obvious one
(we have at least one piece of evidence it gets read...)

diff --git a/Doc/Zsh/grammar.yo b/Doc/Zsh/grammar.yo
index 2eb2018d2..f8f4ada86 100644
--- a/Doc/Zsh/grammar.yo
+++ b/Doc/Zsh/grammar.yo
@@ -288,7 +288,9 @@ for each selection until a break or end-of-file is encountered.
 cindex(subshell)
 item(tt(LPAR()) var(list) tt(RPAR()))(
 Execute var(list) in a subshell.  Traps set by the tt(trap) builtin
-are reset to their default values while executing var(list).
+are reset to their default values while executing var(list); an
+exception is that ignored signals will continue to be ignored
+if the option tt(POSIXTRAPS) is set.
 )
 item(tt({) var(list) tt(}))(
 Execute var(list).
diff --git a/Src/exec.c b/Src/exec.c
index 49ff88b80..79d8064b6 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -1033,7 +1033,8 @@ entersubsh(int flags, struct entersubsh_ret *retp)
 
     if (!(flags & ESUB_KEEPTRAP))
 	for (sig = 0; sig < SIGCOUNT; sig++)
-	    if (!(sigtrapped[sig] & ZSIG_FUNC))
+	    if (!(sigtrapped[sig] & ZSIG_FUNC) &&
+		!(isset(POSIXTRAPS) && (sigtrapped[sig] & ZSIG_IGNORED)))
 		unsettrap(sig);
     monitor = isset(MONITOR);
     job_control_ok = monitor && (flags & ESUB_JOB_CONTROL) && isset(POSIXJOBS);


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

* Re: [BUG] ignored trap and subshell
  2021-08-25 10:47   ` Peter Stephenson
@ 2021-08-25 16:07     ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2021-08-25 16:07 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh hackers list

On Wed, Aug 25, 2021 at 3:48 AM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
>
> And some documentation; the place Vincent mentioned looks like the obvious one
> (we have at least one piece of evidence it gets read...)

A note under the description of POSIX_TRAPS itself would not be amiss.


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

end of thread, other threads:[~2021-08-25 16:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-24 16:23 [BUG] ignored trap and subshell Vincent Lefevre
2021-08-24 17:29 ` Bart Schaefer
2021-08-24 17:33   ` Bart Schaefer
2021-08-25 10:10 ` Peter Stephenson
2021-08-25 10:47   ` Peter Stephenson
2021-08-25 16:07     ` Bart Schaefer

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.vuxu.org/zsh-workers

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-workers zsh-workers/ https://inbox.vuxu.org/zsh-workers \
		zsh-workers@zsh.org
	public-inbox-index zsh-workers

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git