Skip to content

Commit 54b7602

Browse files
authored
fix(curl-impersonate): treat a refused CONNECT tunnel as a proxy error (#2109)
`curl-cffi` 0.16.0 broke two things in `CurlImpersonateHttpClient`, which is why #2107 capped it. This fixes both and drops the cap. 1. Bundled libcurl went 8.15.0 -> 8.21.0, so a refused CONNECT tunnel is now `CURLE_COULDNT_CONNECT` with `CONNECT tunnel failed` instead of `CURLE_RECV_ERROR`. `curl_cffi` maps only the latter to `CurlProxyError`, so `_is_proxy_error` stopped recognizing it and sessions were not rotated. The needle lives in `_curl_impersonate.py` rather than the shared `ROTATE_PROXY_ERRORS`, which mirrors the JS Crawlee list - impit and httpx never produce libcurl messages. 2. The session cookie write path moved to `Cookies.update_cookies_from_curl_changes`, leaving the `_EmptyCookies` override inert. Isolation held (`get_cookies_for_curl` is still overridden, so nothing was ever sent), but every response cookie accumulated in the jar of the per-proxy session cached for the client's lifetime. Measured with the same 25-request repro: 25 cookies on 0.16.0, 0 on 0.15.0. The existing `test_crawl_with_proxy_disabled[curl]` and `test_send_request_with_proxy_disabled[curl]` cover the first; `test_internal_session_cookie_jar_stays_empty` is new for the second. Closes: #2111 *✍️ Drafted by Claude Code*
1 parent a1b3c83 commit 54b7602

4 files changed

Lines changed: 64 additions & 28 deletions

File tree

‎pyproject.toml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ cli = [
6767
"rich>=13.9.0",
6868
"typer>=0.26.0",
6969
]
70-
# TODO: Remove the upper bound, see #2108.
71-
curl-impersonate = ["curl-cffi>=0.9.0,<0.16.0"]
70+
curl-impersonate = ["curl-cffi>=0.9.0"]
7271
httpx = ["httpx[brotli,http2,zstd]>=0.27.0", "apify_fingerprint_datapoints>=0.0.2", "browserforge>=1.2.3"]
7372
parsel = ["parsel>=1.10.0"]
7473
playwright = ["playwright>=1.27.0", "apify_fingerprint_datapoints>=0.0.2", "browserforge>=1.2.3"]

‎src/crawlee/http_clients/_curl_impersonate.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
from crawlee.statistics import Statistics
4040

4141

42+
_CURL_PROXY_ERRORS = [
43+
# Raised as a plain `ConnectionError` by `curl_cffi`, which maps only some tunnel failures to `CurlProxyError`.
44+
'CONNECT tunnel failed',
45+
]
46+
"""Content of `libcurl` proxy errors that `curl_cffi` does not report as `CurlProxyError`."""
47+
48+
4249
class _EmptyCookies(CurlCookies):
4350
@override
4451
def get_cookies_for_curl(self, request: CurlRequest) -> list[CurlMorsel]:
@@ -48,6 +55,11 @@ def get_cookies_for_curl(self, request: CurlRequest) -> list[CurlMorsel]:
4855
def update_cookies_from_curl(self, morsels: list[CurlMorsel]) -> None:
4956
return None
5057

58+
@override
59+
def update_cookies_from_curl_changes(self, changes: list[bytes]) -> None:
60+
# The write path `curl_cffi` uses since 0.16.0; the one above is kept for older versions.
61+
return None
62+
5163

5264
class _AsyncSession(AsyncSession):
5365
@override
@@ -347,7 +359,7 @@ def _is_proxy_error(error: CurlRequestError) -> bool:
347359
Check if the error message contains known proxy-related error keywords or if it is an instance
348360
of `CurlProxyError`.
349361
"""
350-
if any(needle in str(error) for needle in ROTATE_PROXY_ERRORS):
362+
if any(needle in str(error) for needle in (*ROTATE_PROXY_ERRORS, *_CURL_PROXY_ERRORS)):
351363
return True
352364

353365
if isinstance(error, CurlProxyError): # noqa: SIM103
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from crawlee.http_clients import CurlImpersonateHttpClient
6+
from crawlee.sessions import Session
7+
8+
if TYPE_CHECKING:
9+
from yarl import URL
10+
11+
12+
async def test_internal_session_cookie_jar_stays_empty(server_url: URL) -> None:
13+
"""Test that response cookies never accumulate in the jar of the internally cached `curl_cffi` session."""
14+
session = Session()
15+
client = CurlImpersonateHttpClient()
16+
17+
async with client:
18+
for index in range(5):
19+
url = server_url.with_path('set_cookies').extend_query(**{f'c{index}': index})
20+
await client.send_request(str(url), session=session)
21+
22+
curl_session = client._client_by_proxy_url[None]
23+
assert list(curl_session._cookies.jar) == []
24+
25+
# The cookies are read from the `curl` handle, so they still reach the crawlee session.
26+
assert {cookie['name'] for cookie in session.cookies} == {f'c{index}' for index in range(5)}

‎uv.lock‎

Lines changed: 24 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)