From 05d9ac81c502aeb9020a95d58ef6734771473876 Mon Sep 17 00:00:00 2001 From: Rayeed Zaman Date: Fri, 6 Nov 2026 22:10:03 -0500 Subject: [PATCH 1/2] fix: make PycurlClient timeouts configurable PycurlClient hardcoded CONNECTTIMEOUT=30 and TIMEOUT=80 on every request, unlike RequestsClient, HTTPXClient and AIOHTTPClient, which accept a `timeout` constructor argument. Add keyword-only `timeout` and `connect_timeout` arguments (defaults 80 and 30) after the existing parameters so positional callers are unaffected. Fixes #1962 --- ...ace_pycurl-configurable-timeouts.change.md | 8 ++ stripe/_http_client.py | 15 ++- tests/test_http_client.py | 101 ++++++++++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 .hark/changes/2026-11-06_itsrayeedscodespace_pycurl-configurable-timeouts.change.md diff --git a/.hark/changes/2026-11-06_itsrayeedscodespace_pycurl-configurable-timeouts.change.md b/.hark/changes/2026-11-06_itsrayeedscodespace_pycurl-configurable-timeouts.change.md new file mode 100644 index 000000000..0e93637c7 --- /dev/null +++ b/.hark/changes/2026-11-06_itsrayeedscodespace_pycurl-configurable-timeouts.change.md @@ -0,0 +1,8 @@ +--- +title: Add configurable `timeout` and `connect_timeout` to `PycurlClient` +semver_level: minor +github_issues_resolved: +- https://github.com/stripe/stripe-python/issues/1962 +--- + +`PycurlClient` now accepts keyword-only `timeout` (total request time, curl's `TIMEOUT`) and `connect_timeout` (curl's `CONNECTTIMEOUT`) arguments, in whole seconds. They default to the previous hardcoded values of 80 and 30 seconds. diff --git a/stripe/_http_client.py b/stripe/_http_client.py index 6f0815e91..0d95982da 100644 --- a/stripe/_http_client.py +++ b/stripe/_http_client.py @@ -858,12 +858,23 @@ def __init__( proxy: Optional[HTTPClient._Proxy] = None, async_fallback_client: Optional[HTTPClient] = None, _lib=None, # used for internal unit testing + *, + timeout: int = 80, + connect_timeout: int = 30, ): + """ + `timeout` is the maximum number of seconds the whole request may take + (curl's `TIMEOUT`), and `connect_timeout` is the maximum number of + seconds to wait for the connection to be established (curl's + `CONNECTTIMEOUT`). curl only accepts whole seconds for both. + """ super(PycurlClient, self).__init__( verify_ssl_certs=verify_ssl_certs, proxy=proxy, async_fallback_client=async_fallback_client, ) + self._timeout = timeout + self._connect_timeout = connect_timeout if _lib is None: import pycurl # pyright: ignore[reportMissingModuleSource] @@ -972,8 +983,8 @@ def _request_internal( self._curl.setopt(self.pycurl.WRITEFUNCTION, b.write) self._curl.setopt(self.pycurl.HEADERFUNCTION, rheaders.write) self._curl.setopt(self.pycurl.NOSIGNAL, 1) - self._curl.setopt(self.pycurl.CONNECTTIMEOUT, 30) - self._curl.setopt(self.pycurl.TIMEOUT, 80) + self._curl.setopt(self.pycurl.CONNECTTIMEOUT, self._connect_timeout) + self._curl.setopt(self.pycurl.TIMEOUT, self._timeout) _assert_no_header_injection(headers) self._curl.setopt( self.pycurl.HTTPHEADER, diff --git a/tests/test_http_client.py b/tests/test_http_client.py index 0c6eaa557..ac4ec85d6 100644 --- a/tests/test_http_client.py +++ b/tests/test_http_client.py @@ -1166,6 +1166,107 @@ def test_request_rejects_header_injection( with pytest.raises(ValueError): make_request("get", self.valid_url, headers, None) + @pytest.mark.parametrize( + "client_kwargs,expected_connect_timeout,expected_timeout", + [ + ({}, 30, 80), + ({"connect_timeout": 5, "timeout": 10}, 5, 10), + ({"connect_timeout": 5}, 5, 80), + ({"timeout": 10}, 30, 10), + ], + ) + def test_timeout( + self, + mocked_request_lib, + mock_response, + make_request, + client_kwargs, + expected_connect_timeout, + expected_timeout, + ): + mock_response(mocked_request_lib, '{"foo": "baz"}', 200) + make_request( + "GET", self.valid_url, {}, None, client_kwargs=client_kwargs + ) + + curl_mock = mocked_request_lib.Curl() + curl_mock.setopt.assert_any_call( + mocked_request_lib.CONNECTTIMEOUT, expected_connect_timeout + ) + curl_mock.setopt.assert_any_call( + mocked_request_lib.TIMEOUT, expected_timeout + ) + + def test_timeout_stream( + self, mocked_request_lib, mock_response, make_streamed_request + ): + mock_response(mocked_request_lib, "some streamed content", 200) + make_streamed_request( + "GET", + self.valid_url, + {}, + None, + client_kwargs={"connect_timeout": 5, "timeout": 10}, + ) + + curl_mock = mocked_request_lib.Curl() + curl_mock.setopt.assert_any_call(mocked_request_lib.CONNECTTIMEOUT, 5) + curl_mock.setopt.assert_any_call(mocked_request_lib.TIMEOUT, 10) + + def test_timeout_reapplied_after_reset( + self, mocked_request_lib, mock_response, make_client + ): + mock_response(mocked_request_lib, '{"foo": "baz"}', 200) + client = make_client(connect_timeout=5, timeout=10) + + client.request("get", self.valid_url, {}) + client.request("get", self.valid_url, {}) + + curl_mock = mocked_request_lib.Curl() + # reset() clears every option, so each request must set the + # timeouts again after its own reset() call. + relevant_calls = [ + c + for c in curl_mock.mock_calls + if c == call.reset() + or ( + c[0] == "setopt" + and c[1][0] + in ( + mocked_request_lib.CONNECTTIMEOUT, + mocked_request_lib.TIMEOUT, + ) + ) + ] + assert ( + relevant_calls + == [ + call.reset(), + call.setopt(mocked_request_lib.CONNECTTIMEOUT, 5), + call.setopt(mocked_request_lib.TIMEOUT, 10), + ] + * 2 + ) + + def test_positional_args(self, mocked_request_lib): + client = self.REQUEST_CLIENT(False, None, None, mocked_request_lib) + + assert client.pycurl is mocked_request_lib + assert client._verify_ssl_certs is False + assert client._connect_timeout == 30 + assert client._timeout == 80 + + # Timeouts are keyword-only so they can't be confused with each + # other or with the existing positional parameters. + with pytest.raises(TypeError): + self.REQUEST_CLIENT( + False, + None, + None, + mocked_request_lib, + 10, # pyright: ignore[reportGeneralTypeIssues] + ) + class TestPycurlClientHttpProxy(TestPycurlClient): PROXY = "http://user:withPwd@slap:8888/" From a5dd59d110181a9d8fa815445331bb6adb3806dc Mon Sep 17 00:00:00 2001 From: Rayeed Zaman Date: Fri, 13 Nov 2026 11:29:17 -0500 Subject: [PATCH 2/2] Remove unnecessary PycurlClient constructor docstring --- stripe/_http_client.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/stripe/_http_client.py b/stripe/_http_client.py index 45b6b695c..f11f6c600 100644 --- a/stripe/_http_client.py +++ b/stripe/_http_client.py @@ -862,12 +862,6 @@ def __init__( timeout: int = 80, connect_timeout: int = 30, ): - """ - `timeout` is the maximum number of seconds the whole request may take - (curl's `TIMEOUT`), and `connect_timeout` is the maximum number of - seconds to wait for the connection to be established (curl's - `CONNECTTIMEOUT`). curl only accepts whole seconds for both. - """ super(PycurlClient, self).__init__( verify_ssl_certs=verify_ssl_certs, proxy=proxy,