From fd432cea2ffcabb549dfdc563db825466e19cedf Mon Sep 17 00:00:00 2001 From: John Speaks Date: Thu, 19 Nov 2026 23:00:16 +0000 Subject: [PATCH 1/8] Reuse pooled HTTP connections for workspace calls Co-authored-by: Isaac --- src/ucode/databricks.py | 132 ++++++++++++++++- tests/test_databricks.py | 226 +++++++++++++++++++++++++---- tests/test_managed_config.py | 2 +- tests/test_mcp_connection_login.py | 8 +- tests/test_mcp_proxy.py | 2 +- tests/test_workspace_selection.py | 8 +- 6 files changed, 336 insertions(+), 42 deletions(-) diff --git a/src/ucode/databricks.py b/src/ucode/databricks.py index 66032fb34..8ec1b5561 100644 --- a/src/ucode/databricks.py +++ b/src/ucode/databricks.py @@ -5,6 +5,7 @@ import configparser import functools +import io import json import logging import logging.handlers @@ -15,6 +16,7 @@ import re import shlex import shutil +import ssl import subprocess import sys import tempfile @@ -27,12 +29,16 @@ from email.message import Message from enum import Enum from http.client import HTTPException +from http.cookiejar import CookieJar, DefaultCookiePolicy from pathlib import Path -from typing import Any, Literal, NamedTuple, NoReturn, TypeGuard, cast, overload +from typing import TYPE_CHECKING, Any, Literal, NamedTuple, NoReturn, TypeGuard, cast, overload from urllib import error as urllib_error from urllib import request as urllib_request from urllib.parse import quote, urlencode, urlparse +if TYPE_CHECKING: + import httpx + from ucode import metrics, tls from ucode.codex_config import catalog_slugs from ucode.config_io import APP_DIR @@ -376,6 +382,126 @@ def clear_workspace_org_id_cache() -> None: _WORKSPACE_ORG_IDS.clear() +class _RejectCookiesPolicy(DefaultCookiePolicy): + def set_ok(self, cookie, request) -> bool: + return False + + +_HTTP_CLIENT: httpx.Client | None = None +_HTTP_CLIENT_PID: int | None = None +_HTTP_CLIENT_LOCK = threading.Lock() + + +def _reset_http_client_after_fork() -> None: + global _HTTP_CLIENT, _HTTP_CLIENT_PID, _HTTP_CLIENT_LOCK + _HTTP_CLIENT = None + _HTTP_CLIENT_PID = os.getpid() + _HTTP_CLIENT_LOCK = threading.Lock() + + +if hasattr(os, "register_at_fork"): + os.register_at_fork(after_in_child=_reset_http_client_after_fork) + + +def _get_http_client() -> httpx.Client: + import httpx + + global _HTTP_CLIENT, _HTTP_CLIENT_PID + pid = os.getpid() + if _HTTP_CLIENT is not None and _HTTP_CLIENT_PID == pid: + return _HTTP_CLIENT + with _HTTP_CLIENT_LOCK: + if _HTTP_CLIENT is None or _HTTP_CLIENT_PID != pid: + # Before building the SSL context, so the pooled client verifies against the same + # trust store (os or python) as the urllib path did. + tls.apply() + client = httpx.Client( + verify=ssl.create_default_context(), + trust_env=True, + follow_redirects=True, + limits=httpx.Limits(max_connections=100, max_keepalive_connections=20), + cookies=CookieJar(policy=_RejectCookiesPolicy()), + ) + _HTTP_CLIENT = client + _HTTP_CLIENT_PID = pid + return _HTTP_CLIENT + + +class _PooledResponse: + def __init__(self, response: httpx.Response): + self.status = response.status_code + self.headers = Message() + for name, value in response.headers.multi_items(): + self.headers.add_header(name, value) + self._body = response.content + self._url = str(response.url) + + def __enter__(self): + return self + + def __exit__(self, *exc): + return False + + def geturl(self) -> str: + return self._url + + def read(self) -> bytes: + return self._body + + +def _http_open(request: urllib_request.Request, *, timeout: float) -> _PooledResponse: + import httpx + + request_headers = dict(request.header_items()) + header_names = {name.casefold() for name in request_headers} + if "accept-encoding" not in header_names: + request_headers["Accept-Encoding"] = "identity" + if "connection" not in header_names: + request_headers["Connection"] = "keep-alive" + if "user-agent" not in header_names: + request_headers["User-Agent"] = f"Python-urllib/{urllib_request.__version__}" + httpx_request = httpx.Request( + request.get_method(), + request.full_url, + headers=request_headers, + content=request.data, + extensions={"timeout": httpx.Timeout(timeout).as_dict()}, + ) + try: + response = _get_http_client().send(httpx_request, follow_redirects=True) + except httpx.RequestError as exc: + pending = [exc] + seen: set[int] = set() + reason: BaseException = exc + while pending: + current = pending.pop() + if id(current) in seen: + continue + seen.add(id(current)) + if isinstance(current, OSError): + reason = current + break + reason = current + if isinstance(current, BaseExceptionGroup): + pending.extend(current.exceptions) + if current.__cause__ is not None: + pending.append(current.__cause__) + elif current.__context__ is not None: + pending.append(current.__context__) + raise urllib_error.URLError(reason) from exc + + pooled_response = _PooledResponse(response) + if not 200 <= response.status_code < 300: + raise urllib_error.HTTPError( + pooled_response.geturl(), + response.status_code, + response.reason_phrase, + pooled_response.headers, + io.BytesIO(pooled_response.read()), + ) + return pooled_response + + @metrics.http_result def _http_get_bytes( url: str, @@ -406,7 +532,7 @@ def _http_get_bytes( if attempt: metrics.record(metrics.HTTP_RETRY_COUNT, 1, api_operation=operation, http_method="GET") try: - with urllib_request.urlopen(request, timeout=timeout) as response: + with _http_open(request, timeout=timeout) as response: status = response.status body = response.read() failure = _workspace_response_failure(url, response) @@ -537,7 +663,7 @@ def _http_send_json( request_started = time.perf_counter() status = None try: - with urllib_request.urlopen(request, timeout=timeout) as response: + with _http_open(request, timeout=timeout) as response: status = response.status body = response.read().decode("utf-8") failure = _workspace_response_failure(url, response) diff --git a/tests/test_databricks.py b/tests/test_databricks.py index d1e428a5d..1d17dbb9f 100644 --- a/tests/test_databricks.py +++ b/tests/test_databricks.py @@ -225,8 +225,8 @@ def __init__(self, payload: dict, headers: dict): class TestWorkspaceOrgId: def _stub_response(self, monkeypatch, headers: dict) -> None: monkeypatch.setattr( - db_mod.urllib_request, - "urlopen", + db_mod, + "_http_open", lambda request, timeout=None: _FakeResponseWithHeaders({"ok": True}, headers), ) @@ -4715,7 +4715,7 @@ def fake_urlopen(request, timeout=None): raise outcome return outcome - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) monkeypatch.setattr(db_mod.time, "sleep", sleeps.append) assert db_mod.probe_unity_gateway_capabilities(WS, "fake-token") == db_mod.GatewayProbe( @@ -4764,7 +4764,7 @@ def fake_urlopen(request, timeout=None): calls.append(request) raise TestHttpGetJsonRetries._http_error(499, "Unknown") - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) monkeypatch.setattr(db_mod.time, "sleep", lambda s: None) with pytest.raises(RuntimeError, match="Could not verify") as excinfo: @@ -4811,7 +4811,7 @@ def test_reason_includes_body_on_http_error(self): from ucode.databricks import _http_get_json exc = self._http_error(400, "Bad Request", body="Invalid Token") - with patch("ucode.databricks.urllib_request.urlopen", side_effect=exc): + with patch("ucode.databricks._http_open", side_effect=exc): payload, reason = _http_get_json("https://x/y", "tok") assert payload is None assert "HTTP 400" in reason @@ -4823,7 +4823,7 @@ def test_reason_without_body_is_status_only(self): from ucode.databricks import _http_get_json exc = self._http_error(404, "Not Found") - with patch("ucode.databricks.urllib_request.urlopen", side_effect=exc): + with patch("ucode.databricks._http_open", side_effect=exc): payload, reason = _http_get_json("https://x/y", "tok") assert payload is None assert reason == "HTTP 404 Not Found" @@ -4853,7 +4853,7 @@ def test_truncated_response_obeys_retry_budget( responses = [truncated, _FakeResponse({"complete": True})] if recovers else None urlopen = MagicMock(side_effect=responses, return_value=truncated) sleeps = [] - monkeypatch.setattr(db_mod.urllib_request, "urlopen", urlopen) + monkeypatch.setattr(db_mod, "_http_open", urlopen) monkeypatch.setattr(db_mod.random, "uniform", lambda low, high: high) monkeypatch.setattr(db_mod.time, "sleep", sleeps.append) @@ -4880,7 +4880,7 @@ def test_retries_protocol_errors_before_response_body(self, monkeypatch, error_t _FakeResponse({"complete": True}), ] ) - monkeypatch.setattr(db_mod.urllib_request, "urlopen", urlopen) + monkeypatch.setattr(db_mod, "_http_open", urlopen) monkeypatch.setattr(db_mod.time, "sleep", lambda delay: None) payload, reason = db_mod._http_get_json("https://x/y", "tok", max_retries=1) @@ -4906,7 +4906,7 @@ def fake_urlopen(request, timeout=None): raise outcome return outcome - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) monkeypatch.setattr(db_mod.random, "uniform", lambda low, high: high) monkeypatch.setattr(db_mod.time, "sleep", sleeps.append) @@ -4929,7 +4929,7 @@ def fake_urlopen(request, timeout=None): raise outcome return outcome - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) monkeypatch.setattr(db_mod.random, "uniform", lambda low, high: high) monkeypatch.setattr(db_mod.time, "sleep", sleeps.append) @@ -4952,7 +4952,7 @@ def fake_urlopen(request, timeout=None): raise outcome return outcome - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) monkeypatch.setattr(db_mod.random, "uniform", lambda low, high: high) monkeypatch.setattr(db_mod.time, "sleep", sleeps.append) @@ -4982,7 +4982,7 @@ def fake_urlopen(request, timeout=None): calls.append(request) raise self._http_error(code, "Nope") - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) monkeypatch.setattr(db_mod.time, "sleep", sleeps.append) payload, reason = db_mod._http_get_json("https://x/y", "tok", max_retries=2) @@ -5000,7 +5000,7 @@ def fake_urlopen(request, timeout=None): calls.append(request) raise self._http_error(429, "Too Many Requests") - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) monkeypatch.setattr(db_mod.random, "uniform", lambda low, high: high) monkeypatch.setattr(db_mod.time, "sleep", sleeps.append) @@ -5018,7 +5018,7 @@ def fake_urlopen(request, timeout=None): calls.append(request) raise self._http_error(500, "Internal Server Error") - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) monkeypatch.setattr( db_mod.time, "sleep", @@ -5040,7 +5040,7 @@ def fake_urlopen(request, timeout=None): raise outcome return outcome - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) monkeypatch.setattr(db_mod.time, "sleep", lambda delay: None) body, reason = db_mod._http_get_bytes("https://x/y", "tok", max_retries=2) @@ -5058,7 +5058,7 @@ def fake_urlopen(request, timeout=None): requests.append(request) return _FakeResponse({}) - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) db_mod._http_get_json(f"{WS}/get", "tok") db_mod._http_post_json(f"{WS}/post", "tok", {}) @@ -5076,7 +5076,7 @@ def fake_urlopen(request, timeout=None): requests.append(request) return _FakeResponse({}) - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) db_mod._http_get_json(f"{WS}/get", "tok") db_mod._http_post_json(f"{WS}/post", "tok", {}) @@ -5809,7 +5809,7 @@ def test_read_timeout_returns_reason_instead_of_raising(self, monkeypatch): def raise_timeout(request, timeout=None): raise TimeoutError("The read operation timed out") - monkeypatch.setattr(db_mod.urllib_request, "urlopen", raise_timeout) + monkeypatch.setattr(db_mod, "_http_open", raise_timeout) payload, reason = db_mod._http_get_json(f"{WS}/api/2.0/anything", "tok") @@ -5821,7 +5821,7 @@ def test_post_read_timeout_returns_reason_instead_of_raising(self, monkeypatch): def raise_timeout(request, timeout=None): raise TimeoutError("The read operation timed out") - monkeypatch.setattr(db_mod.urllib_request, "urlopen", raise_timeout) + monkeypatch.setattr(db_mod, "_http_open", raise_timeout) payload, reason = db_mod._http_post_json(f"{WS}/api/2.0/anything", "tok", {"k": "v"}) @@ -5833,7 +5833,7 @@ def test_bytes_read_timeout_returns_reason_instead_of_raising(self, monkeypatch) def raise_timeout(request, timeout=None): raise TimeoutError("The read operation timed out") - monkeypatch.setattr(db_mod.urllib_request, "urlopen", raise_timeout) + monkeypatch.setattr(db_mod, "_http_open", raise_timeout) body, reason = db_mod._http_get_bytes(f"{WS}/api/2.0/fs/files/anything", "tok") @@ -6172,7 +6172,7 @@ def _empty_response(body: str = ""): def test_empty_body_is_success_not_a_decode_error(self, monkeypatch, metrics_recorder): # Without `allow_empty_body` this would fail with "response was not valid JSON". monkeypatch.setattr( - db_mod.urllib_request, "urlopen", lambda request, timeout=None: self._empty_response() + db_mod, "_http_open", lambda request, timeout=None: self._empty_response() ) payload, reason = db_mod._http_delete(f"{WS}/api/anything", "tok") assert reason is None @@ -6186,8 +6186,8 @@ def test_empty_body_is_success_not_a_decode_error(self, monkeypatch, metrics_rec def test_empty_json_object_is_also_success(self, monkeypatch): monkeypatch.setattr( - db_mod.urllib_request, - "urlopen", + db_mod, + "_http_open", lambda request, timeout=None: self._empty_response("{}"), ) payload, reason = db_mod._http_delete(f"{WS}/api/anything", "tok") @@ -6202,7 +6202,7 @@ def capture(request, timeout=None): seen["data"] = request.data return self._empty_response() - monkeypatch.setattr(db_mod.urllib_request, "urlopen", capture) + monkeypatch.setattr(db_mod, "_http_open", capture) db_mod._http_delete(f"{WS}/api/anything", "tok") assert seen["method"] == "DELETE" assert seen["data"] is None @@ -6219,7 +6219,7 @@ def raise_http_error(request, timeout=None): url="", code=403, msg="Forbidden", hdrs=MagicMock(), fp=io.BytesIO(body.encode()) ) - monkeypatch.setattr(db_mod.urllib_request, "urlopen", raise_http_error) + monkeypatch.setattr(db_mod, "_http_open", raise_http_error) _, reason = db_mod._http_delete(f"{WS}/api/anything", "tok") assert reason is not None assert "403" in reason @@ -6251,7 +6251,7 @@ def capture(request, timeout=None): response.status = 200 return response - monkeypatch.setattr(db_mod.urllib_request, "urlopen", capture) + monkeypatch.setattr(db_mod, "_http_open", capture) payload, reason = db_mod._http_patch_json(f"{WS}/api/anything", "tok", {"k": "v"}) assert reason is None assert payload == {"name": "coding-agent-configs/x"} @@ -6279,7 +6279,7 @@ def capture(request, timeout=None): response.status = 200 return response - monkeypatch.setattr(db_mod.urllib_request, "urlopen", capture) + monkeypatch.setattr(db_mod, "_http_open", capture) payload, reason = db_mod._http_post_json( f"{WS}/api/anything", "tok", {"k": "v"}, headers={"User-Agent": "ucode/1.2.3"} ) @@ -6406,7 +6406,7 @@ def urlopen(request, timeout): requests.append((request, timeout)) return _FakeResponse({"dashboard_id": "dashboard", "dashboard_url": dashboard_url}) - monkeypatch.setattr(db_mod.urllib_request, "urlopen", urlopen) + monkeypatch.setattr(db_mod, "_http_open", urlopen) assert db_mod.get_usage_dashboard_url(WS, "workspace-token") == dashboard_url assert len(requests) == 1 @@ -7350,3 +7350,173 @@ def test_model_service_backing_model_reads_the_first_live_foundation_model(): assert model_service_backing_model(service) == "system.ai.databricks-claude-sonnet-4-6" assert model_service_backing_model({"config": {}}) is None + + +def test_http_get_reuses_keep_alive_connection(monkeypatch): + from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + + class CountingServer(ThreadingHTTPServer): + daemon_threads = True + + def __init__(self, address, handler): + self.connection_count = 0 + self.connection_count_lock = threading.Lock() + super().__init__(address, handler) + + def get_request(self): + request, address = super().get_request() + with self.connection_count_lock: + self.connection_count += 1 + return request, address + + class Handler(BaseHTTPRequestHandler): + protocol_version = "HTTP/1.1" + + def do_GET(self): + body = b"{}" + self.send_response(200) + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def log_message(self, *args): + pass + + for name in ( + "HTTP_PROXY", + "http_proxy", + "HTTPS_PROXY", + "https_proxy", + "ALL_PROXY", + "all_proxy", + ): + monkeypatch.delenv(name, raising=False) + server = CountingServer(("127.0.0.1", 0), Handler) + thread = threading.Thread(target=server.serve_forever, daemon=True) + thread.start() + try: + url = f"http://127.0.0.1:{server.server_port}/api" + for _ in range(2): + body, reason = db_mod._http_get_bytes(url, "token") + assert body == b"{}" + assert reason is None + assert server.connection_count == 1 + finally: + server.shutdown() + server.server_close() + thread.join() + + +def test_http_transport_preserves_status_errors(monkeypatch): + from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + + class Handler(BaseHTTPRequestHandler): + protocol_version = "HTTP/1.1" + + def do_GET(self): + if self.path == "/bad": + status, body = 400, b"Invalid Token" + elif self.path == "/redirect": + status, body = 302, b"redirect" + else: + status, body = 503, b"unavailable" + self.send_response(status) + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def log_message(self, *args): + pass + + for name in ( + "HTTP_PROXY", + "http_proxy", + "HTTPS_PROXY", + "https_proxy", + "ALL_PROXY", + "all_proxy", + ): + monkeypatch.delenv(name, raising=False) + server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + thread = threading.Thread(target=server.serve_forever, daemon=True) + thread.start() + try: + url = f"http://127.0.0.1:{server.server_port}" + _, reason = db_mod._http_get_bytes(f"{url}/bad", "token") + assert reason == "HTTP 400 Bad Request: Invalid Token" + _, reason = db_mod._http_get_bytes(f"{url}/redirect", "token") + assert reason == "HTTP 302 Found: redirect" + _, reason = db_mod._http_get_bytes(f"{url}/failure", "token") + assert reason == "HTTP 503 Service Unavailable: unavailable" + finally: + server.shutdown() + server.server_close() + thread.join() + + +def test_http_transport_maps_refused_connections_to_network_errors(monkeypatch): + import socket + + for name in ( + "HTTP_PROXY", + "http_proxy", + "HTTPS_PROXY", + "https_proxy", + "ALL_PROXY", + "all_proxy", + ): + monkeypatch.delenv(name, raising=False) + with socket.socket() as listener: + listener.bind(("127.0.0.1", 0)) + port = listener.getsockname()[1] + _, reason = db_mod._http_get_bytes(f"http://127.0.0.1:{port}/api", "token") + assert reason is not None + assert reason.startswith("network error:") + assert "Connection refused" in reason + + +def test_http_transport_passes_headers_and_timeout(monkeypatch): + import httpx + + captured = {} + + class Client: + def send(self, request, *, follow_redirects): + captured.update(request=request, follow_redirects=follow_redirects) + return httpx.Response(200, content=b"{}", request=request) + + monkeypatch.setattr(db_mod, "_get_http_client", lambda: Client()) + body, reason = db_mod._http_get_bytes( + "https://example.test/api", "token", timeout=1.75, headers={"X-Transport-Test": "yes"} + ) + assert body == b"{}" + assert reason is None + request = captured["request"] + assert request.extensions["timeout"] == httpx.Timeout(1.75).as_dict() + assert request.method == "GET" + assert str(request.url) == "https://example.test/api" + assert captured["follow_redirects"] is True + headers = dict(request.headers) + assert headers["authorization"] == "Bearer token" + assert headers["x-transport-test"] == "yes" + assert headers["accept-encoding"] == "identity" + assert headers["connection"] == "keep-alive" + assert headers["user-agent"] == f"Python-urllib/{db_mod.urllib_request.__version__}" + assert "accept" not in headers + + +def test_after_fork_hook_replaces_parent_http_client(monkeypatch): + parent_pid = os.getpid() + parent_client = db_mod._get_http_client() + monkeypatch.setattr(db_mod, "_HTTP_CLIENT", parent_client) + monkeypatch.setattr(db_mod, "_HTTP_CLIENT_PID", parent_pid) + monkeypatch.setattr(db_mod, "_HTTP_CLIENT_LOCK", db_mod._HTTP_CLIENT_LOCK) + monkeypatch.setattr(db_mod.os, "getpid", lambda: parent_pid + 1) + + db_mod._reset_http_client_after_fork() + child_client = db_mod._get_http_client() + try: + assert child_client is not parent_client + assert db_mod._HTTP_CLIENT_PID == parent_pid + 1 + finally: + child_client.close() diff --git a/tests/test_managed_config.py b/tests/test_managed_config.py index 40964b88b..2f522d0e2 100644 --- a/tests/test_managed_config.py +++ b/tests/test_managed_config.py @@ -717,7 +717,7 @@ def fake_urlopen(request, timeout=None): captured["request"] = request return _FakeResponse() - monkeypatch.setattr(db_mod.urllib_request, "urlopen", fake_urlopen) + monkeypatch.setattr(db_mod, "_http_open", fake_urlopen) db_mod.fetch_managed_coding_agent_configs("https://ws", "tok") assert captured["request"].get_header("User-agent") == f"{expected} {client_platform()}" diff --git a/tests/test_mcp_connection_login.py b/tests/test_mcp_connection_login.py index 53ee0d3b6..d8f71d371 100644 --- a/tests/test_mcp_connection_login.py +++ b/tests/test_mcp_connection_login.py @@ -70,7 +70,7 @@ def launch(argv, **kwargs): launches.append((argv, kwargs)) return subprocess.CompletedProcess(argv, 0) - monkeypatch.setattr(db_mod.urllib_request, "urlopen", urlopen) + monkeypatch.setattr(db_mod, "_http_open", urlopen) monkeypatch.setattr(browser_cross_os.subprocess_cross_os, "run", launch) return states, requests, launches, metadata @@ -264,7 +264,7 @@ def urlopen(request, **kwargs): response.status = 200 return response - monkeypatch.setattr(db_mod.urllib_request, "urlopen", urlopen) + monkeypatch.setattr(db_mod, "_http_open", urlopen) ok, message = mcl.run_connection_login(AIGW_URL, WS, profile="selected") assert not ok and "workspace ID" in message assert not launches @@ -328,7 +328,7 @@ def test_invalid_service_does_not_launch(self, sign_in, url): def test_poll_finishing_after_deadline_never_reports_success(self, sign_in, monkeypatch): _, _, launches, _ = sign_in - original = db_mod.urllib_request.urlopen + original = db_mod._http_open timeouts = [] def slow_poll(request, **kwargs): @@ -337,7 +337,7 @@ def slow_poll(request, **kwargs): mcl.time.sleep(7) return original(request, **kwargs) - monkeypatch.setattr(db_mod.urllib_request, "urlopen", slow_poll) + monkeypatch.setattr(db_mod, "_http_open", slow_poll) ok, detail = mcl.run_connection_login(AIGW_URL, WS, profile="selected") assert not ok and "timed out" in detail assert timeouts == [6] diff --git a/tests/test_mcp_proxy.py b/tests/test_mcp_proxy.py index bb69fd0fc..90f63194e 100644 --- a/tests/test_mcp_proxy.py +++ b/tests/test_mcp_proxy.py @@ -370,7 +370,7 @@ def launch(argv, **kwargs): assert "webbrowser.open" in argv[3] return subprocess.CompletedProcess(argv, 0) - monkeypatch.setattr(db_mod.urllib_request, "urlopen", urlopen) + monkeypatch.setattr(db_mod, "_http_open", urlopen) monkeypatch.setattr(browser_cross_os.subprocess_cross_os, "run", launch) auth = mcp_proxy._build_token_auth(CONN_URL, WS, "p") sent = [] diff --git a/tests/test_workspace_selection.py b/tests/test_workspace_selection.py index ef8d7f53e..32d34c2aa 100644 --- a/tests/test_workspace_selection.py +++ b/tests/test_workspace_selection.py @@ -170,7 +170,7 @@ def urlopen(request, timeout): sent.append(request) return response - with patch.object(db_mod.urllib_request, "urlopen", side_effect=urlopen): + with patch.object(db_mod, "_http_open", side_effect=urlopen): result = db_mod._http_get_json(url, "tok") return result, sent[0] @@ -185,9 +185,7 @@ def test_usage_dashboard_uses_selected_workspace(self, selected): url = f"{SHARED}/api/ai-gateway/v2/usage-analytics-dashboard" dashboard_url = f"{SHARED}/dashboardsv3/dashboard/published?o={WSID}" body = json.dumps({"dashboard_url": dashboard_url}).encode() - with patch.object( - db_mod.urllib_request, "urlopen", return_value=_Response(body, url, WSID) - ) as urlopen: + with patch.object(db_mod, "_http_open", return_value=_Response(body, url, WSID)) as urlopen: assert db_mod.get_usage_dashboard_url(SHARED, "tok") == dashboard_url request = urlopen.call_args.args[0] @@ -227,7 +225,7 @@ class TestHelperArgv: def test_model_service_metadata_lookup(self, selected): url = f"{SHARED}/api/2.1/unity-catalog/model-services/main.team.opaque" with patch.object( - db_mod.urllib_request, "urlopen", return_value=_Response(b"{}", url, WSID) + db_mod, "_http_open", return_value=_Response(b"{}", url, WSID) ) as urlopen: assert db_mod.get_model_service("main.team.opaque", SHARED, "tok") == ({}, None) From 3176db02d9d349f5a8f8b18613e517f659006d34 Mon Sep 17 00:00:00 2001 From: John Speaks Date: Thu, 19 Nov 2026 23:14:42 +0000 Subject: [PATCH 2/8] Honor macOS system proxies in pooled client Co-authored-by: Isaac --- src/ucode/databricks.py | 65 ++++++++++++++++++++++++++++++++++++++-- tests/test_databricks.py | 32 ++++++++++++++++++++ 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/ucode/databricks.py b/src/ucode/databricks.py index 8ec1b5561..a439d33b2 100644 --- a/src/ucode/databricks.py +++ b/src/ucode/databricks.py @@ -387,6 +387,56 @@ def set_ok(self, cookie, request) -> bool: return False +def _macos_system_proxies() -> dict[str, str] | None: + if sys.platform != "darwin" or any( + os.environ.get(name) + for name in ( + "HTTP_PROXY", + "http_proxy", + "HTTPS_PROXY", + "https_proxy", + "ALL_PROXY", + "all_proxy", + ) + ): + return None + proxies = urllib_request.getproxies() + system_proxies = { + scheme: proxy for scheme in ("http", "https", "all") if (proxy := proxies.get(scheme)) + } + return system_proxies or None + + +def _urllib_proxy_for_request(scheme: str, host: str, proxies: dict[str, str]) -> str | None: + if urllib_request.proxy_bypass(host): + return None + return proxies.get(scheme) or proxies.get("all") + + +def _macos_proxy_transport(httpx, proxies, verify, limits): + class SystemProxyTransport(httpx.BaseTransport): + def __init__(self): + self.direct = httpx.HTTPTransport(verify=verify, trust_env=False, limits=limits) + self.proxied = { + proxy: httpx.HTTPTransport( + proxy=proxy, verify=verify, trust_env=False, limits=limits + ) + for proxy in set(proxies.values()) + } + + def handle_request(self, request): + proxy = _urllib_proxy_for_request(request.url.scheme, request.url.host, proxies) + transport = self.proxied.get(proxy) if proxy else self.direct + return transport.handle_request(request) + + def close(self): + self.direct.close() + for transport in self.proxied.values(): + transport.close() + + return SystemProxyTransport() + + _HTTP_CLIENT: httpx.Client | None = None _HTTP_CLIENT_PID: int | None = None _HTTP_CLIENT_LOCK = threading.Lock() @@ -415,12 +465,21 @@ def _get_http_client() -> httpx.Client: # Before building the SSL context, so the pooled client verifies against the same # trust store (os or python) as the urllib path did. tls.apply() + verify = ssl.create_default_context() + limits = httpx.Limits(max_connections=100, max_keepalive_connections=20) + system_proxies = _macos_system_proxies() + transport = ( + _macos_proxy_transport(httpx, system_proxies, verify, limits) + if system_proxies + else None + ) client = httpx.Client( - verify=ssl.create_default_context(), - trust_env=True, + verify=verify, + trust_env=transport is None, follow_redirects=True, - limits=httpx.Limits(max_connections=100, max_keepalive_connections=20), + limits=limits, cookies=CookieJar(policy=_RejectCookiesPolicy()), + **({"transport": transport} if transport is not None else {}), ) _HTTP_CLIENT = client _HTTP_CLIENT_PID = pid diff --git a/tests/test_databricks.py b/tests/test_databricks.py index 1d17dbb9f..97969ec35 100644 --- a/tests/test_databricks.py +++ b/tests/test_databricks.py @@ -7505,6 +7505,38 @@ def send(self, request, *, follow_redirects): assert "accept" not in headers +def test_macos_proxy_fallback_uses_urllib_system_proxy_and_bypass(monkeypatch): + monkeypatch.setattr(db_mod.sys, "platform", "darwin") + for name in ( + "HTTP_PROXY", + "http_proxy", + "HTTPS_PROXY", + "https_proxy", + "ALL_PROXY", + "all_proxy", + ): + monkeypatch.delenv(name, raising=False) + monkeypatch.setattr( + db_mod.urllib_request, + "getproxies", + lambda: {"http": "http://system-proxy:8080", "https": "http://system-proxy:8080"}, + ) + monkeypatch.setattr( + db_mod.urllib_request, "proxy_bypass", lambda host: host == "internal.example" + ) + + proxies = db_mod._macos_system_proxies() + assert proxies == { + "http": "http://system-proxy:8080", + "https": "http://system-proxy:8080", + } + assert ( + db_mod._urllib_proxy_for_request("https", "workspace.example", proxies) + == "http://system-proxy:8080" + ) + assert db_mod._urllib_proxy_for_request("https", "internal.example", proxies) is None + + def test_after_fork_hook_replaces_parent_http_client(monkeypatch): parent_pid = os.getpid() parent_client = db_mod._get_http_client() From 9886cf84f3a753f22fdbb6ff9d166931a731a786 Mon Sep 17 00:00:00 2001 From: John Speaks Date: Fri, 20 Nov 2026 00:37:44 +0000 Subject: [PATCH 3/8] Harden pooled transport error and proxy mapping Map httpx's redirect-exceeded error to a urllib HTTPError with a status-shaped reason, and other transport errors to URLError, so callers see what urllib would have raised. Normalize scheme-less macOS system proxies to http:// URLs as urllib's proxy handler does. Route the unauthenticated workspace host-type probe through the pooled client. Co-authored-by: Isaac --- src/ucode/databricks.py | 24 +++++++++++++-- src/ucode/workspace.py | 6 +++- tests/test_databricks.py | 49 +++++++++++++++++++++++++++++++ tests/test_workspace_selection.py | 49 +++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 4 deletions(-) diff --git a/src/ucode/databricks.py b/src/ucode/databricks.py index a439d33b2..e7692b189 100644 --- a/src/ucode/databricks.py +++ b/src/ucode/databricks.py @@ -401,9 +401,16 @@ def _macos_system_proxies() -> dict[str, str] | None: ): return None proxies = urllib_request.getproxies() - system_proxies = { - scheme: proxy for scheme in ("http", "https", "all") if (proxy := proxies.get(scheme)) - } + system_proxies: dict[str, str] = {} + for scheme in ("http", "https", "all"): + proxy = proxies.get(scheme) + if not proxy: + continue + if "://" not in proxy: + # macOS can report scheme-less values ("host:port"); urllib's proxy handler + # treats those as HTTP proxies, so make that explicit for httpx. + proxy = f"http://{proxy}" + system_proxies[scheme] = proxy return system_proxies or None @@ -528,6 +535,13 @@ def _http_open(request: urllib_request.Request, *, timeout: float) -> _PooledRes ) try: response = _get_http_client().send(httpx_request, follow_redirects=True) + except httpx.TooManyRedirects as exc: + # urllib's redirect handler raises HTTPError when it exceeds its redirect budget, so + # callers see a status-shaped failure, not a network one. httpx drops the last response, + # so report the generic 310 (Too Many Redirects) status. + raise urllib_error.HTTPError( + request.full_url, 310, "Too Many Redirects", Message(), io.BytesIO(b"") + ) from exc except httpx.RequestError as exc: pending = [exc] seen: set[int] = set() @@ -548,6 +562,10 @@ def _http_open(request: urllib_request.Request, *, timeout: float) -> _PooledRes elif current.__context__ is not None: pending.append(current.__context__) raise urllib_error.URLError(reason) from exc + except httpx.HTTPError as exc: + # Other transport-level httpx errors (the pool closed, a stream broke): urllib has no + # counterpart, so surface them as network errors like the RequestError mapping above. + raise urllib_error.URLError(exc) from exc pooled_response = _PooledResponse(response) if not 200 <= response.status_code < 300: diff --git a/src/ucode/workspace.py b/src/ucode/workspace.py index 3ea2d90a7..2cf4b0883 100644 --- a/src/ucode/workspace.py +++ b/src/ucode/workspace.py @@ -109,7 +109,11 @@ def host_type(host: str) -> str | None: ) result: str | None = None try: - with urllib_request.urlopen(request, timeout=_HOST_TYPE_TIMEOUT_SECONDS) as response: + # Imported lazily: databricks imports this module. This unauthenticated request rides the + # same pooled HTTP client as every other workspace call. + from ucode.databricks import _http_open + + with _http_open(request, timeout=_HOST_TYPE_TIMEOUT_SECONDS) as response: payload = json.loads(response.read().decode("utf-8")) value = payload.get("host_type") if isinstance(payload, dict) else None result = value.lower() if isinstance(value, str) else None diff --git a/tests/test_databricks.py b/tests/test_databricks.py index 97969ec35..52eb7a46b 100644 --- a/tests/test_databricks.py +++ b/tests/test_databricks.py @@ -7552,3 +7552,52 @@ def test_after_fork_hook_replaces_parent_http_client(monkeypatch): assert db_mod._HTTP_CLIENT_PID == parent_pid + 1 finally: child_client.close() + + +def test_macos_schemeless_system_proxy_is_normalized(monkeypatch): + monkeypatch.setattr(db_mod.sys, "platform", "darwin") + for name in ("HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", "ALL_PROXY", "all_proxy"): + monkeypatch.delenv(name, raising=False) + monkeypatch.setattr( + db_mod.urllib_request, "getproxies", lambda: {"https": "system-proxy:3128", "all": ""} + ) + + proxies = db_mod._macos_system_proxies() + + assert proxies == {"https": "http://system-proxy:3128"} + + +class TestPooledRedirectErrors: + """httpx errors urllib's opener would have surfaced as HTTP errors keep their shape, so + callers see a status-shaped reason instead of a network failure.""" + + @staticmethod + def _client_raising(monkeypatch, exc): + import httpx + from unittest.mock import MagicMock + + client = MagicMock() + client.send.side_effect = exc + monkeypatch.setattr(db_mod, "_get_http_client", lambda: client) + return httpx + + def test_too_many_redirects_maps_to_http_error(self, monkeypatch): + import httpx + from urllib.error import HTTPError + + self._client_raising(monkeypatch, httpx.TooManyRedirects("too many redirects")) + + with pytest.raises(HTTPError) as raised: + db_mod._http_open(db_mod.urllib_request.Request("https://x/y"), timeout=5) + + assert raised.value.code == 310 + + def test_too_many_redirects_reason_is_status_shaped(self, monkeypatch): + import httpx + + self._client_raising(monkeypatch, httpx.TooManyRedirects("too many redirects")) + + body, reason = db_mod._http_get_bytes("https://x/y", "tok") + + assert body is None + assert reason == "HTTP 310 Too Many Redirects" diff --git a/tests/test_workspace_selection.py b/tests/test_workspace_selection.py index 32d34c2aa..66be2cd91 100644 --- a/tests/test_workspace_selection.py +++ b/tests/test_workspace_selection.py @@ -526,3 +526,52 @@ def popen(argv, env, **kwargs): ) skills_usage.report_skill_usage_in_background(SHARED, "tok", [ref]) assert captured["request"]["workspace_id"] == WSID + + +class TestHostTypeUsesPooledTransport: + """The unauthenticated well-known probe rides the pooled HTTP client like every other + workspace request, keeping urllib's error mapping.""" + + @staticmethod + def _pooled_response(payload: bytes): + from email.message import Message + + class _Response: + def __init__(self): + self.headers = Message() + self.status = 200 + + def __enter__(self): + return self + + def __exit__(self, *exc): + return False + + def read(self): + return payload + + return _Response() + + def test_host_type_opens_through_the_pooled_client(self): + with patch.object(db_mod, "_http_open") as http_open: + http_open.return_value = self._pooled_response( + json.dumps({"host_type": "unified"}).encode() + ) + workspace_mod._host_types.clear() + + assert workspace_mod.host_type("https://example.com") == "unified" + + assert http_open.call_count == 1 + assert http_open.call_args.kwargs["timeout"] == workspace_mod._HOST_TYPE_TIMEOUT_SECONDS + assert http_open.call_args.args[0].full_url == ( + "https://example.com/.well-known/databricks-config" + ) + + def test_host_type_maps_pooled_urllib_errors_as_before(self): + from urllib.error import URLError + + with patch.object(db_mod, "_http_open", side_effect=URLError("conn refused")): + workspace_mod._host_types.clear() + + with pytest.raises(workspace_mod.HostUnreachableError, match="did not answer"): + workspace_mod.host_type("https://example.com") From 06f08092f6f9e599f8a17689bf618d98c5208f71 Mon Sep 17 00:00:00 2001 From: John Speaks Date: Fri, 20 Nov 2026 01:58:15 +0000 Subject: [PATCH 4/8] Point the host-type tests at the pooled transport The host-type probe rides the pooled client now, so patch _http_open instead of urlopen; error-shape expectations are unchanged. Co-authored-by: Isaac --- tests/test_workspace.py | 54 +++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/tests/test_workspace.py b/tests/test_workspace.py index 0b83a8908..875b9ea43 100644 --- a/tests/test_workspace.py +++ b/tests/test_workspace.py @@ -98,44 +98,52 @@ def test_largest_accepted_id(self): class TestHostType: - def _response(self, payload: dict): - return io.BytesIO(json.dumps(payload).encode()) + def _open(self, payload: dict): + import ucode.databricks as db_mod + + class _Response: + headers = None + + def __enter__(self): + return self + + def __exit__(self, *exc): + return False + + def read(self): + return json.dumps(payload).encode() + + return patch.object(db_mod, "_http_open", return_value=_Response()) def test_reads_host_type_without_a_selector(self): - with patch.object( - workspace_mod.urllib_request, - "urlopen", - return_value=self._response({"host_type": "UNIFIED"}), - ) as urlopen: + with self._open({"host_type": "UNIFIED"}) as http_open: assert host_type(SHARED) == "unified" - request = urlopen.call_args.args[0] + request = http_open.call_args.args[0] assert request.full_url == f"{SHARED}/.well-known/databricks-config" assert not any(name.lower().startswith("x-databricks") for name in request.headers) def test_cached_per_host(self): - with patch.object( - workspace_mod.urllib_request, - "urlopen", - return_value=self._response({"host_type": "workspace"}), - ) as urlopen: + with self._open({"host_type": "workspace"}) as http_open: host_type(CLASSIC) host_type(CLASSIC) - assert urlopen.call_count == 1 + assert http_open.call_count == 1 def test_unreachable_host_raises_and_is_not_cached(self): - with patch.object( - workspace_mod.urllib_request, "urlopen", side_effect=urllib_error.URLError("down") - ): + import ucode.databricks as db_mod + + with patch.object(db_mod, "_http_open", side_effect=urllib_error.URLError("down")): with pytest.raises(workspace_mod.HostUnreachableError, match="down"): host_type(CLASSIC) assert not workspace_mod.is_unified_host(CLASSIC) assert CLASSIC not in workspace_mod._host_types def test_bad_response_is_unreachable(self): + import ucode.databricks as db_mod + with ( patch.object( - workspace_mod.urllib_request, - "urlopen", + db_mod, + "_http_open", side_effect=workspace_mod.http.client.IncompleteRead(b""), ), pytest.raises(workspace_mod.HostUnreachableError), @@ -143,16 +151,20 @@ def test_bad_response_is_unreachable(self): host_type(CLASSIC) def test_server_error_is_unreachable(self): + import ucode.databricks as db_mod + error = urllib_error.HTTPError(f"{SHARED}/x", 503, "Unavailable", None, None) # type: ignore[arg-type] with ( - patch.object(workspace_mod.urllib_request, "urlopen", side_effect=error), + patch.object(db_mod, "_http_open", side_effect=error), pytest.raises(workspace_mod.HostUnreachableError, match="503"), ): host_type(SHARED) def test_host_without_the_document_is_a_single_workspace_host(self): + import ucode.databricks as db_mod + error = urllib_error.HTTPError(f"{CLASSIC}/x", 404, "Not Found", None, None) # type: ignore[arg-type] - with patch.object(workspace_mod.urllib_request, "urlopen", side_effect=error): + with patch.object(db_mod, "_http_open", side_effect=error): assert host_type(CLASSIC) is None From dcf74c0ceb96308dcffb20fb37ebcba1ea75feaf Mon Sep 17 00:00:00 2001 From: John Speaks Date: Fri, 20 Nov 2026 12:45:16 +0000 Subject: [PATCH 5/8] Build the pooled client on the #168 trust store tls.apply() runs before the pooled client's SSL context is built, so it verifies against the same trust store (UG_TLS_TRUST_STORE, state.json, default os) as the urllib path did, keeping the inspection-proxy fix. A test pins the ordering and reuses the helper's context. Co-authored-by: Isaac --- tests/test_databricks.py | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/test_databricks.py b/tests/test_databricks.py index 52eb7a46b..dd704fce0 100644 --- a/tests/test_databricks.py +++ b/tests/test_databricks.py @@ -7537,6 +7537,47 @@ def test_macos_proxy_fallback_uses_urllib_system_proxy_and_bypass(monkeypatch): assert db_mod._urllib_proxy_for_request("https", "internal.example", proxies) is None +def test_pooled_client_built_on_tls_trust_store(monkeypatch): + """The pooled client's SSL context comes from #168's trust-store helper: tls.apply() runs + first, so the context it builds (and both transports share) verifies against the + configured store, exactly as the urllib path did.""" + import httpx + + order = [] + real_apply = db_mod.tls.apply + real_context = db_mod.ssl.create_default_context + contexts = [] + + def spy_apply(*args, **kwargs): + order.append("apply") + return real_apply(*args, **kwargs) + + def spy_context(*args, **kwargs): + context = real_context(*args, **kwargs) + contexts.append(context) + order.append("context") + return context + + built = {} + real_client = httpx.Client + + def spy_client(*args, **kwargs): + built.update(kwargs) + return real_client(*args, **kwargs) + + monkeypatch.setattr(db_mod.tls, "apply", spy_apply) + monkeypatch.setattr(db_mod.ssl, "create_default_context", spy_context) + monkeypatch.setattr(httpx, "Client", spy_client) + monkeypatch.setattr(db_mod, "_HTTP_CLIENT", None) + + client = db_mod._get_http_client() + try: + assert order == ["apply", "context"] + assert built["verify"] is contexts[-1] + finally: + client.close() + + def test_after_fork_hook_replaces_parent_http_client(monkeypatch): parent_pid = os.getpid() parent_client = db_mod._get_http_client() From c19624683d792190e81eef98d5e59f62c5c44476 Mon Sep 17 00:00:00 2001 From: John Speaks Date: Fri, 20 Nov 2026 13:37:22 +0000 Subject: [PATCH 6/8] Fix ruff findings in the pooled transport tests Co-authored-by: Isaac --- tests/test_databricks.py | 15 ++++++++++++--- tests/test_workspace.py | 1 - 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/test_databricks.py b/tests/test_databricks.py index dd704fce0..07f3ef6e9 100644 --- a/tests/test_databricks.py +++ b/tests/test_databricks.py @@ -7597,7 +7597,14 @@ def test_after_fork_hook_replaces_parent_http_client(monkeypatch): def test_macos_schemeless_system_proxy_is_normalized(monkeypatch): monkeypatch.setattr(db_mod.sys, "platform", "darwin") - for name in ("HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", "ALL_PROXY", "all_proxy"): + for name in ( + "HTTP_PROXY", + "http_proxy", + "HTTPS_PROXY", + "https_proxy", + "ALL_PROXY", + "all_proxy", + ): monkeypatch.delenv(name, raising=False) monkeypatch.setattr( db_mod.urllib_request, "getproxies", lambda: {"https": "system-proxy:3128", "all": ""} @@ -7614,18 +7621,20 @@ class TestPooledRedirectErrors: @staticmethod def _client_raising(monkeypatch, exc): - import httpx from unittest.mock import MagicMock + import httpx + client = MagicMock() client.send.side_effect = exc monkeypatch.setattr(db_mod, "_get_http_client", lambda: client) return httpx def test_too_many_redirects_maps_to_http_error(self, monkeypatch): - import httpx from urllib.error import HTTPError + import httpx + self._client_raising(monkeypatch, httpx.TooManyRedirects("too many redirects")) with pytest.raises(HTTPError) as raised: diff --git a/tests/test_workspace.py b/tests/test_workspace.py index 875b9ea43..711548d98 100644 --- a/tests/test_workspace.py +++ b/tests/test_workspace.py @@ -2,7 +2,6 @@ from __future__ import annotations -import io import json from unittest.mock import patch from urllib import error as urllib_error From d8903f6759aaa19f2be3aec4852ecf34323a766c Mon Sep 17 00:00:00 2001 From: John Speaks Date: Fri, 20 Nov 2026 14:25:06 +0000 Subject: [PATCH 7/8] Fix ty findings in the pooled transport Co-authored-by: Isaac --- src/ucode/databricks.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ucode/databricks.py b/src/ucode/databricks.py index e7692b189..93dfd6d94 100644 --- a/src/ucode/databricks.py +++ b/src/ucode/databricks.py @@ -525,12 +525,14 @@ def _http_open(request: urllib_request.Request, *, timeout: float) -> _PooledRes if "connection" not in header_names: request_headers["Connection"] = "keep-alive" if "user-agent" not in header_names: - request_headers["User-Agent"] = f"Python-urllib/{urllib_request.__version__}" + # urllib's own default: '%d.%d' % sys.version_info[:2]. + request_headers["User-Agent"] = f"Python-urllib/{sys.version_info[0]}.{sys.version_info[1]}" httpx_request = httpx.Request( request.get_method(), request.full_url, headers=request_headers, - content=request.data, + # The only Request built with data is `_http_send_json`'s, which sends bytes or None. + content=cast("bytes | None", request.data), extensions={"timeout": httpx.Timeout(timeout).as_dict()}, ) try: @@ -543,7 +545,7 @@ def _http_open(request: urllib_request.Request, *, timeout: float) -> _PooledRes request.full_url, 310, "Too Many Redirects", Message(), io.BytesIO(b"") ) from exc except httpx.RequestError as exc: - pending = [exc] + pending: list[BaseException] = [exc] seen: set[int] = set() reason: BaseException = exc while pending: From 4409ade901cee061e788c4452ff414518dc1db85 Mon Sep 17 00:00:00 2001 From: John Speaks Date: Fri, 20 Nov 2026 15:26:16 +0000 Subject: [PATCH 8/8] Accept Windows' wording of a refused connection in the transport test Co-authored-by: Isaac --- tests/test_databricks.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_databricks.py b/tests/test_databricks.py index 07f3ef6e9..33ddc3edf 100644 --- a/tests/test_databricks.py +++ b/tests/test_databricks.py @@ -7472,7 +7472,8 @@ def test_http_transport_maps_refused_connections_to_network_errors(monkeypatch): _, reason = db_mod._http_get_bytes(f"http://127.0.0.1:{port}/api", "token") assert reason is not None assert reason.startswith("network error:") - assert "Connection refused" in reason + # Windows words it "[WinError 10061] ... actively refused it". + assert "refused" in reason.lower() def test_http_transport_passes_headers_and_timeout(monkeypatch):