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

Cors related traceback on run_async.py #2944

Open
1 task done
LasseBlaauwbroek opened this issue Apr 29, 2024 · 3 comments
Open
1 task done

Cors related traceback on run_async.py #2944

LasseBlaauwbroek opened this issue Apr 29, 2024 · 3 comments
Labels

Comments

@LasseBlaauwbroek
Copy link

LasseBlaauwbroek commented Apr 29, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm running Sanic as part of a larger async codebase. I'm using the run_async.py example as a blueprint. If you run this code, and visit the website, everything works as normal. However, on every page-load, this traceback is printed:

[2024-04-29 13:41:43 +0200] [34199] [ERROR] Exception occurred in one of response middleware handlers
Traceback (most recent call last):
  File "<..>/venv-311/lib/python3.11/site-packages/sanic/request/types.py", line 402, in respond
    response = await self.app._run_response_middleware(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "_run_response_middleware", line 17, in _run_response_middleware
    from asyncio.futures import Future
                            ^^^^^^^^^^^
  File "<..>/venv-311/lib/python3.11/site-packages/sanic_ext/extensions/http/cors.py", line 54, in _add_cors_headers
    _add_origin_header(request, response)
  File "<..>/venv-311/lib/python3.11/site-packages/sanic_ext/extensions/http/cors.py", line 160, in _add_origin_header
    allow_origins = _get_from_cors_ctx(
                    ^^^^^^^^^^^^^^^^^^^
  File "<..>/venv-311/lib/python3.11/site-packages/sanic_ext/extensions/http/cors.py", line 151, in _get_from_cors_ctx
    value = getattr(request.route.ctx._cors, key, default)
                    ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'types.SimpleNamespace' object has no attribute '_cors'

Code snippet

No response

Expected Behavior

No response

How do you run Sanic?

As a module

Operating System

Linux

Sanic Version

23.12.0

Additional context

No response

@dinosaurtirex
Copy link

Same

@thnee
Copy link

thnee commented Oct 15, 2024

This is indeed very annoying, and it probably means that CORS will not work correctly at all.

This probably happens because the internal _cors attribute is created via a before_server_start hook, and that hook does not get called when not using the normal app.run.

https://github.com/sanic-org/sanic-ext/blob/v23.12.0/sanic_ext/extensions/http/cors.py#L83

This seems like a design oversight, and it should really be addressed.

If you don't need/want CORS, then you can get rid of the error by setting app.config.CORS = False to disable CORS completely,

But if you do want CORS, then this is a blocker. And it is pretty difficult to create a workaround, since _assign_cors_settings is defined as an inner function, which prevents it from being imported and called like a normal function.

@thnee
Copy link

thnee commented Oct 16, 2024

Looks like I found a workaround.

This code uses Sanic's built in server, and it does not call the before_server_start hook, so it produces the error.

async def server():
    server = await app.create_server()
    await server.startup()
    await server.serve_forever()

This code uses the Uvicorn server, and it does call the before_server_start hook, so it does not produce the error.

async def server():
    config = uvicorn.Config(app=app)
    server = uvicorn.Server(config=config)
    await server.serve()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants