diff --git a/src/tribler/core/components/socks_servers/socks5/connection.py b/src/tribler/core/components/socks_servers/socks5/connection.py index 060fe4de425..2c1d1794f29 100644 --- a/src/tribler/core/components/socks_servers/socks5/connection.py +++ b/src/tribler/core/components/socks_servers/socks5/connection.py @@ -215,3 +215,4 @@ def close(self, reason='unspecified'): if self.transport: self.transport.close() + self.transport = None diff --git a/src/tribler/core/components/socks_servers/socks5/tests/test_connection.py b/src/tribler/core/components/socks_servers/socks5/tests/test_connection.py index 13094ed2362..6e9e0041fe4 100644 --- a/src/tribler/core/components/socks_servers/socks5/tests/test_connection.py +++ b/src/tribler/core/components/socks_servers/socks5/tests/test_connection.py @@ -50,7 +50,7 @@ def test_invalid_version(connection): Test passing an invalid version to the socks5 server """ connection.data_received(unhexlify('040100')) - assert not connection.transport.connected + assert not connection.transport def test_method_request(connection): diff --git a/src/tribler/core/components/tunnel/community/dispatcher.py b/src/tribler/core/components/tunnel/community/dispatcher.py index d7a3b2cb135..fa88a76b1a0 100644 --- a/src/tribler/core/components/tunnel/community/dispatcher.py +++ b/src/tribler/core/components/tunnel/community/dispatcher.py @@ -93,9 +93,13 @@ async def on_socks5_tcp_data(self, tcp_connection, destination, request): self._logger.info('Failed to get HTTP response using tunnels: %s', e) return + transport = tcp_connection.transport + if not transport: + return + if response: - tcp_connection.transport.write(response) - tcp_connection.transport.close() + transport.write(response) + transport.close() def select_circuit(self, connection, request): if request.destination[1] == CIRCUIT_ID_PORT: diff --git a/src/tribler/core/components/tunnel/tests/test_dispatcher.py b/src/tribler/core/components/tunnel/tests/test_dispatcher.py index c31c5cea7e2..e1cc953b0ae 100644 --- a/src/tribler/core/components/tunnel/tests/test_dispatcher.py +++ b/src/tribler/core/components/tunnel/tests/test_dispatcher.py @@ -94,6 +94,14 @@ async def test_on_socks_in_tcp(dispatcher): tcp_connection.transport.write.assert_called_once_with(b'test') +async def test_on_socks5_tcp_data_with_transport_none(dispatcher): + tcp_connection = Mock(transport=None) + dispatcher.set_socks_servers([tcp_connection.socksserver]) + + dispatcher.tunnels.perform_http_request = Mock(return_value=succeed(b'test')) + await dispatcher.on_socks5_tcp_data(tcp_connection, ("0.0.0.0", 1024), b'') + + def test_circuit_dead(dispatcher, mock_circuit): """ Test whether the correct peers are removed when a circuit breaks