Commit ae504a3
opencode
fix(pooling): avoid PoolEntry/ConnectionImpl deadlock by calling client.close()/emit outside monitor
The previous attempt to fix race conditions in PoolEntry made shutdown(),
close() and handleConnectError() synchronized on the PoolEntry monitor while
they invoked client.close() (which acquires the ConnectionImpl monitor) and
emitted pool events. The Netty close-event path runs the reverse order:
ConnectionImpl.onChannelClose() (synchronized on ConnectionImpl) calls back
into PoolEntry.handleConnectError(), which then blocks on the PoolEntry
monitor. This produced an ABBA deadlock between the HashedWheelTimer worker
and the Netty event loop, which manifested as a permanent hang in
DistributingRoundRobinBalancerTest (the @timeout(25) SameThread variant
cannot interrupt CompletableFuture.join()).
The thread dump of the hung JVM confirmed:
- 'pool-N-thread-1' BLOCKED in ConnectionImpl.closeChannel, holding PoolEntry
- 'multiThreadIoEventLoopGroup-2-1' BLOCKED in PoolEntry.handleConnectError, holding ConnectionImpl
Fix: narrow the entry-monitor critical sections to field mutations only.
client.close() and emit(...) are now called outside the monitor, breaking
the lock-ordering cycle.
Also fix a race in internalConnect(): assign connectFuture to the local chain
BEFORE attaching whenComplete, so an inline failure completing before the
method returns cannot have its nulling of connectFuture in
handleConnectError() overwritten by a stale failed future.
Verified locally:
- DistributingRoundRobinBalancerTest: 5/5 on 2.11.8, 5/5 on 3.5.0, no hangs
- testDistributingRoundRobinStartWithStuckNodeA (reproducer): 5/5 stable
- ConnectionPoolReconnectsTest + ConnectionPoolHeartbeatTest: 5/5 on 2.11.8, 3/3 on 3.5.0
- IProtoClientPoolTest + ConnectionPoolTest (unit): 32/321 parent c527f09 commit ae504a3
1 file changed
Lines changed: 36 additions & 12 deletions
Lines changed: 36 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
345 | 345 | | |
346 | 346 | | |
347 | 347 | | |
348 | | - | |
| 348 | + | |
349 | 349 | | |
350 | 350 | | |
351 | 351 | | |
352 | 352 | | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
357 | 367 | | |
358 | 368 | | |
359 | 369 | | |
| |||
422 | 432 | | |
423 | 433 | | |
424 | 434 | | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
425 | 440 | | |
426 | 441 | | |
427 | 442 | | |
| |||
433 | 448 | | |
434 | 449 | | |
435 | 450 | | |
436 | | - | |
| 451 | + | |
437 | 452 | | |
438 | 453 | | |
439 | 454 | | |
| |||
443 | 458 | | |
444 | 459 | | |
445 | 460 | | |
446 | | - | |
447 | | - | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
448 | 464 | | |
449 | 465 | | |
450 | 466 | | |
| |||
477 | 493 | | |
478 | 494 | | |
479 | 495 | | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
480 | 502 | | |
481 | 503 | | |
482 | 504 | | |
483 | 505 | | |
484 | 506 | | |
485 | 507 | | |
486 | 508 | | |
| 509 | + | |
487 | 510 | | |
488 | | - | |
489 | 511 | | |
490 | | - | |
491 | | - | |
492 | 512 | | |
| 513 | + | |
| 514 | + | |
493 | 515 | | |
494 | 516 | | |
495 | 517 | | |
| |||
500 | 522 | | |
501 | 523 | | |
502 | 524 | | |
| 525 | + | |
| 526 | + | |
503 | 527 | | |
504 | 528 | | |
505 | 529 | | |
506 | 530 | | |
507 | 531 | | |
508 | 532 | | |
509 | 533 | | |
510 | | - | |
511 | 534 | | |
| 535 | + | |
512 | 536 | | |
513 | 537 | | |
514 | 538 | | |
| |||
0 commit comments