Skip to content

Need way to cleanly exit asyncio loop #821

Open
@koitsu

Description

@koitsu

Summary

See title

What is the feature request for?

The core library

The Problem

Currently there is no way to cleanly shut down a discord.py-self client. I am not talking about SIGINT/Ctrl-C, I am talking about Client.run() which effectively invokes asyncio.run(runner()) and is stuck an in asyncio event loop forever.

While I understand this library is mainly intended for self-bots, I have several applications that are effectively "one-shot"; that is to say, they connect to Discord, do some work, then need to properly exit: no exceptions being thrown, no non-zero exit codes, etc..

What I've already tried, all done within on_ready:

  • await self.close() -- closes socket but never exits asyncio loop. Not to mention, SIGINT after this point generates asyncio complaints about tasks not being retried etc. as as result of WS being closed
  • raise Exception('go away') -- generates exception but never exits asyncio loop
  • quit() -- works but generates an exception of SystemExit: None in asyncio library

I've also tried the above with an on_disconnect handler, but that never gets invoked. I suspect that is for a different kind of disconnection. It's not immediately clear to me what this refers to (WS? Gateway? Actual TCP socket?) but I'm not familiar with Discord protocol.

Finally: I am using discord.py-self from master branch, i.e. https://github.com/dolfies/discord.py-self/archive/refs/heads/master.tar.gz , so I can test change proposals or other branches if necessary.

The Ideal Solution

I imagine this could be implemented through a parameter on run/start/login() -- something like one_shot=True -- which would change the mechanism used in the asyncio loop. This might need to imply reconnect=False which is OK. If its possible to do, maybe exit with a non-zero exit code if socket was closed by remote end (rather than client-requested closure).

Various references that may help implement a solution:

The Current Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions