[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bug that only happens in ubuntu 22.04 even with older bash version
From: |
Chet Ramey |
Subject: |
Re: Bug that only happens in ubuntu 22.04 even with older bash version |
Date: |
Mon, 22 Aug 2022 15:09:58 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.1.2 |
On 8/20/22 11:49 AM, Robert E. Griffith wrote:
Once I understood the full impact of the 5.1 recursive DEBUG trap change, I
resolved most of my issues but I have one more that seems like a bug
introduced in 5.1. When stepping through a non-DEBUG trap with the DEBUG
trap, BASH_COMMAND does not update until the trap is finished.
#!/usr/bin/env bash
echo $BASH_VERSION
shopt -s extdebug
function setExitCode() { return $1; }
function aNoopCommand() { echo "aNoopCommand 1"; }
trap 'echo USR1: start; aNoopCommand' USR1
trap 'echo "DBG: $BASH_COMMAND"
[[ "$BASH_COMMAND" =~ ^aNoopCommand ]] && echo "!!! Hit it"
setExitCode 0
' DEBUG
kill -USR1 $BASHPID
echo 1
[bobg@bgcore-centosS9]$ bin/test51_2.sh
5.1.8(1)-release
DBG: kill -USR1 $BASHPID
DBG: kill -USR1 $BASHPID
USR1: start
DBG: kill -USR1 $BASHPID
DBG: kill -USR1 $BASHPID
DBG: kill -USR1 $BASHPID
aNoopCommand 1
DBG: echo 1
1
The first thing you have to note is that BASH_COMMAND doesn't update while
the shell is running a trap. This is intentional, and there was a bug I
fixed back in late 2018 that made sure the [[ conditional command didn't
reset BASH_COMMAND if it was executed in a trap action. The rest you can
figure out by running `set -x' (this is mostly for other readers, I know
you've already traced this out).
1. The trap command runs to set the DEBUG trap.
2. The DEBUG trap runs before the `kill'
3. The kill runs, the signal arrives, the shell function run as the result
of SIGUSR1 inherits the DEBUG trap, the DEBUG trap runs. You're running
the USR1 trap, so the last command you've run before the trap action is
the kill.
4. The echo USR1 ... runs.
5. The DEBUG trap runs before the call to aNoopCommand.
6. You run aNoopCommand, which inherits the DEBUG trap.
7. The DEBUG trap runs.
8. The echo runs, and aNoopCommand returns. The USR1 trap completes.
9. BASH_COMMAND gets set to `echo 1'.
10. The DEBUG trap runs.
11. The echo runs, and the script ends.
So it's probably the result of the fix to `[[' and a fix from early 2020 to
allow traps for signals the shell handles specially to execute while
another trap handler is running.
Chet
--
``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/