Skip to content

Commit

Permalink
[spec/builtin-trap-bash] Make note of pipeline inconsistency
Browse files Browse the repository at this point in the history
In bash, the first part gets a DEBUG trap:

    echo hi | cat

But here it doesn't:

    { echo 1; echo 2; } | cat

I believe both cases must fork a shell interpreter, so they should be
consistent, as they are in OSH.

I tested with:

    $ strace -ff -e fork,execve -- bash -c 'echo hi | cat'

and both cases do fork a subinterpreter.
  • Loading branch information
Andy C committed Mar 8, 2024
1 parent 7cd2f9e commit 153f02d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
2 changes: 2 additions & 0 deletions core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,8 @@ def PopCall(self, should_pop_argv_stack):
def ShouldRunDebugTrap(self):
# type: () -> bool

# TODO: RunLastPart of pipeline can disable this

# Don't recursively run DEBUG trap
if self.running_debug_trap:
return False
Expand Down
39 changes: 33 additions & 6 deletions spec/builtin-trap-bash.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -243,26 +243,55 @@ debuglog() {
}
trap 'debuglog $LINENO' DEBUG

echo "result =" $(echo command sub)
( echo subshell )
echo "result = $(echo command sub; echo two)"
( echo subshell
echo two
)
echo done

## STDOUT:
[8]
result = command sub
two
subshell
[10]
two
[12]
done
## END

#### trap DEBUG run in forked shell interpreter for first part of pipeline?
#### trap DEBUG not run in forked interpreter for first pipeline part

debuglog() {
#echo " PID=$$ BASHPID=$BASHPID LINENO=$1"
echo " LINENO=$1"
}
trap 'debuglog $LINENO' DEBUG

{ echo pipe1;
echo pipe2; } \
| cat
echo ok

## STDOUT:
LINENO=8
pipe1
pipe2
LINENO=9
ok
## END

#### One 'echo' in first pipeline part - why does bash behave differently from case above?

# TODO: bash runs the trap 3 times, and osh only twice. I don't see why. Is
# it because Process::Run() does trap_state.ClearForSubProgram()? Probably
#echo top PID=$$ BASHPID=$BASHPID
#shopt -s lastpipe

debuglog() {
#echo " PID=$$ BASHPID=$BASHPID LINENO=$1"
#echo " LINENO=$1 $BASH_COMMAND"
# LINENO=6 echo pipeline
# LINENO=7 cat
echo " LINENO=$1"
}
trap 'debuglog $LINENO' DEBUG
Expand All @@ -278,15 +307,13 @@ pipeline
LINENO=8
ok
## END

## OK osh STDOUT:
LINENO=7
pipeline
LINENO=8
ok
## END


#### trap DEBUG and pipeline (lastpipe difference)
debuglog() {
echo " [$@]"
Expand Down

0 comments on commit 153f02d

Please sign in to comment.