[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Regression: EXIT traps always run in global context
From: |
Chet Ramey |
Subject: |
Re: Regression: EXIT traps always run in global context |
Date: |
Fri, 29 Nov 2024 17:58:17 -0500 |
User-agent: |
Mozilla Thunderbird |
On 11/27/24 11:23 PM, Ivan Shapovalov wrote:
Bash Version: 5.2
Patch Level: 37
Release Status: release
Description:
Regression: When an EXIT trap runs, it runs in global context, i.e.
does not see the locals defined in the function that was executing
at the time of the exit.
Repeat-By:
See the attached script.
$ INNER_FAIL_EXIT=1 ./test.sh # or interrupt with ^C during "inner sleep
2")
(observe 5 `inner-*`, `outer-*` and `main-*` messages printed upon exit)
$ INNER_FAIL_2=1 ./test.sh
(observe just one `main-*` message printed upon exit)
Expected behavior in both cases is the same. It was the same until a
recent bash release.
This was a longstanding bug in bash. Going back to bash-2.04 in 1999,
exiting the shell due to a command failing when `set -e' was enabled
(`ERREXIT') was supposed to execute the exit trap at the global context.
The idea was that there was an implicit return from the function, at which
point all function state gets unwound, then the shell exits, running the
exit trap before it goes. Bash has always made this different from the
exit builtin.
The problem was that the code in bash-2.04 changed some of the context
when the EXIT trap was executed due to a commmand failing while in a
function (so things like FUNCNAME weren't visible and running `local'
would fail), but didn't change enough (so the call stack continued to
see local variables that shadowed global variables).
The change in July 2022 was the result of
https://lists.gnu.org/archive/html/bug-bash/2022-07/msg00098.html
which noted that the local variables remained visible in the exit trap.
This change unwound the call stack on ERREXIT before running the exit
trap, which was the original intent.
POSIX standards before 2017 just said "the shell shall immediately exit."
Current versions say the shell "shall exit, as if by executing the exit
special built-in utility with no arguments" so it seems like this should
change in posix mode, at least, to make ERREXIT and the exit builtin
identical when `set -e' is enabled.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/
OpenPGP_signature.asc
Description: OpenPGP digital signature