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

ASGIMiddleware uses non-threadsafe asyncio API #53

Closed
encukou opened this issue May 7, 2024 · 3 comments
Closed

ASGIMiddleware uses non-threadsafe asyncio API #53

encukou opened this issue May 7, 2024 · 3 comments

Comments

@encukou
Copy link

encukou commented May 7, 2024

Running the following simplified app with PYTHONASYNCIODEBUG=1 raises errors about non-thread-safe use:

from a2wsgi import ASGIMiddleware
from werkzeug.test import Client

async def asgi_app(scope, receive, send):
    event = await receive()
    assert event['type'] == 'http.request'
    await send({"type": "http.response.start", 'status': 200, 'headers': []})
    await send({"type": "http.response.body"})


wsgi_app = ASGIMiddleware(asgi_app)

client = Client(wsgi_app)

print(client.get('/'))
$ PYTHONASYNCIODEBUG=1 python demo.py 
Traceback (most recent call last):
  ...
  File ".../site-packages/a2wsgi/asgi.py", line 213, in __call__
    asgi_task = self.start_asgi_app(environ)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../site-packages/a2wsgi/asgi.py", line 197, in start_asgi_app
    run_asgi: asyncio.Task = self.loop.create_task(
                             ^^^^^^^^^^^^^^^^^^^^^^
  File ".../asyncio/base_events.py", line 457, in create_task
    task = tasks.Task(coro, loop=self, name=name, context=context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../asyncio/base_events.py", line 795, in call_soon
    self._check_thread()
  File ".../asyncio/base_events.py", line 832, in _check_thread
    raise RuntimeError(
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one
...

Running a2wsgi's test suite with PYTHONASYNCIODEBUG also raises errors.

@abersheeran
Copy link
Owner

Did you encounter problems in production?

@encukou
Copy link
Author

encukou commented May 28, 2024

So far, only in tests. I'm still evaluating whether to use a2wsgi.
After the application was shut down, a left-over thread sometimes kept the Python process from exiting, so my test suite timed out on CI.

@abersheeran
Copy link
Owner

abersheeran commented May 28, 2024

The design here is not the problem, and your PR cannot run. So I temporarily close this issue PR pair. If you encounter related issues in production, you can create a new issue. Thank you.

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

Successfully merging a pull request may close this issue.

2 participants