Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.
RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.

Bug 1358467

Summary: memory leak when creating hdr objects
Product: Red Hat Enterprise Linux 7 Reporter: Tasos Papaioannou <tpapaioa>
Component: rpmAssignee: Florian Festi <ffesti>
Status: CLOSED ERRATA QA Contact: Jan Blazek <jblazek>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 7.2CC: gbailey
Target Milestone: rc   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: rpm-4.11.3-22.el7 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
: 1438811 (view as bug list) Environment:
Last Closed: 2017-08-01 19:33:33 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
valgrind memory leak output
none
proposed patch none

Description Tasos Papaioannou 2016-07-20 18:47:17 UTC
Created attachment 1182219 [details]
valgrind memory leak output

Description of problem:

I have a python script that calls rpm.readHeaderListFromFile() to check rpm headers. When calling this method iteratively over rpms in a yum repository, the python process' heap grows continuously. There appears to be a memory leak when creating the header object in librpm. A simple reproducer:

----
# cat rpm_hdr_test.py 
import rpm

for i in xrange(1000):
    hdr1 = rpm._rpm.hdr()
----

Running a memory leak test w/ valgrind:

----
# valgrind --leak-check=full --time-stamp=yes --log-file=rpm_hdr.log python rpm_hdr_test.py
----

shows the following:

----
==00:00:00:06.733 19733== 296,000 (40,000 direct, 256,000 indirect) bytes in 1,000 blocks are definitely lost in loss record 1,935 of 1,935
==00:00:00:06.733 19733==    at 0x4C2B974: calloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==00:00:00:06.733 19733==    by 0xDA8E2EE: rcalloc (rpmmalloc.c:55)
==00:00:00:06.733 19733==    by 0xCE38951: headerCreate (header.c:168)
==00:00:00:06.733 19733==    by 0xCC14FD4: hdr_new (header-py.c:394)
==00:00:00:06.733 19733==    by 0x4ED4E52: type_call (typeobject.c:729)
==00:00:00:06.733 19733==    by 0x4E7F0C2: PyObject_Call (abstract.c:2529)
==00:00:00:06.733 19733==    by 0x4F1338B: do_call (ceval.c:4316)
==00:00:00:06.733 19733==    by 0x4F1338B: call_function (ceval.c:4121)
==00:00:00:06.733 19733==    by 0x4F1338B: PyEval_EvalFrameEx (ceval.c:2740)
==00:00:00:06.733 19733==    by 0x4F171EC: PyEval_EvalCodeEx (ceval.c:3330)
==00:00:00:06.733 19733==    by 0x4F172F1: PyEval_EvalCode (ceval.c:689)
==00:00:00:06.733 19733==    by 0x4F3072E: run_mod (pythonrun.c:1374)
==00:00:00:06.733 19733==    by 0x4F318ED: PyRun_FileExFlags (pythonrun.c:1360)
==00:00:00:06.733 19733==    by 0x4F32B78: PyRun_SimpleFileExFlags (pythonrun.c:952)
----

Full log file is attached.


Version-Release number of selected component (if applicable):

rpm-4.11.3-17.el7.x86_64
rpm-libs-4.11.3-17.el7.x86_64
rpm-python-4.11.3-17.el7.x86_64

How reproducible:

100%

Steps to Reproduce:
1.) Create and deallocate multiple hdr objects in python with rpm.readHeaderListFromFile().
2.) See increasing heap size throughout lifetime of process.


Actual results:

Memory leak when creating hdr objects.

Expected results:

No memory leak.

Additional info:

See attached valgrind log.

Comment 3 Tasos Papaioannou 2016-07-25 18:36:02 UTC
Created attachment 1183913 [details]
proposed patch

In python/header-py.c: hdr_new -> headerNew -> headerCreate in lib/header.c. headderCreate calls headerLink to increment h->nrefs once, then hdr_Wrap in python/header-py.c calls headerLink again when wrapping the C header object in a Python object, so h->nrefs is now 2.

When the Python object is deallocated, hdr_dealloc calls headerFree, which calls headerUnlink once and then quits without freeing memory if h->nrefs > 0, leading to a memory leak in Python.

I've attached a test patch which resolves the issue in my testing, to call headerUnlink once in hdr_new between the calls to headerNew and hdr_Wrap. I haven't verified whether this patch works with other code paths that call hdr_new other than "hdr1=rpm.hdr()", so it may be necessary to do this for only some of the if/else blocks in hdr_new.

Comment 4 Florian Festi 2016-10-21 10:01:41 UTC
Thanks for hunting this down. Turns out the reference counting of header objects is messed up at even more places in the Python binding. This is now fixed up stream as commit 40326b5724b0cd55a21b2d86eeef344e4826f863.

Comment 8 errata-xmlrpc 2017-08-01 19:33:33 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2017:2160