No stack trace with "unhandled error in delayed" when using Python Twisted

After reading http://krondo.com/an-introduction-to-asynchronous-programming-and-twisted/ , I have a basic understanding of Twisted. I created a testing framework that several other people also use. From time to time, we encounter errors like Unhandled error in Deferred if there are errors or typos in the code. The problem is that the stack trace with these errors is not always accompanied, which makes it difficult to debug people.

I reproduced the problem with the following simple code:

from twisted.internet import defer, reactor
from twisted.internet.task import deferLater


def sleep(delay):
    """Twisted safe sleep.

    When using Twisted, it is not safe to call time.sleep().  So
    we have this function to emulate the behavior.

    """
    return deferLater(reactor, delay, lambda: None)


@defer.inlineCallbacks
def run_test():
    print 'run_test: start'
    bug()
    yield sleep(1)
    print 'run_test: stop'


@defer.inlineCallbacks
def run_tests():

    def err(arg):
        print 'err', arg
        return arg

    def success(arg):
        print 'success', arg
        return arg

    d = run_test()
    #d.addCallbacks(success, err)

    try:
        yield d
    finally:
        reactor.stop()


reactor.callWhenRunning(run_tests)
reactor.run()

When I run this code, I see the following output:

run_test: start
Unhandled error in Deferred:

And if I uncomment the addCallbacks () line above, I get stack trace information:

run_test: start
err [Failure instance: Traceback: <type 'exceptions.NameError'>: global name 'bug' is not defined
/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py:1406:unwindGenerator
/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py:1260:_inlineCallbacks
tmp.py:34:run_tests
/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py:1406:unwindGenerator
--- <exception caught here> ---
/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py:1260:_inlineCallbacks
tmp.py:18:run_test
]
Unhandled error in Deferred:

, , ?

+4
1

Deferred try/except magic, , addErrback. , , . " " try/except. inlineCallbacks :

try:
    run_test()
except Exception as err:
    # handle error here
finally:
    reactor.stop()

run_test(), , .

, "" , , , Twisted logger. -.

+4

Source: https://habr.com/ru/post/1661186/


All Articles