Commit 4a269b2
committed
bug: fix ProxyClient<Thread> deadlock if disconnected as IPC call is returning
This bug is currently causing mptest "disconnecting and blocking" test to
occasionally hang as reported by maflcko in
bitcoin/bitcoin#33244.
The bug was actually first reported by Sjors in
Sjors/bitcoin#90 (comment) and there are
more details about it in
#189.
The bug is caused by the "disconnecting and blocking" test triggering a
disconnect right before a server IPC call returns. This results in a race
between the IPC server thread and the onDisconnect handler in the event loop
thread both trying to destroy the server's request_threads ProxyClient<Thread>
object when the IPC call is done.
There was a lack of synchronization in this case, fixed here by adding
loop->sync() a few places. Specifically the fixes were to:
- Always call SetThread on the event loop thread using the loop->sync() method,
to prevent a race between the ProxyClient<Thread> creation code and the
connection shutdown code if there was an ill-timed disconnect.
- Similarly in ~ProxyClient<Thread> and thread-context.h PassField(), use
loop->sync() when destroying the thread object, in case a disconnect happens
at that time.
A few other changes were made in this commit to make the resulting code safer
and simpler, even though they are not technically necessary for the fix:
- In thread-context.h PassField(), Waiter::m_mutex is now unlocked while
destroying ProxyClient<Thread> just to respect EventLoop::m_mutex and
Waiter::m_mutex lock order and never lock the Waiter first. This is just for
consistency. There is no actually possibility for a deadlock here due to the
new sync() call.
- This adds asserts to make sure functions expected to run on the event
loop thread are only called on that thread.
- This inlines the ProxyClient<Thread>::setDisconnectCallback function, just
because it was a short function only called in a single place.1 parent 85df964 commit 4a269b2
3 files changed
+48
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
70 | | - | |
71 | 69 | | |
72 | 70 | | |
73 | 71 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
96 | 99 | | |
97 | | - | |
| 100 | + | |
98 | 101 | | |
99 | 102 | | |
100 | 103 | | |
101 | 104 | | |
102 | 105 | | |
103 | 106 | | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
115 | 125 | | |
116 | 126 | | |
117 | 127 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
83 | 89 | | |
84 | 90 | | |
85 | 91 | | |
| |||
155 | 161 | | |
156 | 162 | | |
157 | 163 | | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
158 | 167 | | |
159 | 168 | | |
160 | 169 | | |
| |||
306 | 315 | | |
307 | 316 | | |
308 | 317 | | |
| 318 | + | |
309 | 319 | | |
310 | 320 | | |
311 | 321 | | |
| |||
314 | 324 | | |
315 | 325 | | |
316 | 326 | | |
317 | | - | |
| 327 | + | |
318 | 328 | | |
319 | 329 | | |
320 | 330 | | |
| |||
323 | 333 | | |
324 | 334 | | |
325 | 335 | | |
| 336 | + | |
| 337 | + | |
326 | 338 | | |
327 | 339 | | |
328 | | - | |
329 | 340 | | |
330 | 341 | | |
331 | 342 | | |
| |||
338 | 349 | | |
339 | 350 | | |
340 | 351 | | |
341 | | - | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
342 | 361 | | |
343 | 362 | | |
344 | 363 | | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | 364 | | |
353 | 365 | | |
354 | 366 | | |
| |||
0 commit comments