puresnmp
puresnmp copied to clipboard
puresnmp.transport.SNMPClientProtocol.datagram_received sometimes throws asyncio.exceptions.InvalidStateError
Issue Description
[25310]: Exception in callback _SelectorDatagramTransport._read_ready()
[25310]: handle: <Handle _SelectorDatagramTransport._read_ready()>
[25310]: Traceback (most recent call last):
[25310]: File "/opt/pyenv/versions/3.9.9/lib/python3.9/asyncio/events.py", line 80, in _run
[25310]: self._context.run(self._callback, *self._args)
[25310]: File "/opt/pyenv/versions/3.9.9/lib/python3.9/asyncio/selector_events.py", line 1029, in _read_ready
[25310]: self._protocol.datagram_received(data, addr)
[25310]: File "/path/.venv/lib/python3.9/site-packages/puresnmp/transport.py", line 137, in datagram_received
[25310]: self.future.set_result(data)
[25310]: asyncio.exceptions.InvalidStateError: invalid state
For me this occurs only in production, and thus is a bit hard to pin down the exact circumstances in order to reproduce it with minimal code.
My guess is that get or walk has timed out and future is already cancelled when data is received and datagram_received gets executed...
Adding a check to see if the future is cancelled before set_result seems to get rid of the exceptions:
if not self.future.cancelled():
self.future.set_result(data)