Memory leak in ksh when using typeset in a function called in a loop in Red Hat Enterprise Linux

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux (RHEL) 7.0 Beta
    • ksh-20120801-11.el7.x86_64
  • Red Hat Enterprise Linux (RHEL) 6.5
    • ksh-20120801-10.el6.x86_64
  • Red Hat Enterprise Linux (RHEL) 6.5
    • ksh-20120801-10.el6.x86_64
  • Red Hat Enterprise Linux (RHEL) 6.4
    • 20100621-19.el6_4.4

Issue

  • The ksh shell has a memory leak when using typeset in a function called in a loop.

Resolution

A fix has been released with the following errata: RHBA-2014-0107

Diagnostic Steps

  • Add the following code block to a file so that it can be executed.
[root@rhel7 tmp]# cat mykshtest.sh

#!/bin/ksh
echo "PID = $$"
function myFunction
{
    typeset toPrint="something"
    echo "{toPrint}" 
}
while [[ 1 ]]
do
    state=$(myFunction)
done
  • Execute the script, monitoring the memory usage of the PID using the 'pmap' command. Notice that all columns for memory sizes (Kbytes, RSS, Dirty) continue to grow.
[root@rhel7 tmp]# chmod u+x mykshtest.sh
[root@rhel7 tmp]# ./mykshtest.sh &
[1] 2554

[root@rhel7 ~]# pmap -x 2554 | grep -e total -e Kbytes
Address           Kbytes     RSS   Dirty Mode  Mapping
total kB          159788   44732   43336

[root@rhel7 ~]# pmap -x 2554 | grep -e total -e Kbytes
Address           Kbytes     RSS   Dirty Mode  Mapping
total kB          164684   49608   48212

[root@rhel7 ~]# pmap -x 2554 | grep -e total -e Kbytes
Address           Kbytes     RSS   Dirty Mode  Mapping
total kB          168492   53416   52020

[root@rhel7 ~]# pmap -x 2554 | grep -e total -e Kbytes
Address           Kbytes     RSS   Dirty Mode  Mapping
total kB          175020   59960   58564
valgrind output

==30319== 512 bytes in 8 blocks are definitely lost in loss record 127 of 152
==30319==    at 0x4A0577B: calloc (vg_replace_malloc.c:593)
==30319==    by 0x408EC0: newnode (nvdisc.c:832)
==30319==    by 0x409060: nv_search (nvdisc.c:1077)
==30319==    by 0x43E5F0: nv_create (name.c:876)
==30319==    by 0x43C9E2: nv_open (name.c:1433)
==30319==    by 0x4403B8: nv_setlist (name.c:621)
==30319==    by 0x459B93: sh_exec (xec.c:1225)
==30319==    by 0x4593BA: sh_exec (xec.c:2240)
==30319==    by 0x45E59E: sh_funscope (xec.c:3469)
==30319==    by 0x45E9C7: sh_funct (xec.c:3552)
==30319==    by 0x45A96F: sh_exec (xec.c:1594)
==30319==    by 0x45420D: sh_subshell (subshell.c:598)

  • Component
  • ksh

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.