Coder Social home page Coder Social logo

Support for add_middleware() about hypercorn HOT 8 CLOSED

gellnerm avatar gellnerm commented on September 27, 2024 1
Support for add_middleware()

from hypercorn.

Comments (8)

pgjones avatar pgjones commented on September 27, 2024

@Kludex I think Starlette is doing the wrong thing here as per django/asgiref#343, what do you think?

from hypercorn.

Kludex avatar Kludex commented on September 27, 2024

I did fix that on the last release: encode/starlette#2352. What Starlette version is being used here?

Also, kind a related, it would be cool if you can check django/asgiref#424. 🙏

EDIT: I just saw there's FastAPI on the traceback - FastAPI still doesn't support the last Starlette version.

from hypercorn.

gellnerm avatar gellnerm commented on September 27, 2024

What Starlette version is being used here?

0.27.0

from hypercorn.

pgjones avatar pgjones commented on September 27, 2024

Should be fixed now given enough time has passed for release updates.

from hypercorn.

dragoangel avatar dragoangel commented on September 27, 2024

@pgjones well for me this still the case with python:3.10-slim and next requirements.txt:

hypercorn~=0.16
asgi-correlation-id~=4.3
starlette~=0.37.0
jinja2~=3.1

like:

from asgi_correlation_id import CorrelationIdMiddleware, correlation_id
from hypercorn.middleware import ProxyFixMiddleware
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.gzip import GZipMiddleware

proxy_hops = int(os.getenv('PROXY_HOPS', '0'))
proxy_mode = os.getenv('PROXY_MODE', 'legacy')

middleware = [
    Middleware(CorrelationIdMiddleware, header_name='X-Request-ID'),
    Middleware(GZipMiddleware, minimum_size=1000)
]

if proxy_hops > 0:
    middleware.append(Middleware(ProxyFixMiddleware, mode=proxy_mode, trusted_hops=proxy_hops))

app = Starlette(middleware=middleware)
app.mount('/static', StaticFiles(directory = 'statics'), name = 'static')
templates = Jinja2Templates(directory = 'templates')

...

@app.exception_handler(500)
async def server_error(request, exc):
    """
    Return an HTTP 500 page.
    """
    template = "500.html"
    context = {
        'request': request, 
        'title': 'Server Error', 
        'company': company
    }
    headers = {
        'X-Request-ID': str(correlation_id.get() or '')
    }
    return templates.TemplateResponse(template, context, status_code = 500, headers=headers)

Other middleware's works just fine, and if I would set PROXY_HOPS=0 issue disappears as I skip adding ProxyFixMiddleware.

  • zero proxies mean we don't need to load ProxyFixMiddleware

from hypercorn.

pgjones avatar pgjones commented on September 27, 2024

This is likely a bug with the ProxyFixMiddleware that is fixed in 0.17.0, just released.

from hypercorn.

dragoangel avatar dragoangel commented on September 27, 2024

Just rebuild with hypercorn~=0.17 and still issue is same:

[2024-05-27 20:06:27 +0000] [13] [ERROR] Error in ASGI Framework
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/hypercorn/asyncio/task_group.py", line 27, in _handle
    await app(scope, receive, send, sync_spawn, call_soon)
  File "/usr/local/lib/python3.10/site-packages/hypercorn/app_wrappers.py", line 34, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.10/site-packages/starlette_exporter/middleware.py", line 285, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/asgi_correlation_id/middleware.py", line 90, in __call__
    await self.app(scope, receive, handle_outgoing_request)
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 26, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/hypercorn/middleware/proxy_fix.py", line 23, in __call__
    scope = deepcopy(scope)
  File "/usr/local/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/local/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/local/lib/python3.10/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/local/lib/python3.10/copy.py", line 271, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/local/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/local/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/local/lib/python3.10/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/local/lib/python3.10/copy.py", line 272, in _reconstruct
    if hasattr(y, '__setstate__'):
  File "/usr/local/lib/python3.10/site-packages/starlette/datastructures.py", line 699, in __getattr__
    return self._state[key]
  File "/usr/local/lib/python3.10/site-packages/starlette/datastructures.py", line 699, in __getattr__
    return self._state[key]
  File "/usr/local/lib/python3.10/site-packages/starlette/datastructures.py", line 699, in __getattr__
    return self._state[key]
  [Previous line repeated 966 more times]
RecursionError: maximum recursion depth exceeded

from hypercorn.

dragoangel avatar dragoangel commented on September 27, 2024

Can you please re-open issue as it not yet solved?

from hypercorn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.