Skip to content

[Solved] Uvicorn Closed In Container - HTTP connection lost. Shutting down, exit 0

Last Updated on 2024-10-14 by Clay

Problem Description

Today, while I was using podman to create a container (from a FastAPI image) to run my FastAPI service, I encountered an issue where the container would automatically stop if the user logged out or if there were no HTTP POST requests sent to the API for a while. After some time, the service would stop.

Using podman ps, I found that the container had already stopped, with the final state being exit 0 — indicating that it had exited normally, without any unusual error messages.

Since my service was started using FastAPI with Python, I used logging and debug tools to trace the issue. The only message I found was:

HTTP connection lost.
Shutting down...


I searched online and found discussions on Stackoverflow, which eventually led me to two methods that successfully solved the issue of the container's service shutting down automatically.


Solutions

  1. Since my ASGI service was running with uvicorn, I added an extra parameter to the uvicorn command --timeout-keep-alive 99999999. I set a large value to prevent the service from shutting down automatically (the default is 5 seconds). This worked, and the service stopped shutting down. However, many of my colleagues criticized this as a strange solution.
  2. Updating podman. For instance, on Ubuntu, the version might be 3.x.x, but the actual version can go up to 4.x.x. Updating podman and restarting the service resolved the issue.

References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version