diff mbox series

eval: make traps work when "set -e" is enabled

Message ID 20181016120952.25184-1-ao2@ao2.it (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series eval: make traps work when "set -e" is enabled | expand

Commit Message

Antonio Ospite Oct. 16, 2018, 12:09 p.m. UTC
When "set -e" is enabled traps are not always executed, in particular
the EXIT trap is not executed when the shell exits on an unhandled
error.

Consider the following test script:

  #!/bin/dash

  set -e

  trap 'ret=$?; echo "EXIT: $ret"' EXIT
  trap 'exit 2' HUP INT QUIT PIPE TERM

  read variable

By pressing Ctrl-C one would expect the EXIT trap to be called, as it is
the case with other shells (bash, zsh), but dash does not do it.

By calling dotrap() before jumping to the exit path when checkexit is
not zero, dash behaves like other shells.

Signed-off-by: Antonio Ospite <ao2@ao2.it>
---

Hi,

this has been reported in Debian[1] and I noticed the issue myself too, so
I tried to take a look at it.

I am marking the patch as RFC because I don't know the dash codebase very
well, and I might not be aware of possible drawbacks of this change. It worked
in my limited testing but that's it.

I don't know if the behavior of traps is specified when "set -e" is active,
but in case it isn't it would stll be good to behave like other shells.

Any comment is welcome.

Thank you,
   Antonio


[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=779416


 src/eval.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Antonio Ospite Oct. 16, 2018, 1:34 p.m. UTC | #1
On Tue, 16 Oct 2018 14:09:52 +0200
Antonio Ospite <ao2@ao2.it> wrote:

[...]
> I am marking the patch as RFC because I don't know the dash codebase very
> well, and I might not be aware of possible drawbacks of this change. It worked
> in my limited testing but that's it.
> 

I forgot the '--rfc' option when calling git-format-patch, the subject
meant to say "[RFC PATCH] ".

Ciao,
   Antonio
Herbert Xu Dec. 14, 2018, 5:53 a.m. UTC | #2
Antonio Ospite <ao2@ao2.it> wrote:
> When "set -e" is enabled traps are not always executed, in particular
> the EXIT trap is not executed when the shell exits on an unhandled
> error.
> 
> Consider the following test script:
> 
>  #!/bin/dash
> 
>  set -e
> 
>  trap 'ret=$?; echo "EXIT: $ret"' EXIT
>  trap 'exit 2' HUP INT QUIT PIPE TERM
> 
>  read variable
> 
> By pressing Ctrl-C one would expect the EXIT trap to be called, as it is
> the case with other shells (bash, zsh), but dash does not do it.
> 
> By calling dotrap() before jumping to the exit path when checkexit is
> not zero, dash behaves like other shells.
> 
> Signed-off-by: Antonio Ospite <ao2@ao2.it>
> ---
> 
> Hi,
> 
> this has been reported in Debian[1] and I noticed the issue myself too, so
> I tried to take a look at it.
> 
> I am marking the patch as RFC because I don't know the dash codebase very
> well, and I might not be aware of possible drawbacks of this change. It worked
> in my limited testing but that's it.
> 
> I don't know if the behavior of traps is specified when "set -e" is active,
> but in case it isn't it would stll be good to behave like other shells.
> 
> Any comment is welcome.
> 
> Thank you,
>   Antonio
> 
> 
> [1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=779416
> 
> 
> src/eval.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)

Patch applied.  Thanks.
diff mbox series

Patch

diff --git a/src/eval.c b/src/eval.c
index 6185db4..7d348d0 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -307,11 +307,11 @@  setstatus:
 		break;
 	}
 out:
+	dotrap();
+
 	if (checkexit & status)
 		goto exexit;
 
-	dotrap();
-
 	if (flags & EV_EXIT) {
 exexit:
 		exraise(EXEXIT);