diff --git a/aiocoap/protocol.py b/aiocoap/protocol.py index 75474971..8c739b4b 100644 --- a/aiocoap/protocol.py +++ b/aiocoap/protocol.py @@ -594,10 +594,16 @@ def __init__(self, plumbing_request, loop, log): else: self.observation = None - loop.create_task(self._run()) + self._runner = loop.create_task(self._run()) self.log = log + self.response.add_done_callback(self._response_cancellation_handler) + + def _response_cancellation_handler(self, response): + if self.response.cancelled() and not self._runner.cancelled(): + self._runner.cancel() + @staticmethod def _add_response_properties(response, request): response.request = request