[PR #162] [MERGED] Gracefully shutdown uvicorn.Server on task cancellation in run() #191

Closed
opened 2026-10-18 21:12:33 +00:00 by bsakaguchi · 0 comments
Owner

📋 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)

  • 101c513 Gracefully 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 the uvicorn.Server when the task is cancelled via task.cancel().

Motivation

Previously, cancelling the task running run() did not stop the uvicorn server, leaving the socket bound to the port and causing issues when trying to restart or cleanly terminate the server. This change handles asyncio.CancelledError and triggers server.should_exit = True and await server.shutdown() to release resources.

Changes

  • Added try/except asyncio.CancelledError block around server.serve()
  • On cancellation, server.should_exit is set and server.shutdown() is awaited before re-raising the cancellation

Testing

Tested locally by launching run() via asyncio.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.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/mcpo/pull/162 **Author:** [@021gink](https://github.com/021gink) **Created:** 5/28/2025 **Status:** ✅ Merged **Merged:** 6/6/2025 **Merged by:** [@tjbck](https://github.com/tjbck) **Base:** `main` ← **Head:** `graceful-uvicorn-shutdown` --- ### 📝 Commits (1) - [`101c513`](https://github.com/open-webui/mcpo/commit/101c513aebf2744225a17d51deef27f2a2d98ea4) Gracefully shutdown uvicorn.Server on task cancellation in run() ### 📊 Changes **1 file changed** (+7 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/mcpo/main.py` (+7 -1) </details> ### 📄 Description ### Summary This PR updates the `run()` function to properly shut down the `uvicorn.Server` when the task is cancelled via `task.cancel()`. ### Motivation Previously, cancelling the task running `run()` did not stop the `uvicorn` server, leaving the socket bound to the port and causing issues when trying to restart or cleanly terminate the server. This change handles `asyncio.CancelledError` and triggers `server.should_exit = True` and `await server.shutdown()` to release resources. ### Changes - Added `try/except asyncio.CancelledError` block around `server.serve()` - On cancellation, `server.should_exit` is set and `server.shutdown()` is awaited before re-raising the cancellation ### Testing Tested locally by launching `run()` via `asyncio.create_task()` and then cancelling it. Confirmed that the uvicorn server exits cleanly and releases the port. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-18 21:12:33 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
open-webui/mcpo#191
No description provided.