|
2 | 2 |
|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
| 5 | +import gc |
5 | 6 | import os
|
6 | 7 | import json
|
7 | 8 | import asyncio
|
8 | 9 | import inspect
|
| 10 | +import tracemalloc |
9 | 11 | from typing import Any, Union, cast
|
10 | 12 | from unittest import mock
|
11 | 13 |
|
@@ -194,6 +196,67 @@ def test_copy_signature(self) -> None:
|
194 | 196 | copy_param = copy_signature.parameters.get(name)
|
195 | 197 | assert copy_param is not None, f"copy() signature is missing the {name} param"
|
196 | 198 |
|
| 199 | + def test_copy_build_request(self) -> None: |
| 200 | + options = FinalRequestOptions(method="get", url="/foo") |
| 201 | + |
| 202 | + def build_request(options: FinalRequestOptions) -> None: |
| 203 | + client = self.client.copy() |
| 204 | + client._build_request(options) |
| 205 | + |
| 206 | + # ensure that the machinery is warmed up before tracing starts. |
| 207 | + build_request(options) |
| 208 | + gc.collect() |
| 209 | + |
| 210 | + tracemalloc.start(1000) |
| 211 | + |
| 212 | + snapshot_before = tracemalloc.take_snapshot() |
| 213 | + |
| 214 | + ITERATIONS = 10 |
| 215 | + for _ in range(ITERATIONS): |
| 216 | + build_request(options) |
| 217 | + gc.collect() |
| 218 | + |
| 219 | + snapshot_after = tracemalloc.take_snapshot() |
| 220 | + |
| 221 | + tracemalloc.stop() |
| 222 | + |
| 223 | + def add_leak(leaks: list[tracemalloc.StatisticDiff], diff: tracemalloc.StatisticDiff) -> None: |
| 224 | + if diff.count == 0: |
| 225 | + # Avoid false positives by considering only leaks (i.e. allocations that persist). |
| 226 | + return |
| 227 | + |
| 228 | + if diff.count % ITERATIONS != 0: |
| 229 | + # Avoid false positives by considering only leaks that appear per iteration. |
| 230 | + return |
| 231 | + |
| 232 | + for frame in diff.traceback: |
| 233 | + if any( |
| 234 | + frame.filename.endswith(fragment) |
| 235 | + for fragment in [ |
| 236 | + # to_raw_response_wrapper leaks through the @functools.wraps() decorator. |
| 237 | + # |
| 238 | + # removing the decorator fixes the leak for reasons we don't understand. |
| 239 | + "lithic/_response.py", |
| 240 | + # pydantic.BaseModel.model_dump || pydantic.BaseModel.dict leak memory for some reason. |
| 241 | + "lithic/_compat.py", |
| 242 | + # Standard library leaks we don't care about. |
| 243 | + "/logging/__init__.py", |
| 244 | + ] |
| 245 | + ): |
| 246 | + return |
| 247 | + |
| 248 | + leaks.append(diff) |
| 249 | + |
| 250 | + leaks: list[tracemalloc.StatisticDiff] = [] |
| 251 | + for diff in snapshot_after.compare_to(snapshot_before, "traceback"): |
| 252 | + add_leak(leaks, diff) |
| 253 | + if leaks: |
| 254 | + for leak in leaks: |
| 255 | + print("MEMORY LEAK:", leak) |
| 256 | + for frame in leak.traceback: |
| 257 | + print(frame) |
| 258 | + raise AssertionError() |
| 259 | + |
197 | 260 | def test_request_timeout(self) -> None:
|
198 | 261 | request = self.client._build_request(FinalRequestOptions(method="get", url="/foo"))
|
199 | 262 | timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore
|
@@ -953,6 +1016,67 @@ def test_copy_signature(self) -> None:
|
953 | 1016 | copy_param = copy_signature.parameters.get(name)
|
954 | 1017 | assert copy_param is not None, f"copy() signature is missing the {name} param"
|
955 | 1018 |
|
| 1019 | + def test_copy_build_request(self) -> None: |
| 1020 | + options = FinalRequestOptions(method="get", url="/foo") |
| 1021 | + |
| 1022 | + def build_request(options: FinalRequestOptions) -> None: |
| 1023 | + client = self.client.copy() |
| 1024 | + client._build_request(options) |
| 1025 | + |
| 1026 | + # ensure that the machinery is warmed up before tracing starts. |
| 1027 | + build_request(options) |
| 1028 | + gc.collect() |
| 1029 | + |
| 1030 | + tracemalloc.start(1000) |
| 1031 | + |
| 1032 | + snapshot_before = tracemalloc.take_snapshot() |
| 1033 | + |
| 1034 | + ITERATIONS = 10 |
| 1035 | + for _ in range(ITERATIONS): |
| 1036 | + build_request(options) |
| 1037 | + gc.collect() |
| 1038 | + |
| 1039 | + snapshot_after = tracemalloc.take_snapshot() |
| 1040 | + |
| 1041 | + tracemalloc.stop() |
| 1042 | + |
| 1043 | + def add_leak(leaks: list[tracemalloc.StatisticDiff], diff: tracemalloc.StatisticDiff) -> None: |
| 1044 | + if diff.count == 0: |
| 1045 | + # Avoid false positives by considering only leaks (i.e. allocations that persist). |
| 1046 | + return |
| 1047 | + |
| 1048 | + if diff.count % ITERATIONS != 0: |
| 1049 | + # Avoid false positives by considering only leaks that appear per iteration. |
| 1050 | + return |
| 1051 | + |
| 1052 | + for frame in diff.traceback: |
| 1053 | + if any( |
| 1054 | + frame.filename.endswith(fragment) |
| 1055 | + for fragment in [ |
| 1056 | + # to_raw_response_wrapper leaks through the @functools.wraps() decorator. |
| 1057 | + # |
| 1058 | + # removing the decorator fixes the leak for reasons we don't understand. |
| 1059 | + "lithic/_response.py", |
| 1060 | + # pydantic.BaseModel.model_dump || pydantic.BaseModel.dict leak memory for some reason. |
| 1061 | + "lithic/_compat.py", |
| 1062 | + # Standard library leaks we don't care about. |
| 1063 | + "/logging/__init__.py", |
| 1064 | + ] |
| 1065 | + ): |
| 1066 | + return |
| 1067 | + |
| 1068 | + leaks.append(diff) |
| 1069 | + |
| 1070 | + leaks: list[tracemalloc.StatisticDiff] = [] |
| 1071 | + for diff in snapshot_after.compare_to(snapshot_before, "traceback"): |
| 1072 | + add_leak(leaks, diff) |
| 1073 | + if leaks: |
| 1074 | + for leak in leaks: |
| 1075 | + print("MEMORY LEAK:", leak) |
| 1076 | + for frame in leak.traceback: |
| 1077 | + print(frame) |
| 1078 | + raise AssertionError() |
| 1079 | + |
956 | 1080 | async def test_request_timeout(self) -> None:
|
957 | 1081 | request = self.client._build_request(FinalRequestOptions(method="get", url="/foo"))
|
958 | 1082 | timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore
|
|
0 commit comments