Commit 2b6705d
committed
test: fix TcpProxy close/race in test_client_routes.py NLB test helper
TcpProxy.stop()/drop_connections() used to close a connection's sockets
directly, from the manager thread, while that connection's own forwarder
thread could still be blocked in select()/recv() on those exact file
descriptors -- a classic close-under-concurrent-user race. Since fds are
process-global, closing them could let the OS silently recycle the fd
number into a brand new connection before the stale forwarder thread's
blocked call unwound, causing it to read/write/close a socket that no
longer belonged to it (observed here as unhandled
"ValueError: file descriptor cannot be a negative integer (-1)" crashes
in _forward_loop once a socket was closed out from under it). stop()
also only ever joined the accept-loop thread, never the per-connection
forwarder threads it had just closed sockets out from under.
Fix, scoped entirely to this test helper (no driver code touched):
- TcpProxy._connections now maps (client_sock, target_sock) -> the
forwarder thread serving that pair.
- stop()/drop_connections() now shut down (SHUT_RDWR) both sockets --
safe to do concurrently with a blocked select()/recv(), unlike
close() -- and then join() every forwarder thread before returning.
Only the forwarder thread itself ever closes its own sockets now,
and only after it has fully stopped using them.
- _handle_new_connection starts the forwarder thread before publishing
it into _connections, so a concurrent stop()/drop_connections() can
never observe (and try to join) a thread that hasn't started yet.
- NLBEmulator.add_node()/remove_node() now serialize against each
other via an RLock, so a new proxy/connection can never be created
while another thread's remove_node() is still tearing one down.
Fixes #948.
Validation:
- Standalone stress harness (no CCM needed) driving concurrent
clients through TcpProxy while repeatedly calling
drop_connections()/stop()+restart from another thread: pre-fix,
40 iterations produced 162 unhandled ValueError crashes; post-fix,
40 iterations (same parameters) and a follow-up 150-iteration run
produced zero corruptions/exceptions/leaked threads.
- Full end-to-end runs of
TestFullNodeReplacementThroughNlb::test_should_survive_full_node_replacement_through_nlb
against a real CCM cluster, both before and after the fix, to
check for behavioral regressions and reproduce the reported
flakiness.1 parent 9b5b037 commit 2b6705d
1 file changed
Lines changed: 75 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
77 | 79 | | |
78 | 80 | | |
79 | 81 | | |
| |||
98 | 100 | | |
99 | 101 | | |
100 | 102 | | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
| 103 | + | |
105 | 104 | | |
106 | 105 | | |
107 | 106 | | |
| |||
120 | 119 | | |
121 | 120 | | |
122 | 121 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
| 122 | + | |
127 | 123 | | |
128 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
129 | 159 | | |
130 | 160 | | |
131 | 161 | | |
| |||
153 | 183 | | |
154 | 184 | | |
155 | 185 | | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | 186 | | |
161 | 187 | | |
162 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
163 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
164 | 197 | | |
165 | 198 | | |
166 | 199 | | |
| |||
178 | 211 | | |
179 | 212 | | |
180 | 213 | | |
181 | | - | |
| 214 | + | |
182 | 215 | | |
183 | 216 | | |
184 | 217 | | |
| |||
189 | 222 | | |
190 | 223 | | |
191 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
192 | 239 | | |
193 | 240 | | |
194 | 241 | | |
| |||
227 | 274 | | |
228 | 275 | | |
229 | 276 | | |
230 | | - | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
231 | 281 | | |
232 | 282 | | |
233 | 283 | | |
| |||
288 | 338 | | |
289 | 339 | | |
290 | 340 | | |
291 | | - | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
292 | 349 | | |
293 | 350 | | |
294 | 351 | | |
295 | 352 | | |
| 353 | + | |
| 354 | + | |
296 | 355 | | |
297 | | - | |
298 | 356 | | |
299 | 357 | | |
300 | 358 | | |
| |||
0 commit comments