Author vstinner
Recipients eli.bendersky, jess.j, scoder, serhiy.storchaka, vstinner
Date 2018-12-18.11:20:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Oops, my PR 11169 used the wrong issue number: bpo-35257 instead of bpo-35502. Anyway, I closed it, the change is too complex.


IMHO the root issue is the handling of the SyntaxError exception in XMLPullParser.feed(). I wrote a fix, but I don't have the bandwidth to write an unit test checking that the reference cycle is broken.

commit 9f3354d36a89d7898bdb631e5119cc37e9a74840 (fix_etree_leak)
Author: Victor Stinner <>
Date:   Fri Dec 14 22:03:16 2018 +0100

    bpo-35257: Fix memory leak in XMLPullParser.feed()
    Fix memory leak in XMLPullParser.feed() of xml.etree: on syntax
    error, clear the traceback to break a reference cycle.

diff --git a/Lib/xml/etree/ b/Lib/xml/etree/
index c1cf483cf5..f17c52541b 100644
--- a/Lib/xml/etree/
+++ b/Lib/xml/etree/
@@ -1266,6 +1266,8 @@ class XMLPullParser:
             except SyntaxError as exc:
+                # bpo-35502: Break reference cycle
+                #exc.__traceback__ = None
     def _close_and_return_root(self):

I don't see any behavior difference in XMLPullParser.read_events() which raise again the exception:

        events = self._events_queue
        while events:
            event = events.popleft()
            if isinstance(event, Exception):
                raise event
                yield event


PR 11170 is also a nice enhancement (fix treebuilder_gc_traverse()), but maybe we should also prevent creating reference cycles in the first place?
Date User Action Args
2018-12-18 11:20:42vstinnersetrecipients: + vstinner, scoder, eli.bendersky, serhiy.storchaka, jess.j
2018-12-18 11:20:42vstinnersetmessageid: <>
2018-12-18 11:20:42vstinnerlinkissue35502 messages
2018-12-18 11:20:42vstinnercreate