Commit 949573d
committed
Prevent IPC server crash if disconnected during IPC call
Antoine Poinsot <darosior@protonmail.com> reported a bug with details in
#182 (comment)
where an IPC client rapidly connecting and disconnecting to the server in a
loop could cause problems.
One problem this uncovered was hangs on shutdown fixed by the previous commit.
Another problem it uncovered is that if a disconnect happened while the IPC
server was executing an IPC call, and the onDisconnect() handling code ran
before the IPC call finished, memory corruption would occur usually leading to
a hang or crash.
This happend because the ~ProxyServerBase() destructor was written with the
assumption that it would always be called either from the ~Connection()
destructor, or before that point, so its m_context.connection pointer would
always be valid. Typically this was the case, but not if the connection was
closed during an active IPC call.
A unit test to reproduce this error is added in the subsequent commit.
The fix for the bug here is actually a code simplification. Previously
~ProxyServerBase() destructor would append a std::function callback destroying
the ProxyServerBase::m_impl object to
m_context.connection->m_async_cleanup_fns, so the object destructor could be
called asynchronously without blocking the event loop. Now it just appends the
same callback function to m_context.loop->m_async_fns without going through the
Connection object.
The new code is an improvement over the previous code in two other ways:
- It is a code simplification since previous code was needlessly complicated.
- In the case where there is no disconnect, and the remote ProxyClient is
destroyed, and no "destroy" method is declared, this change causes the
ProxyServer::m_impl object to be freed shortly after the client is freed,
instead of being delayed until the connection is closed. (If a "destroy"
method is declared, this change has no effect because in that case destroying
the ProxyClient calls destroy and destroys ProxyServer::m_impl synchronously.)
The previous code was trying to make the ProxyServer cleanup with
Connection::m_async_cleanup_fns mirror the ProxyClient cleanup with
Connection::m_sync_cleanup_fns, but it was just fragile and unncecessarily
complicated.
Some comments about ProxyClient cleanup code are updated for clarity here but
no changes are made.1 parent ea38392 commit 949573d
2 files changed
+29
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
181 | 185 | | |
182 | 186 | | |
183 | 187 | | |
| |||
329 | 333 | | |
330 | 334 | | |
331 | 335 | | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | 336 | | |
337 | 337 | | |
338 | 338 | | |
| |||
361 | 361 | | |
362 | 362 | | |
363 | 363 | | |
364 | | - | |
365 | | - | |
366 | | - | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
367 | 367 | | |
368 | | - | |
369 | 368 | | |
370 | 369 | | |
371 | 370 | | |
| |||
454 | 453 | | |
455 | 454 | | |
456 | 455 | | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
457 | 466 | | |
458 | 467 | | |
459 | 468 | | |
| |||
477 | 486 | | |
478 | 487 | | |
479 | 488 | | |
480 | | - | |
| 489 | + | |
481 | 490 | | |
482 | 491 | | |
483 | 492 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
92 | 95 | | |
93 | 96 | | |
94 | 97 | | |
| |||
137 | 140 | | |
138 | 141 | | |
139 | 142 | | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | 143 | | |
148 | 144 | | |
149 | 145 | | |
| |||
166 | 162 | | |
167 | 163 | | |
168 | 164 | | |
169 | | - | |
| 165 | + | |
170 | 166 | | |
171 | | - | |
| 167 | + | |
172 | 168 | | |
173 | 169 | | |
174 | 170 | | |
| |||
185 | 181 | | |
186 | 182 | | |
187 | 183 | | |
188 | | - | |
| 184 | + | |
| 185 | + | |
189 | 186 | | |
190 | 187 | | |
191 | 188 | | |
| |||
0 commit comments