From 837a36ad9a6e9da4ac8eda1fc1669ab7d4214d13 Mon Sep 17 00:00:00 2001 From: qstokkink Date: Tue, 20 Aug 2024 14:02:25 +0200 Subject: [PATCH] Wrap tcp_keepalive to allow OSError 22 --- src/tribler/core/restapi/rest_manager.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/tribler/core/restapi/rest_manager.py b/src/tribler/core/restapi/rest_manager.py index 1a4774eabd..7960ecd92b 100644 --- a/src/tribler/core/restapi/rest_manager.py +++ b/src/tribler/core/restapi/rest_manager.py @@ -4,10 +4,11 @@ import ssl import traceback from asyncio.base_events import Server +from functools import wraps from pathlib import Path from typing import TYPE_CHECKING, Awaitable, Callable, Generic, TypeVar, cast -from aiohttp import web +from aiohttp import tcp_helpers, web, web_protocol from aiohttp.web_exceptions import HTTPNotFound, HTTPRequestEntityTooLarge from aiohttp_apispec import AiohttpApiSpec from apispec.core import VALID_METHODS_OPENAPI_V2 @@ -23,6 +24,8 @@ ) if TYPE_CHECKING: + import asyncio + from aiohttp.abc import Request from tribler.tribler_config import TriblerConfigManager @@ -40,6 +43,22 @@ class TriblerRequest(Request, Generic[ComponentsType]): RESTEndpointType = TypeVar("RESTEndpointType", bound=RESTEndpoint) +@wraps(tcp_helpers.tcp_keepalive) +def wrap_tcp_keepalive(transport: asyncio.Transport) -> None: + """ + A wrapper around aiohttp's tcp_keepalive that catches OSError 22 instances. + + See https://github.com/Tribler/tribler/issues/6429 + """ + try: + wrap_tcp_keepalive.__wrapped__(transport) + except OSError as e: + logger.warning("Setting tcp_keepalive on aiohttp socket failed!") + if e.errno != 22: + raise + +web_protocol.tcp_keepalive = wrap_tcp_keepalive + @web.middleware class ApiKeyMiddleware: """