bug-bash
[Top][All Lists]
Advanced

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

Trap not being run for all CHLD signals, 4.3


From: crispusfairbairn
Subject: Trap not being run for all CHLD signals, 4.3
Date: Wed, 3 Sep 2014 07:08:24 -0700 (PDT)
User-agent: G2/1.0

$ cat parallel-test.bash
function process_job {
    sleep 1
}

function main {
    typeset -i index=0 cur_jobs=0 max_jobs=6
    trap '((cur_jobs--))' CHLD
    set -m

    while ((index++ < 30)); do
        echo -n "index: $index, cur_jobs: $cur_jobs"
        set +m
        childs=$(pgrep -P $$ | wc -w)
        (( childs < cur_jobs )) && echo -n ", actual childs: $childs"
        echo
        set -m
        process_job &
        ((++cur_jobs >= max_jobs)) && POSIXLY_CORRECT= wait;
    done

    echo 'finished, waiting for remaining jobs...'
    wait
}

main
echo "done"

This works on:
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

But on:
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
and
GNU bash, version 4.3.24(1)-release (x86_64-unknown-linux-gnu)

it will around "index: 9" start missing traps (not decrementing cur_jobs):

$ bash-4.3.24/bin/bash parallel-test.bash
index: 1, cur_jobs: 0
index: 2, cur_jobs: 1
index: 3, cur_jobs: 2
index: 4, cur_jobs: 3
index: 5, cur_jobs: 4
index: 6, cur_jobs: 5
index: 7, cur_jobs: 5
index: 8, cur_jobs: 5
index: 9, cur_jobs: 5, actual childs: 4
index: 10, cur_jobs: 5, actual childs: 3
index: 11, cur_jobs: 5, actual childs: 3
...


If the sleep is changed to be random, it might work correctly for the whole 30 
iterations, which points to a race condition somewhere?

- Cris


reply via email to

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