Skip to content

Commit

Permalink
Improve shutdown process in Multiprocess mode
Browse files Browse the repository at this point in the history
Explicitly clos listening sockets in both parent and child processes early in
the shutdown process.  That way, no new connections can hit workers after
shutdown is initiated by the parent process.

While here, speedup shutdown by splitting process shutdown into terminate and
join loop, so all processes are shutdown in parallel.
  • Loading branch information
rbtz-openai committed Jun 17, 2023
1 parent d62a537 commit 2782d16
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions uvicorn/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ 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:
server.close()
for server in self.servers:
await server.wait_closed()

Expand Down
5 changes: 5 additions & 0 deletions uvicorn/supervisors/multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ def startup(self) -> None:
self.processes.append(process)

def shutdown(self) -> None:
for socket in self.sockets:
socket.close()

for process in self.processes:
process.terminate()

for process in self.processes:
process.join()

message = "Stopping parent process [{}]".format(str(self.pid))
Expand Down

0 comments on commit 2782d16

Please sign in to comment.