Public bug reported:
Ubuntu release: 24.04 LTS (noble), seen on arm64 and amd64
Package: bash 5.2.21-2ubuntu4
This is a low-severity report for the record. No stable release update
is requested.
== What happens ==
When the exit builtin runs while a top-level command is still being executed,
bash leaves without freeing the parsed command tree of that top-level command.
valgrind memcheck reports the tree as "definitely lost". It happens when exit
is called
- from inside a function, an if, a loop, or a && / || list, or
- at top level while an EXIT trap is set.
It does not happen for a top-level exit with no EXIT trap, for a script
that runs off its end, for a top-level exit after "trap - EXIT", or for
bash -c 'exit 0'.
== Steps to reproduce ==
printf '#!/bin/bash\nf() { exit 0; }\nf\n' > /tmp/b.sh && chmod +x /tmp/b.sh
valgrind --leak-check=full --show-leak-kinds=definite /tmp/b.sh
Result (frames resolved with bash-dbgsym):
72 (32 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss
record 320 of 344
at malloc
by make_simple_command (make_cmd.c:528)
by yyparse (parse.y:782)
by parse_command (eval.c:348)
by read_command (eval.c:392)
by reader_loop (eval.c:139)
by main (shell.c:833)
Expected: "definitely lost: 0 bytes in 0 blocks".
== Variants measured ==
bash 5.2.21-2ubuntu4, amd64, valgrind 3.22.0, each as a two- or three-
line script file:
exit 0 at top level, no trap: 0 bytes
f() { exit 0; } then f: 72 bytes (32 direct, 40 indirect)
if true; then exit 0; fi: 157 bytes (32 direct, 125 indirect)
while true; do exit 0; done: 149 bytes (32 direct, 117 indirect)
test 1 = 1 && exit 0: 259 bytes (32 direct, 227 indirect)
trap "echo c" EXIT then exit 0 at top level: 56 bytes (32 direct, 24 indirect)
trap "echo c" EXIT then script ends: 0 bytes
trap "echo c" EXIT, trap - EXIT, exit 0: 0 bytes
The size follows the size of the top-level command that was running. It
is always exactly one block per process: 200 extra commands before the
exit do not change the result.
The same record is present with upstream bash 5.3.0 built from the
release tarball (trap plus top-level exit: 56 bytes, make_cmd.c:457 from
parse.y:832). The Ubuntu 5.3 packages in later series were not measured.
== Cause, from the 5.2.21 source ==
builtins/exit.def ends in jump_to_top_level (EXITBLTIN). reader_loop in
eval.c handles EXITBLTIN by setting current_command = NULL and leaving
the loop. dispose_command is not called on that path, so the tree of the
command that contained the exit is abandoned.
== Impact ==
None at run time. The block is lost at the moment the shell exits and
the kernel reclaims it. Nothing accumulates in a running process.
The only effect is on leak checking. A test harness that runs shell
scripts under valgrind --trace-children=yes and gates on "definitely
lost: 0" gets a record against /usr/bin/bash for almost every script,
which hides real leaks in the programs under test.
== Workaround ==
A valgrind suppression for Leak records whose frames are in
/usr/bin/bash, or --trace-children-skip=*/bash.
== Other information ==
There is no upstream fix to backport: none of the official bash 5.3 patches 001
to 020 touches this path, and there is no release newer than 5.3. The same
stack has been reported before without a fix:
- bug-bash, May 2017, "memory leak in bash":
https://lists.gnu.org/archive/html/bug-bash/2017-05/msg00098.html
- Red Hat Bugzilla 1746101 (closed EOL):
https://bugzilla.redhat.com/show_bug.cgi?id=1746101
- bug-bash, June 2023, "Various small leaks":
https://lists.gnu.org/archive/html/bug-bash/2023-06/msg00081.html
LP: #1860133 is a different, startup-time record and is not a duplicate.
A possible resolution short of a code change is to ship a valgrind
suppression file for bash.
** Affects: bash (Ubuntu)
Importance: Undecided
Status: New
** Tags: noble
--
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2174802
Title:
bash does not free the command being executed when exit runs inside a
function, loop or if (valgrind: definitely lost)
To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/bash/+bug/2174802/+subscriptions
--
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs