Skip to content

Commit ca060c9

Browse files
committed
fix(playwright): dispose of APIResponse body
Dispose of Playwright's APIResponse body, otherwise it stays in memory until the context closes. If not done, this can lead to excessive memory usage during crawling when using `send_request` within a handler.
1 parent d4c51f8 commit ca060c9

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/crawlee/crawlers/_playwright/_types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from dataclasses import dataclass
44
from typing import TYPE_CHECKING, Literal, Protocol, TypedDict
55

6+
from playwright.async_api import APIResponse, Response
7+
68
from crawlee import HttpHeaders
79
from crawlee._utils.docs import docs_group
810

911
if TYPE_CHECKING:
1012
from collections.abc import AsyncGenerator
1113

12-
from playwright.async_api import APIResponse, Response
1314
from typing_extensions import NotRequired, Self
1415

1516

@@ -56,6 +57,9 @@ async def from_playwright_response(cls, response: Response | APIResponse, protoc
5657
# Used http protocol version cannot be obtained from `Response` and has to be passed as additional argument.
5758
http_version = protocol
5859
_content = await response.body()
60+
# If not called then the body will stay in memory until the context closes.
61+
if isinstance(response, APIResponse):
62+
await response.dispose()
5963

6064
return cls(http_version=http_version, status_code=status_code, headers=headers, _content=_content)
6165

0 commit comments

Comments
 (0)