util/network-devp2p: fix possible deadlock caused by conflicting lock order#11769
util/network-devp2p: fix possible deadlock caused by conflicting lock order#11769BurtonQin wants to merge 1 commit into
Conversation
|
Hello, I didn't dive deep enough to approve this, but conflicting lock order on its own is a bad code smell. I wanted to just point out when this can happen and put it into context. My reasoning: 2: Mutexes in question are: reserved_nodes, sessions, handlers 3: The approach that I used to check this is to find all write locks (do a search for
|
Similar to #11766
This PR fixes possible deadlock caused by conflicting lock order:
Basically, I found three pairs of lock that may have varying lock orders:
reserved_nodes->handlers:
762-825,1118-1122,977-978 conflict with 851-865,851-876,1095-1098.
session->handlers:
763-825 conflicts with 851-850, 851-869.
sessions->handlers:
953-959 conflicts with 851-853.
Note that write lock has a higher priority than read lock. So the following code can lead to deadlock:
B holds a.read(), C holds b.read():
a.read() in C waits for a.write() (high priority) in D, which in turn waits for a.read() in B.
Similarly, b.read() in B waits for b.write() (high priority) in A, which in turn waits for b.read() in C.
The fix is to enforce the order.