socketify.py icon indicating copy to clipboard operation
socketify.py copied to clipboard

Not able to run Litestar app with lifespan

Open euri10 opened this issue 2 years ago • 1 comments

Describe the bug lifespan crashes without any interesting info

To Reproduce Steps to reproduce the behavior:

run that simple app, it runs ok with lifespan=False and crashes with lifespan=True ERROR - 2023-08-24 16:04:36,385 - root - asgi - Uncaught Exception: unknown async library, or not in async context

from contextlib import asynccontextmanager
from functools import partial
from typing import AsyncGenerator

from litestar import Litestar, get, Request
from socketify import ASGI


@asynccontextmanager
async def lifespan(_app: Litestar) -> AsyncGenerator[None, None]:
    print(1)
    yield
    print(2)


def get_app() -> Litestar:
    @get("/")
    async def index(request: Request) -> int:
        return 1

    app = Litestar(route_handlers=[index],
                   lifespan=[partial(lifespan)]
                   )

    return app


if __name__ == "__main__":
    app = get_app()
    ASGI(app, lifespan=True).listen(8000).run(1)

Expected behavior well it should not crash

Desktop (please complete the following information):

  • OS: [debian11]
  • Browser [chrome]
  • Version [Version 116.0.5845.96 (Official Build) (64-bit)]

euri10 avatar Aug 24 '23 14:08 euri10

seems like it has to do with TaskGroup creation is there anything we can provide to help on this ?

euri10 avatar Jan 02 '24 05:01 euri10

Fixed in the main branch

cirospaciari avatar Jul 13 '24 19:07 cirospaciari