Reference leak if function called by deferToThread() raises an error

[email protected]
Newsgroups gmane.comp.python.twisted.bugs
Message-ID <[email protected]>
New submission from haypo <None>:

In the following example, the DestroyMe object is not destroyed at inthread() exit.

It looks like a Failure() stores the Python frame and so stores the 'context' argument of inthread(). But the context object is not destroyed after displayError().

I think that the context object is still stored (reference leak)... somewhere. It's maybe referenced by the failure object, which is not destroyed directly? Another idea: the reference is maybe stored in the thread pool?

{{{
#!python
from twisted.internet.threads import deferToThread
from twisted.internet import reactor
from twisted.internet.defer import succeed

class DestroyMe:
    def __init__(self):
        print "CREATE DESTROY ME"

    def __del__(self):
        print "DELETE DESTROY ME"

def inthread(x, context):
    raise ValueError("error!")
    return x * 10

def func1(x, context):
    x = x + 1
    d = deferToThread(inthread, x, context)
    d.addErrback(displayError, "thread")
    return d

def printResult(x):
    print "Result = %s" % x
    print "test done"

def displayError(failure, where):
    print "FAILURE in %s: %s" % (where, failure)

def test1():
    print "test1"
    d = succeed(10)
    context = DestroyMe()
    d.addErrback(displayError, "test1")
    d.addCallback(func1, context)
    d.addCallback(printResult)
    return d

def stop():
    print "Stop!"
    reactor.stop()

reactor.callLater(0, test1)
reactor.callLater(4, stop)

reactor.run()
}}}

I tested Twisted core 8.1.0 (Ubuntu Ibex).

----------
Type     : defect
Component: core
Keywords : 
Priority : normal
Nosy     : 
----------
http://twistedmatrix.com/trac/ticket/3853
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.