Recreate ASGIMiddleware's internal event loop after forked WSGI startup#82
Draft
Recreate ASGIMiddleware's internal event loop after forked WSGI startup#82
Conversation
Co-authored-by: abersheeran <25364828+abersheeran@users.noreply.github.com> Agent-Logs-Url: https://github.com/abersheeran/a2wsgi/sessions/dd95209d-67f7-4bd1-ae72-85aed3f07808
Co-authored-by: abersheeran <25364828+abersheeran@users.noreply.github.com> Agent-Logs-Url: https://github.com/abersheeran/a2wsgi/sessions/dd95209d-67f7-4bd1-ae72-85aed3f07808
Copilot
AI
changed the title
[WIP] Fix ASGI app timeout issue with ASGIMiddleware
Recreate ASGIMiddleware's internal event loop after forked WSGI startup
Mar 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ASGIMiddlewarecould hang under pre-fork WSGI servers such as Passenger when the middleware was instantiated before worker fork. In that case, requests could be dispatched against an inherited asyncio loop whose backing thread no longer existed, so the ASGI app never started.Root cause
ASGIMiddlewarecreated and retained a background event loop at initialization time.Behavior change
ASGIMiddlewarenow verifies that its internally managed loop is still valid at call time.Regression coverage
Implementation sketch
Original prompt
This section details on the original issue you should resolve
<issue_title>ASGI app with ASGIMiddleware on passenger_wsgi times out</issue_title>
<issue_description>I am trying to deploy a (test) ASGI app on a server with passenger_wsgi, because that is the only interface they offer.
I wanted to test out FastAPI, but the application hangs. To eliminate any malfunctioning of FastAPI, I replaced it by a super-simple ASGI app. It still hangs.
I used the ASGIMiddleware call to convert the ASGI app to a WSGI app, and put that code in a separate Python file. For debugging I added several print statements to see what is happening.
Here is the app.py, copied from uvicorn.dev.
This is the glue code (wsgi.py). It uses the standard ASGIMiddleware idiom, as documented.
I just wrapped the result of ASGIMiddleware in a debugging function.
Passenger uses
application = wsgi.wappIn the stderr log I see that
wsgi_appreturns agenerator object ASGIResponder.__call__which is correct. But my app is never called, which indicates that the generator is stuck somewhere, before calling the app. I also tried to see if the generator is really called with the following code inside the wapp function in wsgi.py:Instead of returning retval.
app loaded
ASGIMiddleware set up
wsgi_app=<a2wsgi.asgi.ASGIMiddleware object at 0x7fa6e83167b0>
calling wsgi_app with
environ={'wsgi.url_scheme': 'https', 'PATH_INFO': '/', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br, zstd', 'HTTP_ACCEPT_LANGUAGE': 'en-GB,en;q=0.9', 'HTTP_HOST': 'asgiapp.example.com', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.3.1 Safari/605.1.15', 'HTTP_SEC_FETCH_DEST': 'document', 'HTTP_SEC_FETCH_SITE': 'none', 'HTTP_SEC_FETCH_MODE': 'navigate', 'HTTP_PRIORITY': 'u=0, i', 'REMOTE_ADDR': 'xxxx:xxxx::xxxx::0:xxxx::xxxx::xxxx::xxxx:, 'REMOTE_PORT': '50143', 'SERVER_ADDR': 'xxxx::xxxx::xxxx::0:xxxx::xxxx::xxxx::xxxx:', 'SERVER_NAME': 'asgiapp.example.com', 'SERVER_ADMIN': 'webmaster@example.com', 'SERVER_PORT': '443', 'REQUEST_SCHEME': 'https', 'REQUEST_URI': '/', 'HTTPS': 'on', 'X_SPDY': 'HTTP3', 'SSL_PROTOCOL': 'QUIC', 'SSL_CIPHER': 'TLS_AES_256_GCM_SHA384', 'SSL_CIPHER_USEKEYSIZE': '256', 'SSL_CIPHER_ALGKEYSIZE': '256', 'QUERY_STRING': '', 'SCRIPT_URI': 'https://asgiapp.example.com/', 'SCRIPT_URL': '/', 'SCRIPT_NAME': '', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'LiteSpeed', 'REQUEST_METHOD': 'GET', 'X-LSCACHE': 'on', 'wsgi.version': (1, 0), 'wsgi.input': <lsapi_wsgi.InputStream object at 0x7fa6e912ac10>, 'wsgi.errors': <_io.TextIOWrapper name='' mode='w' encoding='ascii'>, 'wsgi.multithread': False, 'wsgi.multiprocess': True, 'wsgi.run_once': False, 'wsgi.file_wrapper': <class 'lsapi_wsgi.FileWrapper'>}
start_response=<built-in method start_response of lsapi_wsgi.Request object at 0x7fa6e81de880>
wsgi_app returned: <generator object ASGIResponder.call at 0x7fa6e82bf040>
[UID:1948][3810995] Child process with pid: 3811020 was killed by signal: 15, core dumped: no