Skip to content

Commit

Permalink
Fix InvalidStateError in UdpSocketManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ichorid committed Jun 13, 2020
1 parent 86d1324 commit 622d618
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import socket
import struct
from asyncio import CancelledError, DatagramProtocol, Future, ensure_future, get_event_loop, start_server
from unittest.mock import Mock

from aiohttp.web_exceptions import HTTPBadRequest

Expand All @@ -11,6 +12,7 @@
FakeBep33DHTSession,
FakeDHTSession,
HttpTrackerSession,
UdpSocketManager,
UdpTrackerSession,
)
from tribler_core.session import Session
Expand Down Expand Up @@ -112,6 +114,27 @@ async def test_udpsession_timeout(self):
await session.connect_to_tracker()
transport.close()

@timeout(5)
async def test_pop_finished_transaction(self):
"""
Test that receiving a datagram for an already finished
tracker session does not result in InvalidStateError
"""

mgr = UdpSocketManager()
transaction_id = 123
mgr.connection_made(Mock())
mock_tracker_session = Mock()
mock_tracker_session.transaction_id = transaction_id
mgr.send_request(Mock(), mock_tracker_session)
self.assertTrue(mgr.tracker_sessions)

mgr.tracker_sessions[transaction_id].cancel()

data = struct.pack("!iiq", 124, transaction_id, 126)
mgr.datagram_received(data, None)
self.assertFalse(mgr.tracker_sessions)

@timeout(5)
async def test_httpsession_cancel_operation(self):
session = HttpTrackerSession("127.0.0.1", ("localhost", 8475), "/announce", 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ def datagram_received(self, data, _):
if data and len(data) >= 4:
transaction_id = struct.unpack_from('!i', data, 4)[0]
if transaction_id in self.tracker_sessions:
self.tracker_sessions.pop(transaction_id).set_result(data)
session = self.tracker_sessions.pop(transaction_id)
if not session.done():
session.set_result(data)


class UdpTrackerSession(TrackerSession):
Expand Down

0 comments on commit 622d618

Please sign in to comment.