_mutex
suffix rather than the cs_
prefix.
cs_vProcessMsg
with Mutex (and rename)
#24122
_mutex
suffix rather than the cs_
prefix.
-BEGIN VERIFY SCRIPT-
sed -i 's/cs_vProcessMsg/m_process_msgs_mutex/g' $(git grep -l cs_vProcessMsg)
sed -i 's/vProcessMsg/m_process_msgs/g' $(git grep -l vProcessMsg)
-END VERIFY SCRIPT-
In each of the critical sections, only the the m_process_msgs member is
accessed, without any chance that within one section another one is
called. Hence, we can use an ordinary Mutex instead of RecursiveMutex.
Concept ACK
I was thinking, should we use AssertLockNotHeld(m_process_msgs_mutex)
before locking the mutex and add thread safety annotation LOCKS_EXCLUDED(m_process_msgs_mutex)
, with the function declaration of where this mutex is used?
Doing so will prevent any risk of error due to simultaneous locking of this mutex.
LOCKS_EXCLUDED
annotation is only possible for one method (PeerManagerImpl::ProcessMessages
), as for the other (CConnman::SocketHandlerConnected
) the elements that cointain the loop are in a list rather than a direct parameter that can be accessed.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
21@@ -22,8 +22,8 @@ void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_by
22 nSizeAdded += it->m_raw_message_size;
23 }
24 {
25- LOCK(node.cs_vProcessMsg);
26- node.vProcessMsg.splice(node.vProcessMsg.end(), node.vRecvMsg, node.vRecvMsg.begin(), it);
27+ LOCK(node.m_process_msgs_mutex);
Can we add AssertLockNotHeld
above this line?
0 AssertLockNotHeld(node.m_process_msgs_mutex);
1 LOCK(node.m_process_msgs_mutex);
ConnmanTestMsg::NodeReceiveMsgBytes
.
as for the other (CConnman::SocketHandlerConnected) the elements that cointain the loop are in a list rather than a direct parameter that can be accessed.
Sounds interesting. Let me take another look at this before I can be 100% sure. In the meantime, I had one observation that I would like to discuss.
Co-authored-by: Shashwat <svangani239@gmail.com>
412@@ -413,8 +413,8 @@ class CNode
413 Mutex cs_hSocket;
414 Mutex cs_vRecv;
415
416- RecursiveMutex cs_vProcessMsg;
417- std::list<CNetMessage> vProcessMsg GUARDED_BY(cs_vProcessMsg);
418+ Mutex m_process_msgs_mutex;
419+ std::list<CNetMessage> m_process_msgs GUARDED_BY(m_process_msgs_mutex);
420 size_t nProcessQueueSize{0};
Reading the code makes me strongly believe that the value of nProcessQueueSize
must correspond to the m_process_msgs
content. To preserve this invariant of the CNode
class I’m suggesting:
0 size_t nProcessQueueSize GUARDED_BY(m_process_msgs_mutex){0};
Concept ACK.
Following best practices, mutexes should synchronize access to internal class data to prevent violation of class invariants.
The current implementation has a significant design drawback – external synchronizing, i.e., acquiring CNode::m_process_msgs_mutex
from outside of the CNode
instance.
Therefore, I’m suggesting to consider to encapsulate access to the private members m_process_msgs
and nProcessQueueSize
with internal synchronizing using m_process_msgs_mutex
.
One new CNode
member function could implement https://github.com/bitcoin/bitcoin/blob/e3de7cb9039770e0fd5b8bb8a5cba35c87ae8f00/src/net.cpp#L1592-L1604
🐙 This pull request conflicts with the target branch and needs rebase.
Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a “draft”.