Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When code put in a function I get: Too many open files

806 views
Skip to first unread message

Cecil Westerhof

unread,
Dec 3, 2011, 11:59:10 AM12/3/11
to
I have the following code:
#!/usr/bin/env bash

set -o errexit
set -o nounset

declare LOOP_STEPS=2000
declare -r REQUIRED_STRING="required time"

declare day
declare -i end
declare i
declare minutes
declare seconds
declare -i start
declare -i total

function doTests {
start=$(date +%s)
for i in $(seq ${LOOP_STEPS}) ; do
read minutes seconds day < <(date '+%M %S %d')
done
end=$(date +%s)
total=${end}-${start}
echo "${REQUIRED_STRING} values with read: ${total}"

echo
}

start=$(date +%s)
for i in $(seq ${LOOP_STEPS}) ; do
read minutes seconds day < <(date '+%M %S %d')
done
end=$(date +%s)
total=${end}-${start}
echo "${REQUIRED_STRING} values with read: ${total}"

doTests

When doTests is called (which has the same code as executed before the
call) I get:
required time values with read: 4
/home/cecil/bin/test2.sh: redirection error: cannot duplicate fd: Too many open files
/home/cecil/bin/test2.sh: cannot make pipe for process substitution: Too many open files
/home/cecil/bin/test2.sh: cannot make pipe for process substitution: Too many open files
/home/cecil/bin/test2.sh: line 20: <(date '+%M %S %d'): ambiguous redirect

So when the code is put at the top level it works okay, but when put
into a function there is an error. Why is this?

By the way, when I remove the code before the function call, it still
goes wrong, So it is not because the code is executed a second time.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

DennisW

unread,
Dec 3, 2011, 9:57:07 PM12/3/11
to
I see this error in Bash 4.0 and 4.1. In Bash 2.05b and 3.2, I get a
malloc error.

However, in Bash 4.2, it works as expected. Here is (at least part of)
the relevant information from the CHANGES file:

This document details the changes between this version, bash-4.2-rc1,
and the previous version, bash-4.2-beta.

1. Changes to Bash

a. Fixed a bug that caused some redirection errors to leak file
descriptors.

. . .

This document details the changes between this version, bash-4.2-
alpha,
and the previous version, bash-4.1-release.

. . .

t. Fixed a bug that left FIFOs opened by process substitutions open
long
enough to potentially cause file descriptor exhaustion when
running a
shell function or shell builtin.

. . .

This document details the changes between this version, bash-4.2-
alpha,
and the previous version, bash-4.1-release.


. . .

ww. Fixed a bug that caused failure to save file descriptors for
redirections
to corrupt shell file descriptors.

Cecil Westerhof

unread,
Dec 4, 2011, 1:32:09 PM12/4/11
to
Op zondag 4 dec 2011 03:57 CET schreef DennisW:

> I see this error in Bash 4.0 and 4.1. In Bash 2.05b and 3.2, I get a
> malloc error.
>
> However, in Bash 4.2, it works as expected. Here is (at least part of)
> the relevant information from the CHANGES file:
>
> This document details the changes between this version, bash-4.2-rc1,
> and the previous version, bash-4.2-beta.
>
> 1. Changes to Bash
>
> a. Fixed a bug that caused some redirection errors to leak file
> descriptors.

My version is still: 4.1.10(1)-release.

I already downloaded the latest version and will install it.

Cecil Westerhof

unread,
Dec 4, 2011, 2:59:06 PM12/4/11
to
Op zondag 4 dec 2011 19:32 CET schreef Cecil Westerhof:

>> I see this error in Bash 4.0 and 4.1. In Bash 2.05b and 3.2, I get a
>> malloc error.
>>
>> However, in Bash 4.2, it works as expected. Here is (at least part of)
>> the relevant information from the CHANGES file:
>>
>> This document details the changes between this version, bash-4.2-rc1,
>> and the previous version, bash-4.2-beta.
>>
>> 1. Changes to Bash
>>
>> a. Fixed a bug that caused some redirection errors to leak file
>> descriptors.
>
> My version is still: 4.1.10(1)-release.
>
> I already downloaded the latest version and will install it.

Problem solved. Thanks.

pjodrr

unread,
Aug 30, 2013, 3:17:34 AM8/30/13
to
Hello,

it seems that this bug is not fixed yet. Consider this script:

----------------8<----------------------------------------------
#! /usr/bin/env bash

function compare_files() {
local dir1="$1" dir2="$2"
cd "$dir1" || exit
if [ ! -d "$dir2" ]; then
echo usage error >&2
return 2
fi

find . -type f | while read file; do
if [ ! -f "$dir2/$file" ]; then
continue
fi
case "$file" in
*.gz)
if ! cmp -s <(gzip -dc "$dir1/$file") <(gzip -dc "$dir2/$file") > /dev/null; then
echo "files $file differ"
fi
;;
*)
:
esac
done
}

compare_files "$1" "$2"

----------------8<----------------------------------------------

if you call this as "compare-dirs.sh /usr/share/man /usr/share/man"
(use a directory that contains many gzip compressed files) you will
soon get this error:

----------------8<----------------------------------------------
cannot make pipe for process substitution: Too many open files
----------------8<----------------------------------------------

with latest bash version 4.2.45 Using bash version 4.0.0 or 3.0.16
(these are the versions that i have at hand) is no problem.

Regards,

Peter
0 new messages