Cancellation & graceful shutdown

CancelledError is a request, not a bug

When a client disconnects mid-request, or the timeout from lesson 2 fires, or the server is shutting down โ€” Python cancels the running coroutine by raising asyncio.CancelledError at whatever await it's currently sitting on. It is not an error condition you caused; it's asyncio telling your code "stop now, please." The correct response is almost always: clean up, then let it propagate.

This is why except Exception: is safe but a bare except: is dangerous. CancelledError inherits from BaseException, not Exception (since Python 3.8, specifically so broad handlers wouldn't accidentally eat it). except Exception: lets it pass through untouched. A bare except: โ€” or except BaseException: without re-raising โ€” swallows it, and now the task thinks it finished normally when the loop believes it's cancelled. That mismatch is how you get connections that never close and shutdowns that hang.