Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15615

bash loses control of jobs inside a command substitution

Path csiph.com!weretis.net!feeder6.news.weretis.net!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Luiz Angelo Daros de Luca <luizluca@gmail.com>
Newsgroups gnu.bash.bug
Subject bash loses control of jobs inside a command substitution
Date Tue, 19 Nov 2019 16:51:12 -0300
Lines 88
Approved bug-bash@gnu.org
Message-ID <mailman.2019.1574193089.13325.bug-bash@gnu.org> (permalink)
References <CAJq09z4OWeZchYVH9U1G2OuTzdXBh+F8gLEjkqcJemohzi8dDg@mail.gmail.com>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
X-Trace usenet.stanford.edu 1574193089 7679 209.51.188.17 (19 Nov 2019 19:51:29 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=Z5nBeAq7VDYLGkIczzbCxzjrX4JqAZA96aD5Lbr8TLQ=; b=ueFWJdhRcIAMqk4/OphP1jdI+mi+REuF6Fv0xM3cKF0ElcKhnVxxKtuMWeMgxRhepq GKLHWdyISqxXKfF2ZklNYJUgTxkj4PrtjGp6syUiwVjdtTYgX1n+JejRMTz5EG0z/ksy ltxa2i+q+DC6S69VdvKVmrXJaeX6SMMsMg+eNnERUWMVtUPHMH0Nhv9uTKjj22QoezwL d3250AZBoQML7lGxAM30t+8WFjCW0lMqCrSy7sww+f+fBsA3buoK3r80HSjFI1WxAG5V b8TSrtLk5F1t1emlp63WQAkHsuigMXuyZ10feFx9oakq8snjumhnlthwuNStxjbVtrH9 7uCQ==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=Z5nBeAq7VDYLGkIczzbCxzjrX4JqAZA96aD5Lbr8TLQ=; b=eq1YUnljCgGysE8XXLgJzw80Wxyl/VzNeT2292w8HWN9wzUmTEKTfuYQ3zb38D0LqK 52BbVBvaoijmhuAIhMpTsfvI6ziTVZgnW+w1R9WbpNlD6SaKdUXsx6tG/+5VjxkiW0pH vnJQ7UppNhNLvQlevH+y++unZCUyUnXlTU9WDQqfWsbQXKRWOo9f1U+VgEYyt6M5UiFV /mp/73hBOnmLBYEErldvjy0Vl0PAaiGpM7H38rQnk/uHBVm1XZe+6DJLLwTpbG1FysdY 9T8gObZVvWXrinnd/9fX4AshgGqjhfLsk3seN1wctVXOjuPpQlFWutHcFng2nyn7oovH nKCQ==
X-Gm-Message-State APjAAAVrg3qhmU/218poFXqek/3ceej1+c1ass7vdEkR/CjYYorxn0pD rHBdrHndg9AZePVkm/QHw8WLYS3mw2DdbMaw7EabD3FwoQw=
X-Google-Smtp-Source APXvYqyFyu4UgCZMsTql/QV+HzGlToOPOuApCOXfPKPOenQyLzdY3eoTOsml7oZCnCDI0a8pAbSry0hwsM1P8WsFXZE=
X-Received by 2002:a37:d8e:: with SMTP id 136mr17859831qkn.249.1574193083509; Tue, 19 Nov 2019 11:51:23 -0800 (PST)
X-detected-operating-system by eggs.gnu.org: Genre and OS details not recognized.
X-Received-From 2607:f8b0:4864:20::72c
X-Content-Filtered-By Mailman/MimeDel 2.1.23
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAJq09z4OWeZchYVH9U1G2OuTzdXBh+F8gLEjkqcJemohzi8dDg@mail.gmail.com>
Xref csiph.com gnu.bash.bug:15615

Show key headers only | View raw


Hello,

Normally 'wait -n' will return the exit code of background process when
they terminate before wait is runned. However, when bg processes and 'wait
-n' runs inside a command substitution, bash loses control of bg process as
soon as they exit.

nr=${1:-5}
subjobs() {
        local -a pids
        for a in $(seq $nr); do
                (
                        sleep 0.$a
                        exit $a
                ) &
                pids+=($!)
        done
        sleep $1
        for a in $(seq $nr); do
                wait -n
                echo Got one child $? >&2
        done
}

# run wait -n before jobs have fininshed
echo Command substs no sleep
a=$(subjobs 0 )
echo Command substs pipe no sleep
b=$(subjobs 0 | cat)
echo direct no sleep
subjobs 0
# run wait -n after jobs have fininshed
echo Command substs sleep
a=$(subjobs 1)
echo Command substs pipe sleep
b=$(subjobs 1 | cat)
echo direct sleep
subjobs 1

Output:

Command substs no sleep
Got one child 1
Got one child 2
Got one child 3
Got one child 4
Got one child 5
Command substs pipe no sleep
Got one child 1
Got one child 2
Got one child 3
Got one child 4
Got one child 5
direct no sleep
Got one child 1
Got one child 2
Got one child 3
Got one child 4
Got one child 5
Command substs sleep
Got one child 5
Got one child 127
Got one child 127
Got one child 127
Got one child 127
Command substs pipe sleep
Got one child 127
Got one child 127
Got one child 127
Got one child 127
Got one child 127
direct sleep
Got one child 1
Got one child 2
Got one child 3
Got one child 4
Got one child 5

And two related features requests: 1) It seems that pids args are ignored
by wait when '-n' is specified. However, it would be a nice add_on to use
the list of pids as a filter. 2) 'wait -n' lacks a way to get dead child
PID, requiring some race conditions techniques to get it.

Regards,

-- 
     Luiz Angelo Daros de Luca
            luizluca@gmail.com

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

bash loses control of jobs inside a command substitution Luiz Angelo Daros de Luca <luizluca@gmail.com> - 2019-11-19 16:51 -0300

csiph-web