-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_leak_thread.py
77 lines (56 loc) · 1.41 KB
/
memory_leak_thread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from __future__ import annotations
import contextlib
import dataclasses
import secrets
import sys
import threading
import time
import tracemalloc
from typing import Any
from bencode2 import BencodeDecodeError, bdecode, bencode
@dataclasses.dataclass(frozen=True, slots=True)
class DC:
a: int
b: str
c: bool
d: list[Any]
large_binary = b" " * (100 * 1024 * 1024 + 10)
def w() -> None:
C = type("C", (object,), {})
bdecode(bencode(1844674407370955161600))
bencode(large_binary)
bdecode(bencode(DC(a=1, b="ss", c=True, d=[{"a": 1, "b": b"bb"}, (1, 2, 3)])))
try:
bdecode(s)
except BencodeDecodeError:
pass
with contextlib.suppress(TypeError):
bencode(DC(a=1, b="ss", c=True, d=[None]))
try:
bencode([1, 2, "a", b"b", None])
except TypeError:
pass
try:
bencode([1, 2, "a", b"b", C()])
except TypeError:
pass
try:
bencode({"0": s, "2": [True, C()], "3": None})
bencode({"1": C()})
except TypeError:
pass
def encode_in_thread() -> None:
t = threading.Thread(target=w)
t.start()
t.join()
tracemalloc.start()
while True:
s = b"100:" + secrets.token_bytes(10)
for c in [i for i in range(5000)]:
encode_in_thread()
# gc.collect()
v = tracemalloc.get_tracemalloc_memory()
print(v)
if v > 10610992:
time.sleep(1000)
sys.exit(1)