No stack trace seen with 'Unhandled error in Deferred' when using Python Twisted
<p>Having read through <a href="http://krondo.com/an-introduction-to-asynchronous-programming-and-twisted/" rel="noreferrer">http://krondo.com/an-introduction-to-asynchronous-programming-and-twisted/</a>, I have a basic understanding of Twisted.  I have built a testing infrastructure that a few other people also use.  From time to time, we experience 'Unhandled error in Deferred' type errors if there are bugs or typos in the code.  The problem is that there isn't always an accompanying stack trace with these errors making it hard for people to debug.</p>

<p>I have reproduced the problem with the following simple code:</p>

<pre><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()
</code></pre>

<p>When I run this code, I see the following output:</p>

<pre><code>run_test: start
Unhandled error in Deferred:
</code></pre>

<p>And if I uncomment the addCallbacks() line above, then I get some stack trace information:</p>

<pre><code>run_test: start
err [Failure instance: Traceback: &lt;type 'exceptions.NameError'&gt;: 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
--- &lt;exception caught here&gt; ---
/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py:1260:_inlineCallbacks
tmp.py:18:run_test
]
Unhandled error in Deferred:
</code></pre>

<p>My question is, is there some way to get the stack trace without having to add callbacks at all defer sites?  </p>