From a1f916bc65c57de45cb1dd72f31d0508ac7afed9 Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Fri, 2 Aug 2024 09:37:35 -0400 Subject: [PATCH] fix: retry hcaptcha on timeouts (#16377) --- tests/unit/captcha/test_hcaptcha.py | 13 +++++++++++++ warehouse/captcha/hcaptcha.py | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/tests/unit/captcha/test_hcaptcha.py b/tests/unit/captcha/test_hcaptcha.py index 920236466248..4809705f9726 100644 --- a/tests/unit/captcha/test_hcaptcha.py +++ b/tests/unit/captcha/test_hcaptcha.py @@ -11,6 +11,7 @@ # limitations under the License. import pretend +import pyramid_retry import pytest import requests import responses @@ -119,6 +120,18 @@ def test_remote_ip_added(self): hostname=None, ) + def test_retries_on_timeout(self, monkeypatch): + service = hcaptcha.Service.create_service( + context=None, + request=_REQUEST, + ) + monkeypatch.setattr( + service.request.http, "post", pretend.raiser(requests.Timeout) + ) + + with pytest.raises(pyramid_retry.RetryableException): + service.verify_response("meaningless") + def test_unexpected_error(self, monkeypatch): service = hcaptcha.Service.create_service( context=None, diff --git a/warehouse/captcha/hcaptcha.py b/warehouse/captcha/hcaptcha.py index 9588847649d1..da49378af743 100644 --- a/warehouse/captcha/hcaptcha.py +++ b/warehouse/captcha/hcaptcha.py @@ -14,6 +14,8 @@ from urllib.parse import urlencode +from pyramid_retry import RetryableException +from requests.exceptions import Timeout from zope.interface import implementer from .interfaces import ChallengeResponse, ICaptchaService @@ -134,6 +136,8 @@ def verify_response(self, response, remote_ip=None) -> ChallengeResponse | None: }, timeout=10, ) + except Timeout as err: + raise RetryableException from err except Exception as err: raise UnexpectedError(str(err)) from err