Sitelet https://github.com/python/cpython/issues/113538
Skip to content

Cannot cleanly shut down an asyncio based server #113538

Description

@CendioOssman

Bug report

Bug description:

When writing an asyncio based service, you basically have this sequence:

  1. Create an event loop
  2. Register a SIGTERM handler
  3. Start your server
  4. loop.run_forever()
  5. SIGTERM causes a loop.stop()
  6. Close the server
  7. Close event loop

If there are any connections active at this point, then they don't get discarded until interpreter shutdown, with the result that you get a bunch of ResourceWarnings (and cleanup code might not run).

It would be very useful if there was a Server.close_clients() or something like that. Even a Server.all_transports() would be useful, as then you could do something similar as when doing a Task.cancel() on what you get from loop.all_tasks().

We could poke at Server._transports, but that is something internal that might change in the future.

There is Server.wait_closed(), but that hangs until all clients have gracefully disconnected. It doesn't help us when we want to shut down the service now.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions