mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
[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 <mzumsande@gmail.com> Co-authored-by: theStack <sebastian.falbesoner@gmail.com>
This commit is contained in:
parent
5b91fb14ab
commit
bb7bffed79
1 changed files with 7 additions and 3 deletions
|
@ -163,6 +163,9 @@ class P2PConnection(asyncio.Protocol):
|
|||
# The underlying transport of the connection.
|
||||
# Should only call methods on this from the NetworkThread, c.f. call_soon_threadsafe
|
||||
self._transport = None
|
||||
# This lock is acquired before sending messages over the socket. There's an implied lock order and
|
||||
# p2p_lock must not be acquired after _send_lock as it could result in deadlocks.
|
||||
self._send_lock = threading.Lock()
|
||||
self.v2_state = None # EncryptedP2PState object needed for v2 p2p connections
|
||||
|
||||
@property
|
||||
|
@ -360,6 +363,7 @@ class P2PConnection(asyncio.Protocol):
|
|||
|
||||
This method takes a P2P payload, builds the P2P header and adds
|
||||
the message to the send buffer to be sent over the socket."""
|
||||
with self._send_lock:
|
||||
tmsg = self.build_message(message)
|
||||
self._log_message("send", message)
|
||||
return self.send_raw_message(tmsg)
|
||||
|
|
Loading…
Add table
Reference in a new issue