Commit 93507f0
authored
Fix ByteBuf memory leak in PutOperation when operations are aborted (#3176)
* Fix ByteBuf memory leak in PutOperation when operations are aborted
When a PutOperation is aborted or fails before all data is processed by
the ChunkFiller thread, channelReadBuf may still hold a reference to a
ByteBuf that was read from the channel but not yet consumed. This causes
a memory leak as the buffer is never released.
Changes:
- Add channelReadBuf.release() in cleanupChunks() to ensure the buffer
is properly released when the operation completes or fails.
- Remove synchronized modifier from PutChunk.fillFrom() as it's not
needed with the ChunkFiller single threaded model
- Add test case testPutOperationByteBufLeakOnAbort() to verify ByteBuf
resources are properly released when operations are aborted mid-flight
The fix ensures that even if the ChunkFiller thread hasn't processed all
data from the channel when an operation completes/fails, the ByteBuf
holding unprocessed data is properly released, preventing memory leaks.
* Go one step further and prevent use-after-free race condition
After even more extensive review, we identified that not only is there a case where the read buf
is leaked, it's also possible for the read buf to be used after free in rare cases where the
memory is released by the network and GCed before it is retained by the ChunkFiller thread.
Some of the memory leak in the previous commit is masking some of the use-after-free issues
fixed in this commit. Given how intertwined they are, it wouldn't be safe to merge these
changes separately.
* Protect the operations in fillChunks from router errors.1 parent 691eb00 commit 93507f0
File tree
4 files changed
+302
-17
lines changed- ambry-router/src
- main/java/com/github/ambry/router
- test/java/com/github/ambry/router
4 files changed
+302
-17
lines changedLines changed: 0 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | 18 | | |
20 | 19 | | |
21 | 20 | | |
| |||
Lines changed: 40 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| |||
574 | 575 | | |
575 | 576 | | |
576 | 577 | | |
577 | | - | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
578 | 581 | | |
579 | | - | |
580 | | - | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
581 | 595 | | |
582 | 596 | | |
583 | 597 | | |
| |||
676 | 690 | | |
677 | 691 | | |
678 | 692 | | |
679 | | - | |
| 693 | + | |
680 | 694 | | |
681 | 695 | | |
682 | 696 | | |
683 | 697 | | |
684 | 698 | | |
685 | 699 | | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
686 | 705 | | |
687 | 706 | | |
688 | 707 | | |
| |||
707 | 726 | | |
708 | 727 | | |
709 | 728 | | |
710 | | - | |
711 | | - | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
712 | 736 | | |
713 | 737 | | |
714 | 738 | | |
| |||
1096 | 1120 | | |
1097 | 1121 | | |
1098 | 1122 | | |
1099 | | - | |
1100 | | - | |
1101 | | - | |
1102 | | - | |
1103 | | - | |
1104 | | - | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
1105 | 1130 | | |
1106 | 1131 | | |
1107 | 1132 | | |
1108 | 1133 | | |
| 1134 | + | |
| 1135 | + | |
1109 | 1136 | | |
1110 | 1137 | | |
1111 | 1138 | | |
| |||
1642 | 1669 | | |
1643 | 1670 | | |
1644 | 1671 | | |
1645 | | - | |
| 1672 | + | |
1646 | 1673 | | |
1647 | 1674 | | |
1648 | 1675 | | |
| |||
Lines changed: 52 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| 26 | + | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
| |||
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
40 | | - | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| 101 | + | |
| 102 | + | |
100 | 103 | | |
101 | 104 | | |
102 | 105 | | |
| |||
4550 | 4553 | | |
4551 | 4554 | | |
4552 | 4555 | | |
| 4556 | + | |
| 4557 | + | |
| 4558 | + | |
| 4559 | + | |
| 4560 | + | |
| 4561 | + | |
| 4562 | + | |
| 4563 | + | |
| 4564 | + | |
| 4565 | + | |
| 4566 | + | |
| 4567 | + | |
| 4568 | + | |
| 4569 | + | |
| 4570 | + | |
| 4571 | + | |
| 4572 | + | |
| 4573 | + | |
| 4574 | + | |
| 4575 | + | |
| 4576 | + | |
| 4577 | + | |
| 4578 | + | |
| 4579 | + | |
| 4580 | + | |
| 4581 | + | |
| 4582 | + | |
| 4583 | + | |
| 4584 | + | |
| 4585 | + | |
| 4586 | + | |
| 4587 | + | |
| 4588 | + | |
| 4589 | + | |
| 4590 | + | |
| 4591 | + | |
| 4592 | + | |
| 4593 | + | |
| 4594 | + | |
| 4595 | + | |
| 4596 | + | |
| 4597 | + | |
| 4598 | + | |
| 4599 | + | |
| 4600 | + | |
| 4601 | + | |
| 4602 | + | |
| 4603 | + | |
4553 | 4604 | | |
0 commit comments