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

fix: asgi lifespan msg after lifespan context exception #3315

Merged
merged 1 commit into from Apr 6, 2024

Conversation

peterschutt
Copy link
Contributor

An exception raised within an asgi lifespan context manager would result in a "lifespan.startup.failed" message being sent after we've already sent a "lifespan.startup.complete" message. This would cause uvicorn to raise a STATE_TRANSITION_ERROR assertion error due to their check for that condition, if asgi lifespan is forced (i.e., with $ uvicorn test_apps.test_app:app --lifespan on).

E.g.,

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/peter/.local/share/pdm/venvs/litestar-dj-FOhMr-3.8/lib/python3.8/site-packages/uvicorn/lifespan/on.py", line 86, in main
    await app(scope, self.receive, self.send)
  File "/home/peter/.local/share/pdm/venvs/litestar-dj-FOhMr-3.8/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__
    return await self.app(scope, receive, send)
  File "/home/peter/PycharmProjects/litestar/litestar/app.py", line 568, in __call__
    await self.asgi_router.lifespan(receive=receive, send=send)  # type: ignore[arg-type]
  File "/home/peter/PycharmProjects/litestar/litestar/_asgi/asgi_router.py", line 180, in lifespan
    await send(failure_message)
  File "/home/peter/.local/share/pdm/venvs/litestar-dj-FOhMr-3.8/lib/python3.8/site-packages/uvicorn/lifespan/on.py", line 116, in send
    assert not self.startup_event.is_set(), STATE_TRANSITION_ERROR
AssertionError: Got invalid state transition on lifespan protocol.

This PR modifies ASGIRouter.lifespan() so that it sends a shutdown failure message if we've already confirmed startup. This is consistent with starlette's behavior under the same conditions.

Description

Closes

@peterschutt peterschutt requested review from a team as code owners April 5, 2024 10:49
Copy link

codecov bot commented Apr 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.26%. Comparing base (b5d9c6f) to head (a34f92a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3315   +/-   ##
=======================================
  Coverage   98.26%   98.26%           
=======================================
  Files         322      322           
  Lines       14718    14720    +2     
  Branches     2342     2342           
=======================================
+ Hits        14462    14464    +2     
  Misses        117      117           
  Partials      139      139           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@peterschutt peterschutt force-pushed the error-in-asgi-lifespan-context branch from d2fc354 to 0666740 Compare April 5, 2024 22:35
An exception raised within an asgi lifespan context manager would result in a "lifespan.startup.failed" message being sent after we've already sent a "lifespan.startup.complete" message. This would cause uvicorn to raise a `STATE_TRANSITION_ERROR` assertion error due to their [check for that condition][1].

This PR modifies `ASGIRouter.lifespan()` so that it sends a shutdown failure message if we've already confirmed startup. This is consistent with [starlette's behavior][2] under the same conditions.

[1]: https://github.com/encode/uvicorn/blob/a2219eb2ed2bbda4143a0fb18c4b0578881b1ae8/uvicorn/lifespan/on.py#L115-L117
[2]: https://github.com/encode/starlette/blob/4e453ce91940cc7c995e6c728e3fdf341c039056/starlette/routing.py#L744-L745
@peterschutt peterschutt force-pushed the error-in-asgi-lifespan-context branch from 0666740 to a34f92a Compare April 6, 2024 09:31
@peterschutt peterschutt enabled auto-merge (squash) April 6, 2024 09:31
@peterschutt peterschutt merged commit fac641a into main Apr 6, 2024
19 checks passed
@peterschutt peterschutt deleted the error-in-asgi-lifespan-context branch April 6, 2024 09:35
Copy link

sonarcloud bot commented Apr 6, 2024

Copy link

github-actions bot commented Apr 6, 2024

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/3315

peterschutt added a commit that referenced this pull request Apr 8, 2024
An exception raised within an asgi lifespan context manager would result in a "lifespan.startup.failed" message being sent after we've already sent a "lifespan.startup.complete" message. This would cause uvicorn to raise a `STATE_TRANSITION_ERROR` assertion error due to their [check for that condition][1].

This PR modifies `ASGIRouter.lifespan()` so that it sends a shutdown failure message if we've already confirmed startup. This is consistent with [starlette's behavior][2] under the same conditions.

[1]: https://github.com/encode/uvicorn/blob/a2219eb2ed2bbda4143a0fb18c4b0578881b1ae8/uvicorn/lifespan/on.py#L115-L117
[2]: https://github.com/encode/starlette/blob/4e453ce91940cc7c995e6c728e3fdf341c039056/starlette/routing.py#L744-L745

(cherry picked from commit fac641a)
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 this pull request may close these issues.

None yet

2 participants