sunhailin-Leo / fastapi_profiler

A FastAPI Middleware of https://github.com/joerick/pyinstrument to check your service performance.
MIT License
231 stars 13 forks source link

Error | AttributeError: 'NoneType' object has no attribute 'root_frame' #17

Closed kbhatiya999 closed 5 months ago

kbhatiya999 commented 5 months ago

This is what get thrown if server is stopped without sending any request.


INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8081 (Press CTRL+C to quit)
^CINFO:     Shutting down
INFO:     Waiting for application shutdown.
ERROR:    Traceback (most recent call last):
  File "/home/kas/.cache/pypoetry/virtualenvs/orchestrator-0coqT9JI-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 734, in lifespan
    async with self.lifespan_context(app) as maybe_state:
  File "/home/kas/.cache/pypoetry/virtualenvs/orchestrator-0coqT9JI-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 613, in __aexit__
    await self._router.shutdown()
  File "/home/kas/.cache/pypoetry/virtualenvs/orchestrator-0coqT9JI-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 721, in shutdown
    await handler()
  File "/home/kas/.cache/pypoetry/virtualenvs/orchestrator-0coqT9JI-py3.11/lib/python3.11/site-packages/fastapi_profiler/profiler.py", line 174, in get_profiler_result
    f.write(renderer.render(session=self._profiler.last_session))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kas/.cache/pypoetry/virtualenvs/orchestrator-0coqT9JI-py3.11/lib/python3.11/site-packages/pyinstrument/renderers/speedscope.py", line 210, in render
    frame = self.preprocess(session.root_frame())
                            ^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'root_frame'

ERROR:    Application shutdown failed. Exiting.
INFO:     Finished server process [1002483]
kbhatiya999 commented 5 months ago

`

Adds startup and shutdown events.

app.add_middleware(
    PyInstrumentProfilerMiddleware,
    server_app=app,  # Required to output the profile on server shutdown
    profiler_output_type="speedscope",
    is_print_each_request=False,  # Set to True to show request profile on
    # stdout on each request
    open_in_browser=False,  # Set to true to open your web-browser automatically
    # when the server shuts down
    html_file_name="speedscope.json",  # Filename for output
    async_mode="strict",
    show_all=True,
)