Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Background subshells may finalize session #25

Open
davefx opened this issue Jun 24, 2016 · 15 comments
Open

Background subshells may finalize session #25

davefx opened this issue Jun 24, 2016 · 15 comments

Comments

@davefx
Copy link

davefx commented Jun 24, 2016

After loading bash-preexec.sh, if I launch, for example:
$ ( ls ) &
most of the times, bash exits (not always).

@rcaloras
Copy link
Owner

@davefx thanks for reporting. Just recognizing this now as a result of some issues with bashhub that's using bash-preexec. Believe the changes made to support subshells in 0.3.0 cause this. You can use 0.2.3 if you don't mind losing subshell support.

@rcaloras
Copy link
Owner

@davefx Could you also post the version of bash you're using?

echo "$BASH_VERSION"

@rcaloras
Copy link
Owner

rcaloras commented Jun 24, 2016

Believe this is impacting bash versions > 4.2.46

@davefx
Copy link
Author

davefx commented Jun 24, 2016

I've reproduced this problem with bash 4.3.42(1) and 4.3.30(1).

As some other tips:
If I execute something after the background process, bash doesn't finish .
$ (ls)& echo Hello

@rcaloras
Copy link
Owner

Was able to boil this down to a simple code block to recreate. Still not sure the best way to handle this

# Causes login shells to logout
# Look like bash versions > 4.2.46
set -o functrace > /dev/null 2>&1
no_op() { :;}
trap 'no_op' DEBUG;
# Any command in a subshell and background
( pwd ) & 

@rcaloras
Copy link
Owner

@davefx was this occurring on a box you were ssh'd into? I can't recreate this locally regardless of the version. I can only recreate when ssh'd onto a box.

@davefx
Copy link
Author

davefx commented Jul 21, 2016

No. This happens also locally in X terminals in my Ubuntu 16.04 system with bash 4.3.46(1)-release.

rcaloras added a commit that referenced this issue Aug 9, 2016
- it can be enabled by setting __bp_enable_subshells
rcaloras added a commit that referenced this issue Aug 17, 2016
- it can be enabled by setting __bp_enable_subshells
rcaloras added a commit that referenced this issue Aug 22, 2016
Add option to disable subshells to help with #25
@rcaloras
Copy link
Owner

rcaloras commented Sep 23, 2016

Looks like this got mentioned to some folks on the GNU mailing list. Looking into this as a bug in bash. http://lists.gnu.org/archive/html/bug-bash/2016-09/msg00006.html

@adggit
Copy link

adggit commented Oct 1, 2016

When the DEBUG trap is set with functrace on, it causes other problems too. E.g., certain command groups piped to a pager program will get stopped (via SIGTTOU/SIGTTIN when the pager accesses the tty):

set -o functrace
trap ':' DEBUG

# The following gets stopped:
{ date; who | sort; } | more

[1]+  Stopped                 { date; who | sort; } | more

# But this works fine:
{ who | sort; date; } | more

In general (with functrace on and the DEBUG trap set) a group of command pipelines in the form below will get stopped:

    $ { cmd_a; cmd_b | cmd_c; cmd_d; } | pager_prog

    [1]+  Stopped                  { cmd_a; cmd_b | cmd_c; cmd_d; } | pager_prog

This happens if the first command of the group (cmd_a) is not a bash builtin AND a pipeline occurs later in the group (cmd_b | cmd_c).

(Puzzlingly, the issue does not arise if in the command group either the first command (cmd_a) is a bash builtin or if none of the later commands contains a pipe.)

@adggit
Copy link

adggit commented Oct 1, 2016

Just for reference, I originally reported this as bash bug [1] without realizing that I actually had functrace/DEBUG on, but when the bash maintainers could not reproduce it, I traced it to my preexec setup and eventually on to functrace/DEBUG [2].

[1] http://lists.gnu.org/archive/html/bug-bash/2016-09/msg00127.html
[2] http://lists.gnu.org/archive/html/bug-bash/2016-10/msg00000.html

@rcaloras
Copy link
Owner

