ksh memory leak when using PATH
Environment
- Red Hat Enterprise Linux (RHEL) 6, 7
- ksh-20120801-33.el6
- ksh-20120801-26.el7
Issue
some script using ksh shows overtime increment of resident and consequently virtual memory.
The problem happen when PATH environment variable is used.
Here's example reproducer script:
#!/bin/ksh93
while true
do
PATH=.
for DIR in /usr/bin /usr/sbin /bin /usr/local/bin
do
if [ -d ${DIR} ]
then
PATH=${PATH}:${DIR}
true
fi
done
time=$(date +%T)
prev=${page}
page=$(cat /proc/$$/stat|awk '{print $24}')
if (( page > prev ));
then
echo "$time $page"
fi
done
The page statistic in /proc/<pid>/stat continually goes up, indicating ksh leaks memory:
20:57:05 483
20:57:05 484
20:57:05 485
...
After changing the interpreter to bash the symptom does not appear.
Resolution
- for RHEL 6 update to
ksh-20120801-34.el6_9released with Advisory RHBA-2017:1378 or newer - for RHEL 7 update to
ksh-20120801-34.el7released with Advisory RHBA-2017:1936 or newer
Root Cause
In ksh value of PATH is maintained as a linked list. When a subshell is forked, ksh duplicates PATH by increasing refcount in path_alias() function. However, when the subshell exits this refcount was not being decreased in affected ksh versions. Next time when PATH is reset to a new value, ksh calls path_delete() function to delete linked list which stored older path. But path_delete() function does not free elements whose refcount is greater than 1 and leaves a memory leak in the shell.
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.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
