<?xml version="1.0" encoding="UTF-8" ?>

<bugzilla version="5.2"
          urlbase="https://bugzilla.altlinux.org/"
          
          maintainer="jenya@basealt.ru"
>

    <bug>
          <bug_id>47836</bug_id>
          
          <creation_ts>2023-10-04 16:52:38 +0300</creation_ts>
          <short_desc>memory leaks in python bindings for RPM</short_desc>
          <delta_ts>2023-10-04 19:51:49 +0300</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>4</classification_id>
          <classification>Development</classification>
          <product>Sisyphus</product>
          <component>python3-module-rpm</component>
          <version>unstable</version>
          <rep_platform>x86_64</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>CLOSED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P5</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Ivan A. Melnikov">iv</reporter>
          <assigned_to name="placeholder@altlinux.org">placeholder</assigned_to>
          <cc>at</cc>
    
    <cc>glebfm</cc>
    
    <cc>imz</cc>
    
    <cc>ldv</cc>
    
    <cc>placeholder</cc>
    
    <cc>vt</cc>
          
          <qa_contact>qa-sisyphus</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>234351</commentid>
    <comment_count>0</comment_count>
    <who name="Ivan A. Melnikov">iv</who>
    <bug_when>2023-10-04 16:52:38 +0300</bug_when>
    <thetext>Сизиф, python3-module-rpm-4.13.0.1-alt39

Для поддержки sisyphus_riscv64 и sisyphus_loongarch64 у нас есть несколько аналитических инструментов, написаных на python; некоторые из них используют модуль rpm для чтения pkglist&apos;ов.

Было замечено, что если перечитывать pkglist&apos;ы несколько раз, вся память очень быстро куда-то девается.

Вот несложный способ воспроизвести проблему:

$ wget &apos;http://ftp.altlinux.org/pub/distributions/ALTLinux/Sisyphus/noarch/base/pkglist.classic.xz&apos;
$ xz -d pkglist.classic.xz 
$ cat test.py
import sys
import rpm

for _ in range(int(sys.argv[1])):
    with open(&quot;pkglist.classic&quot;) as f:
        rpms = rpm.readHeaderListFromFD(f.fileno())
        print(len(rpms))
        del rpms
$ python3 test.py 1
19279

По данным time, maxresident растёт линейно с ростом argv[1], valgrind  --leak-check=yes больше всего ругается на:

96,513,220 (771,080 direct, 95,742,140 indirect) bytes in 19,277 blocks are definitely lost in loss record 453 of 453
   at 0x48461E8: calloc (vg_replace_malloc.c:1117)
   by 0x5480053: rcalloc (rpmmalloc.c:55)
   by 0x53F8346: UnknownInlinedFun (header.c:171)
   by 0x53F8346: headerImport (header.c:829)
   by 0x53F8B2F: headerRead (header.c:1029)
   by 0x53A94DD: hdr_new (header-py.c:403)
   by 0x2DCABA: UnknownInlinedFun (typeobject.c:1100)
   by 0x2DCABA: _PyObject_MakeTpCall (call.c:214)
   by 0x2E93A5: _PyEval_EvalFrameDefault (ceval.c:4774)
   by 0x3A5AF8: UnknownInlinedFun (pycore_ceval.h:73)
   by 0x3A5AF8: _PyEval_Vector (ceval.c:6439)
   by 0x3A52E2: PyEval_EvalCode (ceval.c:1154)
   by 0x3C3149: run_eval_code_obj (pythonrun.c:1714)
   by 0x3BF8B2: run_mod (pythonrun.c:1735)
   by 0x3D47A1: pyrun_file (pythonrun.c:1630)

Алексей Шепляков, которого эта проблема больше всего зацепила, предположил, что это https://bugzilla.redhat.com/show_bug.cgi?id=1358467 . И действительно cherry pick 40326b5724b0cd55a21b2d86eeef344e4826f863 поверх rpm 4.13.0.1-alt39 помогает: valgrind не находит больше ничего definitely lost, а maxresident по данным time почти не растёт с ростом argv[0].</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>234356</commentid>
    <comment_count>1</comment_count>
    <who name="Ivan A. Melnikov">iv</who>
    <bug_when>2023-10-04 18:05:52 +0300</bug_when>
    <thetext>Задача с исправлением: https://git.altlinux.org/tasks/330954/

Please review &amp;&amp; approve.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>234361</commentid>
    <comment_count>2</comment_count>
    <who name="Repository Robot">repository-robot</who>
    <bug_when>2023-10-04 19:51:49 +0300</bug_when>
    <thetext>rpm-4.13.0.1-alt40 -&gt; sisyphus:

 Wed Oct 04 2023 Alexey Sheplyakov &lt;asheplyakov@altlinux&gt; 4.13.0.1-alt40
 - Plug a memory leak in python bindings (closes: #47836).</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>