Socket closing error #1803
-
In my project I use gunicorn with uvicorn worker class.
Minimal reproducible example:Requirements:
Launch: gunicorn --worker-class "uvicorn.workers.UvicornWorker" "main:app" main.py from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return "Hello world" Send HUP signal: kill -s HUP {gunicorn master pid} Possible solution:It seems to be a bug in the socket closing sequence and I may have a solution. server.py: async def shutdown(self, sockets: Optional[List[socket.socket]] = None) -> None:
logger.info("Shutting down")
# Stop accepting new connections.
for server in self.servers:
server.close()
for sock in sockets or []:
sock.close()
for server in self.servers:
await server.wait_closed()
# Request shutdown on all existing connections.
for connection in list(self.server_state.connections):
connection.shutdown()
await asyncio.sleep(0.1) fixed server.py: async def shutdown(self, sockets: Optional[List[socket.socket]] = None) -> None:
logger.info("Shutting down")
# Stop accepting new connections.
for sock in sockets or []:
sock.close()
for server in self.servers:
server.close()
for server in self.servers:
await server.wait_closed()
# Request shutdown on all existing connections.
for connection in list(self.server_state.connections):
connection.shutdown()
await asyncio.sleep(0.1) I know it may seem like a small thing, but it would get rid of the misunderstanding. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This was intentionally introduced to support "shutdown" events using You can read more about it here: #1710. I've made |
Beta Was this translation helpful? Give feedback.
This was intentionally introduced to support "shutdown" events using
gunicorn
.You can read more about it here: #1710.
I've made
gunicorn
aware of my intentions: benoitc/gunicorn#1877 (comment).