From 4a2f3baf28ccae0571eaa5985a3bcd94baf8c818 Mon Sep 17 00:00:00 2001 From: sneakypete81 Date: Thu, 6 Feb 2020 21:45:37 +0000 Subject: [PATCH 1/3] Avoid importing twisted.internet.reactor in global scope This import has side effects, which can cause problems when using a non- default reactor (qt5reactor, for example). --- src/wormhole/transit.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/wormhole/transit.py b/src/wormhole/transit.py index 98e1b72..0f28e89 100644 --- a/src/wormhole/transit.py +++ b/src/wormhole/transit.py @@ -10,8 +10,9 @@ from collections import deque import six from nacl.secret import SecretBox +import twisted.internet from twisted.internet import (address, defer, endpoints, error, interfaces, - protocol, reactor, task) + protocol, task) from twisted.internet.defer import inlineCallbacks, returnValue from twisted.protocols import policies from twisted.python import log @@ -559,7 +560,7 @@ class Common: transit_relay, no_listen=False, tor=None, - reactor=reactor, + reactor=None, timing=None): self._side = bytes_to_hexstr(os.urandom(8)) # unicode if transit_relay: @@ -579,6 +580,8 @@ class Common: self._waiting_for_transit_key = [] self._listener = None self._winner = None + if reactor is None: + reactor = twisted.internet.reactor self._reactor = reactor self._timing = timing or DebugTiming() self._timing.add("transit") @@ -596,7 +599,7 @@ class Common: direct_hints = [ DirectTCPV1Hint(six.u(addr), portnum, 0.0) for addr in addresses ] - ep = endpoints.serverFromString(reactor, "tcp:%d" % portnum) + ep = endpoints.serverFromString(twisted.internet.reactor, "tcp:%d" % portnum) return direct_hints, ep def get_connection_abilities(self): From 0c434998509ec26a519c357388ad142fd2d77bb5 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Thu, 6 Feb 2020 22:48:25 -0800 Subject: [PATCH 2/3] test_transit: fix test to tolerate proper virtualized reactor --- src/wormhole/test/test_transit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wormhole/test/test_transit.py b/src/wormhole/test/test_transit.py index 8676ccc..0af8d3b 100644 --- a/src/wormhole/test/test_transit.py +++ b/src/wormhole/test/test_transit.py @@ -1312,8 +1312,8 @@ class Transit(unittest.TestCase): @inlineCallbacks def test_success_direct(self): - clock = task.Clock() - s = transit.TransitSender("", reactor=clock) + reactor = mock.Mock() + s = transit.TransitSender("", reactor=reactor) s.set_transit_key(b"key") hints = yield s.get_connection_hints() # start the listener del hints From aead2495761ede279c3b49b1f389f35940656fed Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Thu, 6 Feb 2020 22:49:12 -0800 Subject: [PATCH 3/3] Transit: properly virtualize reactor in _build_listener --- src/wormhole/transit.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wormhole/transit.py b/src/wormhole/transit.py index 0f28e89..084b85f 100644 --- a/src/wormhole/transit.py +++ b/src/wormhole/transit.py @@ -10,7 +10,6 @@ from collections import deque import six from nacl.secret import SecretBox -import twisted.internet from twisted.internet import (address, defer, endpoints, error, interfaces, protocol, task) from twisted.internet.defer import inlineCallbacks, returnValue @@ -581,7 +580,7 @@ class Common: self._listener = None self._winner = None if reactor is None: - reactor = twisted.internet.reactor + from twisted.internet import reactor self._reactor = reactor self._timing = timing or DebugTiming() self._timing.add("transit") @@ -599,7 +598,7 @@ class Common: direct_hints = [ DirectTCPV1Hint(six.u(addr), portnum, 0.0) for addr in addresses ] - ep = endpoints.serverFromString(twisted.internet.reactor, "tcp:%d" % portnum) + ep = endpoints.serverFromString(self._reactor, "tcp:%d" % portnum) return direct_hints, ep def get_connection_abilities(self):