Skip to content

Commit

Permalink
Wrap tcp_keepalive to allow OSError 22 (#8099)
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink authored Aug 20, 2024
2 parents e46f649 + 837a36a commit f5a8af8
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/tribler/core/restapi/rest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +24,8 @@
)

if TYPE_CHECKING:
import asyncio

from aiohttp.abc import Request

from tribler.tribler_config import TriblerConfigManager
Expand All @@ -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:
"""
Expand Down

0 comments on commit f5a8af8

Please sign in to comment.