Commit 7f0bfc2
Walk gather's child tasks in initialize's finally so cancel-mid-gather doesn't leak completed conns
``ConnectionPool.initialize`` opened ``min_size`` connections
concurrently via ``asyncio.gather(...)``. The post-gather
result-iteration loop populated the ``successes`` list and
mirrored it into ``unqueued_survivors`` so the finally could
walk and close any conn that didn't make it into the pool
queue.
If the calling task was cancelled WHILE gather was still
running (outer ``asyncio.timeout``, TaskGroup sibling
failure, ``task.cancel()``), gather raised CancelledError
out of the await. Control jumped from the await line
directly to the finally — skipping the result-iteration
loop AND the ``unqueued_survivors = list(successes)``
assignment. The finally's close sweep then iterated the
default empty list, leaking every conn that had completed
before the cancel. Each leaked conn carried a live TCP
socket bound to the leader plus an asyncio
StreamReaderProtocol task pinned by the conn's _protocol
ref — the GC-eventually-warns path with no exception path
to surface the leak in tests.
The ``_size`` accounting was already correct (ISSUE-240
moved that into the finally). The connection-close sweep
now gets the same treatment: build the child tasks
explicitly with ``asyncio.create_task`` so the finally
has a handle on every child regardless of where control
left.
Gated on ``gather_returned``: when gather completed
normally the existing pop-based discipline already
tracked the un-queued tail precisely; walking tasks
again would re-add already-queued conns and double-close.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent b0a6ef2 commit 7f0bfc2
2 files changed
Lines changed: 125 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
350 | 350 | | |
351 | 351 | | |
352 | 352 | | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
353 | 383 | | |
354 | 384 | | |
355 | 385 | | |
356 | 386 | | |
357 | | - | |
| 387 | + | |
358 | 388 | | |
359 | 389 | | |
| 390 | + | |
360 | 391 | | |
361 | 392 | | |
362 | 393 | | |
| |||
447 | 478 | | |
448 | 479 | | |
449 | 480 | | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
450 | 507 | | |
451 | 508 | | |
452 | 509 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
172 | 239 | | |
173 | 240 | | |
174 | 241 | | |
| |||
0 commit comments