Bash file descriptor leak

Posted by Charles Duffy on Stack Overflow See other posts from Stack Overflow or by Charles Duffy
Published on 2010-04-15T21:51:05Z Indexed on 2010/04/15 21:53 UTC
Read the original article Hit count: 131

Filed under:

I get a file descriptor leak when running the following code:

function get_fd_count() {
        local fds
        cd /proc/$$/fd; fds=( * ) # avoid a StackOverflow source colorizer bug
        echo "${#fds[@]}"
}

function fd_leak_func() {
        echo ">> Current FDs: $(get_fd_count)"
        read retval new_state < <(set +e; new_state=$(echo foo); retval=$?; printf "%d %s\n" $retval $new_state)
}

function parent_func() {
        while fd_leak_func; do :; done
}

parent_func

Tested on both 3.2.25 and 4.0.28.

Taking the while loop out of parent_func and running it at top level makes the problem go away.

What's going on here? More to the point, are workarounds available?

© Stack Overflow or respective owner

Related posts about bash