Skip to content

Commit 77bb95e

Browse files
Vizonexbdracowebknjazpre-commit-ci[bot]
authored
Fix memory leak caused by identity when default value is inplace (#1284)
Co-authored-by: J. Nick Koston <[email protected]> Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 87dd4a4 commit 77bb95e

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

CHANGES/1284.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed slow memory leak caused by identity by adding ``Py_DECREF`` to identity value before leaving ``md_pop_one`` on success
2+
-- by :user:`Vizonex`.

multidict/_multilib/hashtable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ md_pop_one(MultiDictObject *md, PyObject *key, PyObject **ret)
10191019
goto fail;
10201020
}
10211021
}
1022-
1022+
Py_DECREF(identity);
10231023
ASSERT_CONSISTENT(md, false);
10241024
return 0;
10251025
fail:

tests/isolated/multidict_pop.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ def _test_popone() -> None:
6363
check_for_leak()
6464

6565

66+
# SEE: https://github.com/aio-libs/multidict/issues/1273
67+
def _test_pop_with_default() -> None:
68+
# XXX: mypy wants an annotation so the only
69+
# thing we can do here is pass the headers along.
70+
result = MultiDict(headers)
71+
for i in range(1_000_000):
72+
result.pop(f"missing_key_{i}", None)
73+
check_for_leak()
74+
75+
6676
def _test_del() -> None:
6777
for _ in range(10):
6878
for _ in range(100):
@@ -76,6 +86,7 @@ def _run_isolated_case() -> None:
7686
_test_pop()
7787
_test_popall()
7888
_test_popone()
89+
_test_pop_with_default()
7990
_test_del()
8091

8192

0 commit comments

Comments
 (0)