Merge lp:~cjwatson/storm/py3-traceback-reference-cycles into lp:storm

Proposed by Colin Watson
Status: Merged
Merged at revision: 572
Proposed branch: lp:~cjwatson/storm/py3-traceback-reference-cycles
Merge into: lp:storm
Diff against target: 36 lines (+16/-4)
2 files modified
NEWS (+8/-0)
storm/exceptions.py (+8/-4)
To merge this branch: bzr merge lp:~cjwatson/storm/py3-traceback-reference-cycles
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Storm Developers Pending
Review via email: mp+402726@code.launchpad.net

Commit message

Avoid traceback reference cycles when wrapping exceptions.

Description of the change

`wrap_exceptions` stored the original exception's traceback in a local variable, but the traceback in turn contains a reference to that frame, creating a reference cycle. The situation is worse on Python 3, because the original exception also has the associated traceback as a `__traceback__` attribute, producing more complicated reference cycles. Delete these local variables before returning to avoid leaking memory over time.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS'
2--- NEWS 2021-04-19 09:52:52 +0000
3+++ NEWS 2021-05-13 23:27:28 +0000
4@@ -1,3 +1,11 @@
5+0.26
6+====
7+
8+Bug fixes
9+---------
10+
11+- Avoid traceback reference cycles when wrapping exceptions.
12+
13 0.25 (2021-04-19)
14 =================
15
16
17=== modified file 'storm/exceptions.py'
18--- storm/exceptions.py 2019-06-26 07:16:54 +0000
19+++ storm/exceptions.py 2021-05-13 23:27:28 +0000
20@@ -178,8 +178,12 @@
21 # As close to "raise wrapped.with_traceback(tb) from e" as
22 # we can manage, but without causing syntax errors on
23 # various versions of Python.
24- if six.PY2:
25- six.reraise(wrapped, None, tb)
26- else:
27- six.raise_from(wrapped.with_traceback(tb), e)
28+ try:
29+ if six.PY2:
30+ six.reraise(wrapped, None, tb)
31+ else:
32+ six.raise_from(wrapped.with_traceback(tb), e)
33+ finally:
34+ # Avoid traceback reference cycles.
35+ del wrapped, tb
36 raise

Subscribers

People subscribed via source and target branches

to status/vote changes: