Skip to content

Commit

Permalink
Add test for raw copy memleak
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist committed Oct 13, 2024
1 parent d2a8288 commit 15a62bf
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/test_raw.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import operator
import subprocess
import sys
import textwrap
import weakref

import pytest
Expand Down Expand Up @@ -69,6 +71,29 @@ def test_raw_copy():
assert ref() is None


def test_raw_copy_doesnt_leak():
"""See https://github.com/jcrist/msgspec/pull/709"""
script = textwrap.dedent(
"""
import msgspec
import tracemalloc
tracemalloc.start()
raw = msgspec.Raw(bytearray(1000))
for _ in range(10000):
raw.copy()
_, peak = tracemalloc.get_traced_memory()
print(peak)
"""
)

output = subprocess.check_output([sys.executable, "-c", script])
peak = int(output.decode().strip())
assert peak < 10_000 # should really be ~2000


def test_raw_pickle_bytes():
orig_buffer = b"test"
r = msgspec.Raw(orig_buffer)
Expand Down

0 comments on commit 15a62bf

Please sign in to comment.