Title: Local variables not freed when Exception raises in function called from cycle
Type: resource usage Stage:
Components: Interpreter Core Versions: Python 2.4, Python 2.7, Python 2.6, Python 2.5
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Glin, georg.brandl, nikratio
Priority: normal Keywords:

Created on 2009-04-01 08:49 by Glin, last changed 2009-06-17 09:40 by georg.brandl. This issue is now closed.

File name Uploaded Description Edit Glin, 2009-04-01 08:49 Example simulating issue
Messages (6)
msg84989 - (view) Author: (Glin) Date: 2009-04-01 08:49
You have a while cycle. inside of it a try-except block and in this
try-except block calling a function. Inside of this function raises an
exception, with is caught in the try-except block.

What happens:
Local variables of the function are not freed. (OK, they are freed when
the program leaves a function inside of which is the while cycle, or
when another exception raises and is caught, but it's not helpful when
you have some server program based on infinite while loop or working
with a large amount of data).

Example program is attached.

Output of the example program:

While start
job() start
Creating 1
Catched AttributeError
While end
While start
job() start
Creating 2
job() end
Deleting 2
While end
While start
job() start
Creating 3
job() end
Deleting 3
While end

As you can see, a variable 'a' created in the first call (which throws
an exception) of the 'job' function will never get freed. 

Imagine that in 'job' function you create a large amount of data, or
worse you create a database connection, which will be opened forever.

Tested on Python 2.5.2 and 2.7(svn). On the contrary, Python 3.0 does
not have this issue (it frees variables of the 'job' function at the end
of except: block.)

As Python 2.X will be another long time with us, I think it should be
fixed to behave correctly (that is as in Python 3.)
msg85025 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-04-01 15:51
This is not a question of "correct" behavior, but of documented
semantics.  We can't change that semantics in 2.x, like we did for 3.x,
because many people rely on the exception variable being available after
the except clause finishes.

Closing as "won't fix."
msg85189 - (view) Author: (Glin) Date: 2009-04-02 10:07
I'm not talking about exception variable, but about the variables in
local scope of function job() (in my example it is the variable 'a' of
class A).

Sorry, if I did not make myself clear.
msg85202 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-04-02 14:17
I know, but that object is kept alive by the frame object that is kept
alive by the exception assigned to "e".
msg89306 - (view) Author: Nikolaus Rath (nikratio) * Date: 2009-06-12 22:43
I just spend several days figuring out a problem that was caused by this
behaviour and in the process I really tried to read everything that
relates to destruction of objects and exception handling in Python.

Georg, could you give me a pointer where exactly these semantics are

msg89449 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-06-17 09:40
Well, it's the basic principle that an object is not destroyed until
there are no more references to it. The documented semantics I referred
to are that a variable assigned to in an "except X, Y" clause lives
beyond the scope of that clause.
Date User Action Args
2009-06-17 09:40:30georg.brandlsetmessages: + msg89449
2009-06-12 22:43:14nikratiosetnosy: + nikratio
messages: + msg89306
2009-04-02 14:17:22georg.brandlsetstatus: open -> closed

messages: + msg85202
2009-04-02 10:07:51Glinsetstatus: closed -> open

messages: + msg85189
2009-04-01 15:51:10georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg85025

resolution: wont fix
2009-04-01 08:49:37Glincreate