[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[5.1+] wait <id> may return wrong status from function with EXIT trap

From: Greg Edwards
Subject: [5.1+] wait <id> may return wrong status from function with EXIT trap
Date: Tue, 8 Feb 2022 10:58:09 -0700

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O3 -feliminate-unused-debug-types -pipe -Wall 
-Wp,-D_FORTIFY_SOURCE=2 -fexceptions -Wformat -Wformat-security -m64 
-fasynchronous-unwind-tables -Wp,-D_REENTRANT -ftree-loop-distribute-patterns 
-Wl,-z -Wl,now -Wl,-z -Wl,relro -fno-semantic-interposition -ffat-lto-objects 
-fno-trapping-math -Wl,-sort-common -Wl,--enable-new-dtags -mtune=skylake
uname output: Linux pneuma 5.16.4-1119.native #1 SMP Sat Jan 29 03:03:44 PST 
2022 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.1
Patch Level: !PATCHLEVEL!
Release Status: release


The following simplified reproducer demonstrates an incorrect status being
returned from the test() function when run with bash 5.1 or above.  The correct
status is returned on bash versions 5.0 and below, though it doesn't appear to
run the trap.

#!/bin/bash -x

function cleanup() {
    echo "do some clean up"

function test() {
    trap cleanup EXIT
    if false; then
        echo "do something"
        return 0

test &
wait $!
echo "test() exit status is $?"

Example when run on bash 5.1 or above:

$ ./t.sh
+ wait 66914
+ test
+ trap cleanup EXIT
+ false
+ return 0
++ cleanup
++ echo 'do some clean up'
do some clean up
+ echo 'test() exit status is 1'
test() exit status is 1

Some observations about the behavior:

  1) It only occurs if the function has an EXIT trap.  It returns the correct
     status if a RETURN trap is used.

  2) If the function returns with an 'exit 0' instead of a 'return 0', the
     correct status is returned.

  3) If the if/else is rearranged such that the 'return 0' comes from the true 
     case of the if/else, the correct status is returned.


See above.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]