Skip to content

Commit

Permalink
Add default timeout for HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelm committed Dec 2, 2023
1 parent 6c47980 commit 409c1ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/pretix/helpers/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ class PretixHelpersConfig(AppConfig):
label = 'pretixhelpers'

def ready(self):
from .monkeypatching import monkeypatch_all_at_ready
from .monkeypatching import (
monkeypatch_all_at_ready, monkeypatch_requests_timeout,
)
monkeypatch_all_at_ready()
monkeypatch_requests_timeout()
20 changes: 20 additions & 0 deletions src/pretix/helpers/monkeypatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
#
import types
from datetime import datetime

from PIL import Image
from requests.adapters import HTTPAdapter


def monkeypatch_vobject_performance():
Expand Down Expand Up @@ -70,3 +72,21 @@ def monkeypatch_pillow_safer():
def monkeypatch_all_at_ready():
monkeypatch_vobject_performance()
monkeypatch_pillow_safer()


def monkeypatch_requests_timeout():
"""
The requests package does not by default set a timeout for outgoing HTTP requests. This is dangerous especially since
celery tasks have no timeout on the task as a whole (as web requests do), so HTTP requests to a non-responding
external service could lead to a clogging of the entire celery queue.
"""
old_httpadapter_send = HTTPAdapter.send

def httpadapter_send(self, request, timeout=None, **kwargs):
if timeout is None:
timeout = 3
return types.MethodType(old_httpadapter_send, self)(request, timeout=timeout, **kwargs)

HTTPAdapter.send = httpadapter_send
monkeypatch_vobject_performance()
monkeypatch_pillow_safer()

0 comments on commit 409c1ee

Please sign in to comment.