patchThe GNU Bourne-Again SHell - Patches: patch #10341, waiting on background subshells is...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

patch #10341: waiting on background subshells is possible only with a side effect of a pipeline

Submitter:  Ilguiz Latypov <ilgiz>
Submitted:  Wed 10 May 2023 08:20:22 PM UTC
   
 
Category:  None Priority:  5 - Normal
Status:  Invalid Privacy:  Public
Assigned to:  None Open/Closed:  Open

Fri 12 May 2023 03:18:02 PM UTC, comment #2: 

You cannot use `wait' (or any of the POSIX process management APIs that underly it) to wait for and reap children of another process.

This means, for instance, that you can't wait for grandchildren. Only the process that forked the child and is its parent can wait for it or be notified when it exits.

Chet Ramey <chet>
Group administrator
Thu 11 May 2023 06:03:54 PM UTC, comment #1: 

Using "wait" inside the subshell that spawns background subshells works.


$ cat bash-subshell-wait.sh
#! /bin/bash
# https://savannah.gnu.org/patch/index.php?10341

((numrepeats=1))
stderrf="/tmp/bash-redir-runs.txt"
> "${stderrf}"

echo "Runs:"
(
    for ((it=0;it<numrepeats;it++)); do
        (
            sleep 1
            echo "Done with ${it}: ${RANDOM}."
        ) &
    done
    # A "wait" issued inside the outer subshell works.
    wait
# "The shell waits for all commands in the pipeline to terminate before returning a value."
# ) | cat > "${stderrf}"
) > "${stderrf}"
# The wait built-in appears oblivious to jobs spawned by a sub-shell.
wait
wc -l < "${stderrf}"



$ ./bash-subshell-wait.sh
Runs:
1


Ilguiz Latypov <ilgiz>
Wed 10 May 2023 08:20:22 PM UTC, original submission:  

Changing the redirect of a subshell to a pipeline with a "cat" has a useful side effect of waiting on background subshells.

Using the "wait" builtin misses the background subshells spawned from a subshell.

I wonder if waiting on truly all subshells deserves its own option of the builtin "wait" instead of relying on the side effect of the pipeline?  Let me know if I should have sent it to help-bash instead.

$ cat bash-redir-bug.sh
#! /bin/bash

((numrepeats=1))
stderrf="/tmp/bash-redir-runs.txt"

> "${stderrf}"


echo "Runs:"
(
    for ((it=0;it<numrepeats;it++)); do
        (
            sleep 1
            echo "Done with ${it}: ${RANDOM}."
        ) &
    done
# "The shell waits for all commands in the pipeline to terminate before returning a value."
# ) | cat > "${stderrf}"
) > "${stderrf}"
# The wait built-in appears oblivious to jobs spawned by a sub-shell.
wait
wc -l < "${stderrf}"


$ ./bash-redir-bug.sh
Runs:
0

Ilguiz Latypov <ilgiz>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by ilgiz (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-05-12 chet StatusNone Invalid

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code