[Twisted-Python] Thread Cancelled

Robert DiFalco robert.difalco at gmail.com
Sun Jan 24 10:44:36 MST 2021


Hi, I apologize this question is a little vague. I'm looking for pointers.
I have a klein route that makes an underlying deferToThread call with a
simple single thread (an IO based sync call I can't change, a boto3 sqs
write). The thread pool is simple, just a couple of threads, nothing fancy.

VERY rarely it appears that Klein cancels the thread. What techniques can I
use to figure out why my thread is being Canceled? There's nothing in the
failure to tell me "who, why, or where" it was canceled. Also, I cannot get
this down to a reproducible case, but here's the boto3 sqs wrapper, this
fall back works fine, but it's a band-aide for an error I can't track down.:

def write(self, payload):
    """
    Write message to SQS async from thread pool. If twisted cancels the
    thread, instead write synchronously.

    def _retrySynchronously(error):
        if error.type != CancelledError:
            return error

        log.warn("Async SQS write cancelled. Calling synchronously.")
        return defer.succeed(self._writeSyncFallback(payload))

    deferredCall = self._deferToThread(self.sqs.write, payload)
    deferredCall.addErrback(_retrySynchronously)
    return deferredCall

def _writeSyncFallback(self, payload):
    return self.sqs.write(payload)

The _deferToThread call just uses my own thread pool with 2 threads, but is
otherwise stock.

Is there a level of logging I'm missing or some other thing that would tell
me why the thread is being canceled? The retry works great and Klein does
not return an error from the route.

Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20210124/9ecc0d44/attachment-0001.htm>


More information about the Twisted-Python mailing list