rcaloras commented Oct 1, 2016

@adggit thanks for posting for reference! This issue is a real pain.

The good news is, preexec can now be implemented with $PS0 in bash 4.4. I'd love to add version detection to bash-preexec and install hooks to $PS0 if you're using the latest versions of Bash. Mentioned originally in #28 Would love a PR from someone on this.

@spacelama
Copy link

bash 5.2.15(1) (debian stable) - this particular problem doesn't appear to be a problem anymore? 5 minutes of testing hasn't popped anything up anyway. Time to reenable it by default for $BASH_VERSINFO[...] > ...?

But I'll put a caution regarding $PS0 against #28.

@akinomyoga
Copy link
Contributor

I now tried. The problem reproduces with 4.3 and 4.4, but it doesn't with 5.0, 5.1, 5.2, and the devel version.

@spacelama
Copy link

Not so fast! I have found a pretty reliable reproducer, but I haven't yet whittled it down to something simple (the shell function I'm using as a wrapper around sudo isn't reproducing when I just run the simple sudo it boils down to!

: 56903,1; sillysu echo hi there | less
...
hi there
lines 1-1/1 (END)
[1]+  Stopped                 sillysu echo hi there | less

vs

: 55989,1; sudo env echo hi there | less
hi there
#0-0-03:07:38, Thu Jan 02 tconnors@dirac:~ (bash)                                                                      

I've got to go to bed because it's 3am (goddamit 2300 nT auroral substorm guarantees 100% cloud coverage), but here's my sillysu() function if you want to see whether you can see a reproducer in there. Who knows whether it's pulling in something else in my environment that means you won't be able to trigger it:

sillysu is a function
sillysu () 
{ 
    if [ -n "$DISPLAY" ]; then
        command cp -a $XAUTHORITY /tmp/.$USER.Xauthority.tmp;
    fi;
    if [ "$#" -eq 0 ]; then
        ( unset BASH_PROFILE_EXECUTED PROFILE_EXECUTED BASHRC_EXECUTED http_proxy HTTP_PROXY ftp_proxy WWW_http_GATEWAY no_proxy;
        sudo env PUSER=$USER bash --login );
    else
        if [[ "$1" == -u ]]; then
            shift;
            ( unset BASH_PROFILE_EXECUTED PROFILE_EXECUTED BASHRC_EXECUTED http_proxy HTTP_PROXY ftp_proxy WWW_http_GATEWAY no_proxy;
            sudo "$@" );
        else
            if [[ "$1" == -* ]]; then
                ( unset BASH_PROFILE_EXECUTED PROFILE_EXECUTED BASHRC_EXECUTED http_proxy HTTP_PROXY ftp_proxy WWW_http_GATEWAY no_proxy;
                sudo "$@" );
            else
                ( unset BASH_PROFILE_EXECUTED PROFILE_EXECUTED BASHRC_EXECUTED http_proxy HTTP_PROXY ftp_proxy WWW_http_GATEWAY no_proxy;
                COMMAND=;
                while (( $# > 0 )); do
                    arg=$(printf "%q" "$1");
                    COMMAND="${COMMAND} ${arg}";
                    shift;
                done;
                sudo env PUSER=$USER bash --login -c "${COMMAND}" );
            fi;
        fi;
    fi
}

@spacelama
Copy link

spacelama commented Jan 5, 2025

Hah! Here I was getting sidetracked by sudo and it failing to reproduce in my remote testing sessions because $DISPLAY wasn't set, and then finally by all of my testing been done with the echo command which is a bash builtin except when being invoked through sudo.

Is this reproducing for others with bash 5.15 versions?

# fault:
( /bin/echo hi there ; ( /bin/echo ho there ) ; ) | less

[1]+  Stopped                 ( /bin/echo hi there; ( /bin/echo ho there ) ) | less
# no fault:
( /bin/echo hi there ; /bin/echo ho there ; ) | less
hi there
ho there
tconnors@test1:~$ 

No setup needed except:

__bp_enable_subshells=true
. ~/code/bash-preexec/bash-preexec.sh

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants