Bash file descriptor leak
- by Charles Duffy
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?