[Common] Fix Twisted 24.x no longer calling addCallbacks in Deferred subclass

A change to Deferred logic in 24.10 means that `addCallbacks` is no
longer being called in Deferred methods. This is a problem since our
subclass of Deferred for coroutines was using that mechanism to check
awaited status.

Fixed by extending each method that requires awaited status check.

Reference: https://github.com/twisted/twisted/pull/12283
Reference: 4d04b64b77
This commit is contained in:
Calum Lind 2025-02-04 19:51:28 +00:00
parent 8df36c454b
commit cb182daaaf
No known key found for this signature in database
GPG key ID: 90597A687B836BA3

View file

@ -196,11 +196,27 @@ class CoroutineDeferred(defer.Deferred):
d = defer.ensureDeferred(self.coro) d = defer.ensureDeferred(self.coro)
d.chainDeferred(self) d.chainDeferred(self)
def addCallbacks(self, *args, **kwargs): # noqa: N802 def _callback_activate(self):
"""Verify awaited status before calling activate."""
assert not self.awaited, 'Cannot add callbacks to an already awaited coroutine.' assert not self.awaited, 'Cannot add callbacks to an already awaited coroutine.'
self.activate() self.activate()
def addCallback(self, *args, **kwargs): # noqa: N802
self._callback_activate()
return super().addCallback(*args, **kwargs)
def addCallbacks(self, *args, **kwargs): # noqa: N802
self._callback_activate()
return super().addCallbacks(*args, **kwargs) return super().addCallbacks(*args, **kwargs)
def addErrback(self, *args, **kwargs): # noqa: N802
self._callback_activate()
return super().addErrback(*args, **kwargs)
def addBoth(self, *args, **kwargs): # noqa: N802
self._callback_activate()
return super().addBoth(*args, **kwargs)
_RetT = TypeVar('_RetT') _RetT = TypeVar('_RetT')