Commit db59a28
fix(lifecycle): release invocation context after platform report to prevent memory leak (#1050)
JIRA: https://datadoghq.atlassian.net/browse/SVLS-8625
Git issue reported:
#1049
## What does this PR do?
Fixes a memory leak in the extension where `ContextBuffer` accumulated
`Context` objects indefinitely across warm Lambda invocations.
**Root cause:** `on_platform_report()` reads the `Context` for runtime
duration and CPU/network enhanced metrics, but never called
`context_buffer.remove()` afterwards. Each warm invocation appended a
new `Context` to the buffer; none were ever freed.
**Fix:** Call `self.context_buffer.remove(request_id)` at the end of
`on_platform_report()`, after all processing for the invocation is
complete.
**Impact:** Most visible when `DD_CAPTURE_LAMBDA_PAYLOAD=true` — each
retained `Context` holds the full captured request/response payload as
span metadata (~500 KB per invocation). Without the fix, a Lambda
function invoked thousands of times would accumulate hundreds of MB of
leaked memory in the extension process.
## Testing
- Unit tests
- A test with 50+ invocations shows
| without the fix | with the fix |
|--------|--------|
| `buffer size after remove: 52`|`buffer size after remove: 1`|
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 1559722 commit db59a28
2 files changed
+89
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
202 | | - | |
| 202 | + | |
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
771 | 771 | | |
772 | 772 | | |
773 | 773 | | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
774 | 782 | | |
775 | 783 | | |
776 | 784 | | |
| |||
1612 | 1620 | | |
1613 | 1621 | | |
1614 | 1622 | | |
1615 | | - | |
1616 | | - | |
| 1623 | + | |
1617 | 1624 | | |
1618 | 1625 | | |
1619 | 1626 | | |
| |||
1655 | 1662 | | |
1656 | 1663 | | |
1657 | 1664 | | |
| 1665 | + | |
| 1666 | + | |
| 1667 | + | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
1658 | 1676 | | |
1659 | 1677 | | |
1660 | 1678 | | |
| |||
1672 | 1690 | | |
1673 | 1691 | | |
1674 | 1692 | | |
1675 | | - | |
| 1693 | + | |
1676 | 1694 | | |
1677 | 1695 | | |
1678 | 1696 | | |
| |||
1682 | 1700 | | |
1683 | 1701 | | |
1684 | 1702 | | |
1685 | | - | |
| 1703 | + | |
1686 | 1704 | | |
1687 | 1705 | | |
1688 | 1706 | | |
1689 | 1707 | | |
1690 | 1708 | | |
1691 | 1709 | | |
1692 | | - | |
1693 | 1710 | | |
1694 | 1711 | | |
1695 | 1712 | | |
| |||
1698 | 1715 | | |
1699 | 1716 | | |
1700 | 1717 | | |
1701 | | - | |
1702 | 1718 | | |
1703 | 1719 | | |
1704 | 1720 | | |
| |||
1707 | 1723 | | |
1708 | 1724 | | |
1709 | 1725 | | |
1710 | | - | |
1711 | 1726 | | |
1712 | 1727 | | |
1713 | 1728 | | |
| |||
1716 | 1731 | | |
1717 | 1732 | | |
1718 | 1733 | | |
1719 | | - | |
1720 | 1734 | | |
1721 | 1735 | | |
1722 | 1736 | | |
| 1737 | + | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
| 1741 | + | |
| 1742 | + | |
| 1743 | + | |
| 1744 | + | |
| 1745 | + | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
| 1758 | + | |
| 1759 | + | |
| 1760 | + | |
| 1761 | + | |
| 1762 | + | |
| 1763 | + | |
| 1764 | + | |
| 1765 | + | |
| 1766 | + | |
| 1767 | + | |
| 1768 | + | |
| 1769 | + | |
| 1770 | + | |
| 1771 | + | |
| 1772 | + | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
| 1776 | + | |
| 1777 | + | |
| 1778 | + | |
| 1779 | + | |
| 1780 | + | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
| 1786 | + | |
| 1787 | + | |
| 1788 | + | |
| 1789 | + | |
| 1790 | + | |
| 1791 | + | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
| 1798 | + | |
| 1799 | + | |
| 1800 | + | |
1723 | 1801 | | |
1724 | 1802 | | |
1725 | 1803 | | |
| |||
0 commit comments