Commit 38b5650
authored
Fix ByteBuf Memory Leak in PutOperation.encryptChunk() (#3169)
Fixes ByteBuf memory leak in PutOperation.encryptChunk() when KMS throws exception
after retainedDuplicate() is evaluated.
Root Cause: Java evaluates constructor arguments left-to-right. In the original code:
If kms.getRandomKey() throws GeneralSecurityException after buf.retainedDuplicate()
has alreadybeen evaluated and incremented the refCount, the retained duplicate is
orphaned and leaks because:
1. The constructor never completes, so ownership never transfers to EncryptJob
2. The exception handler has no reference to clean up the orphaned ByteBuf
The ix temp holds onto reference for proper handling on exception.
Added testMinimal_RetainedDuplicateArgumentEvaluationLeak() which:
- Creates a ByteBuf with refCount=1
- Simulates the argument evaluation pattern where retainedDuplicate() executes before exception
- Verifies no leak occurs with proper cleanup1 parent aa1d91b commit 38b5650
File tree
2 files changed
+102
-2
lines changed- ambry-router/src
- main/java/com/github/ambry/router
- test/java/com/github/ambry/router
2 files changed
+102
-2
lines changedLines changed: 10 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1580 | 1580 | | |
1581 | 1581 | | |
1582 | 1582 | | |
| 1583 | + | |
1583 | 1584 | | |
1584 | 1585 | | |
1585 | 1586 | | |
1586 | 1587 | | |
1587 | 1588 | | |
1588 | 1589 | | |
| 1590 | + | |
1589 | 1591 | | |
1590 | 1592 | | |
1591 | | - | |
1592 | | - | |
| 1593 | + | |
| 1594 | + | |
1593 | 1595 | | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
1594 | 1599 | | |
1595 | 1600 | | |
1596 | 1601 | | |
1597 | 1602 | | |
1598 | 1603 | | |
1599 | 1604 | | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
1600 | 1608 | | |
1601 | 1609 | | |
1602 | 1610 | | |
| |||
Lines changed: 92 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1339 | 1339 | | |
1340 | 1340 | | |
1341 | 1341 | | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
1342 | 1434 | | |
0 commit comments