Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap tcp_keepalive to allow OSError 22 #8099

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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