mirror of
https://github.com/open-webui/mcpo.git
synced 2026-07-23 12:15:22 -04:00
[PR #162] [MERGED] Gracefully shutdown uvicorn.Server on task cancellation in run() #180
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/mcpo/pull/162
Author: @021gink
Created: 5/28/2025
Status: ✅ Merged
Merged: 6/6/2025
Merged by: @tjbck
Base:
main← Head:graceful-uvicorn-shutdown📝 Commits (1)
101c513Gracefully shutdown uvicorn.Server on task cancellation in run()📊 Changes
1 file changed (+7 additions, -1 deletions)
View changed files
📝
src/mcpo/main.py(+7 -1)📄 Description
Summary
This PR updates the
run()function to properly shut down theuvicorn.Serverwhen the task is cancelled viatask.cancel().Motivation
Previously, cancelling the task running
run()did not stop theuvicornserver, leaving the socket bound to the port and causing issues when trying to restart or cleanly terminate the server. This change handlesasyncio.CancelledErrorand triggersserver.should_exit = Trueandawait server.shutdown()to release resources.Changes
try/except asyncio.CancelledErrorblock aroundserver.serve()server.should_exitis set andserver.shutdown()is awaited before re-raising the cancellationTesting
Tested locally by launching
run()viaasyncio.create_task()and then cancelling it. Confirmed that the uvicorn server exits cleanly and releases the port.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.