Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

new unbounded memory leak in exception handling?

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


greg at hewgill

Nov 17, 2009, 2:31 AM

Post #1 of 3 (368 views)
Permalink
new unbounded memory leak in exception handling?

I've constructed an example program that does not leak memory in Python
2.x, but causes unbounded memory allocation in Python 3.1. Here is the
code:

import gc
import sys

class E(Exception):
def __init__(self, fn):
self.fn = fn
def call(self):
self.fn()

def f():
raise E(f)

a = E(f)
while True:
print(len(gc.get_objects()))
try:
a.call()
except E:
# get exception value in a python2/3 portable way
a = sys.exc_info()[1]

Every time through the loop, the length of gc.get_objects() increases
by 7 under Python 3.1. I believe this is a regression error, since
Python 2.x does not exhibit the same behaviour.

Can anybody confirm this?

Greg Hewgill
http://hewgill.com
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


rdmurray at bitdance

Nov 17, 2009, 5:40 AM

Post #2 of 3 (328 views)
Permalink
Re: new unbounded memory leak in exception handling? [In reply to]

On Tue, 17 Nov 2009 at 10:31, Greg Hewgill wrote:
> I've constructed an example program that does not leak memory in Python
> 2.x, but causes unbounded memory allocation in Python 3.1. Here is the
> code:
>
> import gc
> import sys
>
> class E(Exception):
> def __init__(self, fn):
> self.fn = fn
> def call(self):
> self.fn()
>
> def f():
> raise E(f)
>
> a = E(f)
> while True:
> print(len(gc.get_objects()))
> try:
> a.call()
> except E:
> # get exception value in a python2/3 portable way
> a = sys.exc_info()[1]
>
> Every time through the loop, the length of gc.get_objects() increases
> by 7 under Python 3.1. I believe this is a regression error, since
> Python 2.x does not exhibit the same behaviour.
>
> Can anybody confirm this?

I think you want to take a look at PEP 3134. And then please file a doc
bug to have someone update the documentation of sys.exc_info, since the
advice in the warning box is no longer valid in Python 3.

--David (RDM)
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


greg at hewgill

Nov 17, 2009, 10:14 AM

Post #3 of 3 (330 views)
Permalink
Re: new unbounded memory leak in exception handling? [In reply to]

On Tue, Nov 17, 2009 at 08:40:37AM -0500, R. David Murray wrote:
> I think you want to take a look at PEP 3134. And then please file a doc
> bug to have someone update the documentation of sys.exc_info, since the
> advice in the warning box is no longer valid in Python 3.

Thanks for the pointer, I understand the cause now. I had read the
warning in the description of sys.exc_info, but I hadn't realised that
Python was (now) implicitly saving the traceback for me. I was able to
solve this memory leak by explicitly clearing __traceback__:

except E:
# get exception value in a python2/3 portable way
a = sys.exc_info()[1]
a.__traceback__ = None

I have added a doc bug issue for this: http://bugs.python.org/issue7340

Greg Hewgill
http://hewgill.com
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.