Bash leaks memory when doing function calls while reading from subshell as stdin

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Bash leaks memory when doing function calls while reading from subshell as stdin

Øyvind Hvidsten-2
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu'
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash'
-DSHELL -DHAVE_CONFIG_H   -I.  -I../. -I.././include -I.././lib
-Wdate-time -D_FORTIFY_SOURCE=2 -g -O2
-fdebug-prefix-map=/build/bash-7fckc0/bash-4.4=.
-fstack-protector-strong -Wformat -Werror=format-security -Wall -no-pie
-Wno-parentheses -Wno-format-security
uname output: Linux vampiric 4.9.0-4-amd64 #1 SMP Debian 4.9.51-1
(2017-09-28) x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.4
Patch Level: 12
Release Status: release

Description:
        Somehow the included script causes a memory leak. Memory usage will
grow huge over a very short timeframe.
        The script is pointless, but cut down from a larger one that does
something useful.
        Originally, the subshell was reading a while (cat ...), so the printf
loop has nothing to do with it.
        Replacing the call to f2() with an echo, or anything not a function
call, makes it not leak.

Repeat-By:
        f2() { return 1; }
        f1()
        {
            while read -r line; do
                f2 "test"
            done < <(
                for ((i=0;i<1000000000;i++)); do
                    printf '%s\n' "$i"
                done
            )
        }
       
        f1

Reply | Threaded
Open this post in threaded view
|

Re: Bash leaks memory when doing function calls while reading from subshell as stdin

Chet Ramey
On 12/7/17 7:41 AM, Øyvind Hvidsten wrote:

> Bash Version: 4.4
> Patch Level: 12
> Release Status: release
>
> Description:
>     Somehow the included script causes a memory leak. Memory usage will
> grow huge over a very short timeframe.

Thanks for the report. It's the return in f2 that causes the problem.
This will be fixed in the next devel branch push. If you want to play
around with the fix, I've attached a patch.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    [hidden email]    http://tiswww.cwru.edu/~chet/

saved-fifo-memleak.patch (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Bash leaks memory when doing function calls while reading from subshell as stdin

Øyvind Hvidsten-2
Awesome. Thanks!

On 08/12/17 15:57, Chet Ramey wrote:

> On 12/7/17 7:41 AM, Øyvind Hvidsten wrote:
>
>> Bash Version: 4.4
>> Patch Level: 12
>> Release Status: release
>>
>> Description:
>>      Somehow the included script causes a memory leak. Memory usage will
>> grow huge over a very short timeframe.
>
> Thanks for the report. It's the return in f2 that causes the problem.
> This will be fixed in the next devel branch push. If you want to play
> around with the fix, I've attached a patch.
>
> Chet
>