Commit a321441
utils: use WeakReference in BatchInserter to fix ThreadLocal retention of JCQueue
BatchInserter held a strong reference to its owning JCQueue, and the
inserters live in instance-field ThreadLocals on the same JCQueue. This
formed a cycle through ThreadLocalMap:
value (BatchInserter) -> queue (JCQueue) -> thdLocalBatcher (ThreadLocal) = key
Because the key was strongly reachable via the value, the weak-key
expunge path in ThreadLocalMap never triggered, and the JCQueue (along
with its metrics, recv/overflow queues and batch buffers) could not be
GC'd for as long as any producer thread that ever published to it stayed
alive.
The fix stores the JCQueue as a WeakReference inside BatchInserter,
cutting the value->key path. When the last external strong ref to the
JCQueue is dropped, the ThreadLocal field it owns becomes weakly
reachable, the ThreadLocalMap key can be expunged, and both the
BatchInserter and the JCQueue are released.
flush() and tryFlush() dereference the WeakReference once at entry and
bail out cleanly if the queue has already been collected (dead topology
in LocalCluster/embedded scenarios). publish() and tryPublish() are
unchanged — they only manipulate currentBatch.
Fixes #8810
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent a18676b commit a321441
1 file changed
Lines changed: 18 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
345 | 346 | | |
346 | 347 | | |
347 | 348 | | |
348 | | - | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
349 | 355 | | |
350 | 356 | | |
351 | 357 | | |
352 | | - | |
| 358 | + | |
353 | 359 | | |
354 | 360 | | |
355 | 361 | | |
| |||
402 | 408 | | |
403 | 409 | | |
404 | 410 | | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
405 | 416 | | |
406 | 417 | | |
407 | 418 | | |
| |||
432 | 443 | | |
433 | 444 | | |
434 | 445 | | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
435 | 451 | | |
436 | 452 | | |
437 | 453 | | |
| |||
0 commit comments