Skip to content

gh-148058: Do not refleak when XMLParser.__init__() called many times#148061

Closed
maurycy wants to merge 5 commits intopython:mainfrom
maurycy:xml-init-twice-no-ref-leak
Closed

gh-148058: Do not refleak when XMLParser.__init__() called many times#148061
maurycy wants to merge 5 commits intopython:mainfrom
maurycy:xml-init-twice-no-ref-leak

Conversation

@maurycy
Copy link
Copy Markdown
Contributor

@maurycy maurycy commented Apr 3, 2026

XMLParser.__init__() refleaks when called many times. See gh-148058 for more details. Thankfully, there's already xmlparser_gc_clear.

I copied the test pattern from:

class TestLeaks(unittest.TestCase):

def test_refleaks_in___init__(self):

def test_refleaks_in_decompressor___init__(self):

etc.

The test failed on main:

[2] 2026-04-03T22:44:40.611204490+0000 maurycy@weiss /home/maurycy/cpython (main-with-test-cherry-pick a16064f) % ./python.exe -m test test_xml_etree_c -m test_xmlparser_refleaks_in___init__   
Using random seed: 3586145128
0:00:00 load avg: 0.45 Run 1 test sequentially in a single process
0:00:00 load avg: 0.45 [1/1] test_xml_etree_c
test test_xml_etree_c failed -- Traceback (most recent call last):
  File "/home/maurycy/cpython/Lib/test/support/__init__.py", line 1395, in trace_wrapper
    return func(*args, **kwargs)
  File "/home/maurycy/cpython/Lib/test/test_xml_etree_c.py", line 182, in test_xmlparser_refleaks_in___init__
    self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 1917 != 0 within 10 delta (1917 difference)

0:00:00 load avg: 0.45 [1/1/1] test_xml_etree_c failed (1 failure)

== Tests result: FAILURE ==

1 test failed:
    test_xml_etree_c

Total duration: 83 ms
Total tests: run=1 (filtered) failures=1
Total test files: run=1/1 (filtered) failed=1
Result: FAILURE

@maurycy maurycy changed the title gh-148058: Do not leak refleak when XMLParser.__init__() called many times gh-148058: Do not refleak when XMLParser.__init__() called many times Apr 3, 2026
Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced by this check. I would rather prefer that we decide first what to do with re-init being called in general rather than fixing them case by case. I think other C classes are subject to this issue.

Comment on lines +3718 to +3719
if (self->parser != NULL) {
(void)xmlparser_gc_clear((PyObject *)self);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer rejecting this rather than fixing it. If there is no crash issues, I would rather not fix it at all.

@picnixz picnixz closed this Apr 4, 2026
@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 4, 2026

For XML parsers, I would rather hard reject re-init. Because there mighr be a way to do bad stuff if the parser is still a parent of someone else, isn't there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants