emmett-framework / granian

A Rust HTTP server for Python applications
BSD 3-Clause "New" or "Revised" License
2.86k stars 83 forks source link

Granian won't log or exit on ASGI lifespan startup event failures #147

Closed wihlarkop closed 11 months ago

wihlarkop commented 12 months ago

i got this issue when running granian with fastapi, server is running but can't open/blank in browser

image

my requirements.txt

fastapi==0.104.1
psycopg2==2.9.9
pydantic==2.4.2
pydantic-settings==2.0.3
python-dotenv==1.0.0
SQLAlchemy==2.0.23
alembic==1.12.1
granian==0.7.2

image

this is my main.py file

class Client:
    def __init__(self):
        self.connection_url: URL = connection_url
        self.engine: Engine = create_engine(url=self.connection_url, pool_pre_ping=True)
        self.metadata = MetaData()

    def get_engine(self) -> Engine:
        return self.engine

    def disconnect(self):
        self.engine.dispose()

client = Client()

@asynccontextmanager
async def lifespan(app: FastAPI):
    db_client = client.get_engine()
    with db_client.connect() as connection:
        yield {
            "db": connection
        }
    client.disconnect()

app = FastAPI(
    title=settings.APP_NAME,
    version=settings.APP_VERSION,
    lifespan=lifespan,
)

if __name__ == "__main__":
    Granian(
        target="main:app",
        interface=Interfaces.ASGI,
        reload=True
    ).serve()

and i run python main.py

ameyer117 commented 12 months ago

I believe this is because Granian doesn't have lifespan support yet.

You can use the old startup events as a workaround

gi0baro commented 11 months ago

@wihlarkop @ameyer117 yep I suppose this needs #94, but I didn't check if the lifespan parameter of starlette (and thus FastAPI) requires that. Probably someone like @Kludex can confirm this.

Kludex commented 11 months ago

The OP is not pointing out what is the error on the server side...

I think Starlette warns if the server doesn't support lifespan state. I would need to confirm (I'm afk).

wihlarkop commented 11 months ago

Hi @Kludex

there is no log or error when i start the server, when i access 127.0.0.1:8000 it just blank page like my ss and in cli there is no error or warning too, even when i add log_enabled=True in granian still no error log image

wihlarkop commented 11 months ago

@ameyer117 it can work with your solution , but on_event workaround will deprecated so i want to migrate to lifespan event handlers

Kludex commented 11 months ago

Does granian log error if startup has errors?

wihlarkop commented 11 months ago

@Kludex in my case granian just give log like this

[INFO] Starting granian
[INFO] Listening at: 127.0.0.1:8000
[INFO] Spawning worker-1 with pid: 10501

if you want try my code you can access this https://github.com/wihlarkop/fastapi-granian

gi0baro commented 11 months ago

Hey all, I debugged this locally.

So there are two themes here:

The actual issue here is that Granian is actually catching the exception and exiting the worker process without logging or re-raising anything, and since there's no fine-grained process management today in granian, the main process is kept alive even if the actual worker prematurely exited (this is a theme also in #148 ).

So in conclusion: