Skip to content

Commit bb7bffe

Browse files
stratosphermzumsandetheStack
committed
[test] Use lock for sending P2P messages in test framework
Messages are built, encrypted and sent over the socket in v2 connections. If a race condition happens between python's main thread and p2p thread with both of them trying to send a message, it's possible that the messages get encrypted with wrong keystream. Messages are built and sent over the socket in v1 connections. So there's no problem if messages are sent in the wrong order. Co-authored-by: Martin Zumsande <[email protected]> Co-authored-by: theStack <[email protected]>
1 parent 5b91fb1 commit bb7bffe

File tree

1 file changed

+7
-3
lines changed
  • test/functional/test_framework

1 file changed

+7
-3
lines changed

test/functional/test_framework/p2p.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ def __init__(self):
163163
# The underlying transport of the connection.
164164
# Should only call methods on this from the NetworkThread, c.f. call_soon_threadsafe
165165
self._transport = None
166+
# This lock is acquired before sending messages over the socket. There's an implied lock order and
167+
# p2p_lock must not be acquired after _send_lock as it could result in deadlocks.
168+
self._send_lock = threading.Lock()
166169
self.v2_state = None # EncryptedP2PState object needed for v2 p2p connections
167170

168171
@property
@@ -360,9 +363,10 @@ def send_message(self, message):
360363
361364
This method takes a P2P payload, builds the P2P header and adds
362365
the message to the send buffer to be sent over the socket."""
363-
tmsg = self.build_message(message)
364-
self._log_message("send", message)
365-
return self.send_raw_message(tmsg)
366+
with self._send_lock:
367+
tmsg = self.build_message(message)
368+
self._log_message("send", message)
369+
return self.send_raw_message(tmsg)
366370

367371
def send_raw_message(self, raw_message_bytes):
368372
if not self.is_connected:

0 commit comments

Comments
 (0)