Description
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 closedraise Exception('go away')
-- generates exception but never exits asyncio loopquit()
-- works but generates an exception ofSystemExit: 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:
- Cannot cleanly shut down an asyncio based server python/cpython#113538
- https://stackoverflow.com/questions/39351988/python-asyncio-program-wont-exit
- https://superfastpython.com/asyncio-event-loop-exit/
The Current Solution
No response
Additional Context
No response