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

HH-230357 move anyio patch #738

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion frontik/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import time
from ctypes import c_bool, c_int
from typing import Optional, Union
from typing import Any, Optional, Union

from aiokafka import AIOKafkaProducer
from fastapi import FastAPI, HTTPException
Expand Down Expand Up @@ -52,6 +52,10 @@ def __init__(self) -> None:
self.include_router(_router)


def anyio_noop(*_args: Any, **_kwargs: Any) -> None:
raise RuntimeError(f'trying to use non async {_args[0]}')


class FrontikApplication:
request_id = ''

Expand All @@ -60,6 +64,7 @@ class DefaultConfig:

def __init__(self, app_module_name: Optional[str] = None) -> None:
self.start_time = time.time()
self.patch_anyio()

self.app_module_name: str = app_module_name or self.__class__.__module__
app_module = importlib.import_module(self.app_module_name)
Expand Down Expand Up @@ -93,6 +98,17 @@ def __init__(self, app_module_name: Optional[str] = None) -> None:
self.asgi_app = FrontikAsgiApp()
self.service_discovery: ServiceDiscovery

def patch_anyio(self) -> None:
"""
We have problems with anyio running sync dependencies in threadpool, so sync deps are prohibited
"""
try:
import anyio

anyio.to_thread.run_sync = anyio_noop # type: ignore
except ImportError:
pass

def __call__(self, tornado_request: httputil.HTTPServerRequest) -> None:
# for make it more asgi, reimplement tornado.http1connection._server_request_loop and ._read_message
task = asyncio.create_task(serve_tornado_request(self, self.asgi_app, tornado_request))
Expand Down
8 changes: 0 additions & 8 deletions frontik/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from threading import Lock
from typing import Callable, Optional, Union

import anyio
import tornado.autoreload
from http_client.balancing import Upstream
from tornado.httpserver import HTTPServer
Expand Down Expand Up @@ -178,10 +177,3 @@ async def _deinit_app(app: FrontikApplication) -> None:
await asyncio.sleep(options.stop_timeout)
if app.http_client_factory is not None:
await app.http_client_factory.http_client.client_session.close()


def anyio_noop(*_args, **_kwargs):
raise RuntimeError(f'trying to use non async {_args[0]}')


anyio.to_thread.run_sync = anyio_noop
Loading