|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 Atlan Pte. Ltd. |
| 3 | +""" |
| 4 | +Regression tests for GOVFOUN-408: httpcore connection pool deadlock fix. |
| 5 | +
|
| 6 | +Root cause: csa-metadata-completeness hung indefinitely on masterc-prd because |
| 7 | +two missing configs caused all 100 httpcore connection slots to fill with |
| 8 | +CLOSE_WAIT zombies, then 109 worker threads blocked forever in |
| 9 | +wait_for_connection(timeout=None). |
| 10 | +
|
| 11 | +These tests verify the two SDK-side fixes: |
| 12 | +1. httpx.Timeout has pool=30.0 — threads raise PoolTimeout after 30s instead |
| 13 | + of waiting forever. |
| 14 | +2. httpx.Limits has keepalive_expiry=30.0 — client retires idle connections |
| 15 | + before nginx's keepalive_timeout=75s FIN, preventing CLOSE_WAIT accumulation. |
| 16 | +""" |
| 17 | + |
| 18 | +from unittest.mock import Mock, patch |
| 19 | + |
| 20 | +import httpx |
| 21 | +import pytest |
| 22 | + |
| 23 | +from pyatlan.client.atlan import AtlanClient |
| 24 | +from pyatlan.client.transport import PyatlanSyncTransport |
| 25 | +from pyatlan.model.assets import AtlasGlossary |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(autouse=True) |
| 29 | +def set_env(monkeypatch): |
| 30 | + monkeypatch.setenv("ATLAN_BASE_URL", "https://test.atlan.com") |
| 31 | + monkeypatch.setenv("ATLAN_API_KEY", "test-api-key") |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture() |
| 35 | +def client(): |
| 36 | + return AtlanClient() |
| 37 | + |
| 38 | + |
| 39 | +def _error_response(): |
| 40 | + r = Mock() |
| 41 | + r.status_code = 500 |
| 42 | + r.text = "internal server error" |
| 43 | + return r |
| 44 | + |
| 45 | + |
| 46 | +def _trigger_api_call(client: AtlanClient) -> None: |
| 47 | + """Drive any outbound request through _call_api_internal.""" |
| 48 | + try: |
| 49 | + client.asset.save(AtlasGlossary.creator(name="t")) |
| 50 | + except Exception: |
| 51 | + pass |
| 52 | + |
| 53 | + |
| 54 | +def _get_httpcore_pool(client: AtlanClient): |
| 55 | + transport = client._session._transport |
| 56 | + assert isinstance(transport, PyatlanSyncTransport) |
| 57 | + return transport._transport._pool |
| 58 | + |
| 59 | + |
| 60 | +# --------------------------------------------------------------------------- |
| 61 | +# Pool timeout |
| 62 | +# --------------------------------------------------------------------------- |
| 63 | + |
| 64 | + |
| 65 | +@patch.object(AtlanClient, "_session") |
| 66 | +def test_pool_timeout_is_30_seconds(mock_session, client): |
| 67 | + """pool=30.0 must be set — the live deadlock showed pool=None caused infinite blocking.""" |
| 68 | + mock_session.request.return_value = _error_response() |
| 69 | + _trigger_api_call(client) |
| 70 | + assert mock_session.request.called, "no HTTP request was made" |
| 71 | + timeout = mock_session.request.call_args.kwargs["timeout"] |
| 72 | + assert timeout.pool == 30.0 |
| 73 | + |
| 74 | + |
| 75 | +@patch.object(AtlanClient, "_session") |
| 76 | +def test_pool_timeout_is_not_none(mock_session, client): |
| 77 | + """pool timeout must never be None — threading.Event.wait(timeout=None) blocks forever.""" |
| 78 | + mock_session.request.return_value = _error_response() |
| 79 | + _trigger_api_call(client) |
| 80 | + assert mock_session.request.called |
| 81 | + timeout = mock_session.request.call_args.kwargs["timeout"] |
| 82 | + assert timeout.pool is not None |
| 83 | + |
| 84 | + |
| 85 | +@patch.object(AtlanClient, "_session") |
| 86 | +def test_connect_timeout_unchanged(mock_session, client): |
| 87 | + """connect timeout must still equal client.connect_timeout (default 30s).""" |
| 88 | + mock_session.request.return_value = _error_response() |
| 89 | + _trigger_api_call(client) |
| 90 | + assert mock_session.request.called |
| 91 | + timeout = mock_session.request.call_args.kwargs["timeout"] |
| 92 | + assert timeout.connect == client.connect_timeout |
| 93 | + |
| 94 | + |
| 95 | +@patch.object(AtlanClient, "_session") |
| 96 | +def test_read_timeout_unchanged(mock_session, client): |
| 97 | + """read timeout must still equal client.read_timeout (default 900s).""" |
| 98 | + mock_session.request.return_value = _error_response() |
| 99 | + _trigger_api_call(client) |
| 100 | + assert mock_session.request.called |
| 101 | + timeout = mock_session.request.call_args.kwargs["timeout"] |
| 102 | + assert timeout.read == client.read_timeout |
| 103 | + |
| 104 | + |
| 105 | +@patch.object(AtlanClient, "_session") |
| 106 | +def test_pool_timeout_propagates_not_hangs(mock_session, client): |
| 107 | + """httpx.PoolTimeout must propagate — if it were swallowed the workflow would still hang.""" |
| 108 | + mock_session.request.side_effect = httpx.PoolTimeout( |
| 109 | + "connection pool exhausted", request=None |
| 110 | + ) |
| 111 | + with pytest.raises(Exception): |
| 112 | + client.asset.save(AtlasGlossary.creator(name="t")) |
| 113 | + |
| 114 | + |
| 115 | +# --------------------------------------------------------------------------- |
| 116 | +# Transport limits |
| 117 | +# --------------------------------------------------------------------------- |
| 118 | + |
| 119 | + |
| 120 | +def test_transport_max_connections_is_50(client): |
| 121 | + """max_connections=50 reduces blast radius when CLOSE_WAIT sockets accumulate.""" |
| 122 | + assert _get_httpcore_pool(client)._max_connections == 50 |
| 123 | + |
| 124 | + |
| 125 | +def test_transport_keepalive_expiry_is_30_seconds(client): |
| 126 | + """keepalive_expiry=30.0 — client closes idle connections before nginx's 75s FIN.""" |
| 127 | + assert _get_httpcore_pool(client)._keepalive_expiry == 30.0 |
| 128 | + |
| 129 | + |
| 130 | +def test_transport_max_keepalive_connections_is_10(client): |
| 131 | + """max_keepalive_connections=10 bounds idle connections held in the pool.""" |
| 132 | + assert _get_httpcore_pool(client)._max_keepalive_connections == 10 |
| 133 | + |
| 134 | + |
| 135 | +# --------------------------------------------------------------------------- |
| 136 | +# max_retries context manager |
| 137 | +# --------------------------------------------------------------------------- |
| 138 | + |
| 139 | + |
| 140 | +def _capture_transport_limits(client: AtlanClient) -> httpx.Limits: |
| 141 | + """Enter max_retries, capture the limits used to construct the new transport.""" |
| 142 | + captured: dict = {} |
| 143 | + original_init = PyatlanSyncTransport.__init__ |
| 144 | + |
| 145 | + def capturing_init(self_t, retry=None, client=None, **kwargs): # noqa: ARG001 |
| 146 | + if "limits" in kwargs: |
| 147 | + captured["limits"] = kwargs["limits"] |
| 148 | + original_init(self_t, retry=retry, client=client, **kwargs) |
| 149 | + |
| 150 | + with patch.object(PyatlanSyncTransport, "__init__", capturing_init): |
| 151 | + with client.max_retries(): |
| 152 | + pass |
| 153 | + |
| 154 | + assert "limits" in captured, "max_retries did not pass limits to PyatlanSyncTransport" |
| 155 | + return captured["limits"] |
| 156 | + |
| 157 | + |
| 158 | +def test_max_retries_transport_max_connections_is_50(client): |
| 159 | + """max_retries context manager must use the same max_connections=50.""" |
| 160 | + assert _capture_transport_limits(client).max_connections == 50 |
| 161 | + |
| 162 | + |
| 163 | +def test_max_retries_transport_keepalive_expiry_is_30_seconds(client): |
| 164 | + """max_retries context manager must use keepalive_expiry=30.0.""" |
| 165 | + assert _capture_transport_limits(client).keepalive_expiry == 30.0 |
| 166 | + |
| 167 | + |
| 168 | +def test_max_retries_transport_max_keepalive_connections_is_10(client): |
| 169 | + """max_retries context manager must use max_keepalive_connections=10.""" |
| 170 | + assert _capture_transport_limits(client).max_keepalive_connections == 10 |
| 171 | + |
| 172 | + |
| 173 | +def test_max_retries_replaces_transport_inside_context(client): |
| 174 | + """max_retries installs a fresh transport for the duration of the context.""" |
| 175 | + original = client._session._transport |
| 176 | + with client.max_retries(): |
| 177 | + assert client._session._transport is not original |
| 178 | + |
| 179 | + |
| 180 | +def test_max_retries_restores_original_transport_on_exit(client): |
| 181 | + """max_retries restores the original transport when the context exits.""" |
| 182 | + original = client._session._transport |
| 183 | + with client.max_retries(): |
| 184 | + pass |
| 185 | + assert client._session._transport is original |
0 commit comments