Description
Summary
After I see the connection reset stack trace, I've noticed that while everything else is working fine, tasks simply do not ever loop again.
Reproduction Steps
Sadly I have been struggling to reproduce this but I see it in the wild every now and then.
Minimal Reproducible Code
I have some tasks in my codebase that are set up like so:
from discord.ext import commands, tasks
import MyBot
class TasksCog(commands.Cog):
def __init__(self, bot: MyBot):
self.bot = bot
self.my_task.start()
@tasks.loop(minutes=5)
async def my_task(self):
# do stuff...
# await interaction.[...]
@my_task.before_loop
async def before_my_task(self):
await self.bot.wait_until_ready()
async def setup(bot: MyBot):
await bot.add_cog(TasksCog(bot))
Expected Results
Connection resets happen, sometimes the network is flakey. When this happens the bot should attempt to reconnect and carry on.
Actual Results
This seems to work just fine most of the time. Every now and then though I get this stack trace. When I do it means that my tasks have stopped looping. The bot continues to work just fine in all other respects, but the tasks completely stop happening. In my example code the my_task()
code should execute every 5ish minutes. But when I see this stack trace it means that my_task()
will never execute again. It just stops looping tasks completely.
future: <Task finished name='Task-10' coro=<ConnectionState._delay_ready() done, defined at /usr/local/lib/python3.10/site-packages/discord/state.py:533> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/discord/state.py", line 545, in _delay_ready
future = await self.chunk_guild(guild, wait=False)
File "/usr/local/lib/python3.10/site-packages/discord/state.py", line 1172, in chunk_guild
await self.chunker(guild.id, nonce=request.nonce)
File "/usr/local/lib/python3.10/site-packages/discord/state.py", line 510, in chunker
await ws.request_chunks(guild_id, query=query, limit=limit, presences=presences, nonce=nonce)
File "/usr/local/lib/python3.10/site-packages/discord/gateway.py", line 730, in request_chunks
await self.send_as_json(payload)
File "/usr/local/lib/python3.10/site-packages/discord/gateway.py", line 658, in send_as_json
await self.send(utils._to_json(data))
File "/usr/local/lib/python3.10/site-packages/discord/gateway.py", line 654, in send
await self.socket.send_str(data)
File "/usr/local/lib/python3.10/site-packages/aiohttp/client_ws.py", line 151, in send_str
await self._writer.send(data, binary=False, compress=compress)
File "/usr/local/lib/python3.10/site-packages/aiohttp/http_websocket.py", line 690, in send
await self._send_frame(message, WSMsgType.TEXT, compress)
File "/usr/local/lib/python3.10/site-packages/aiohttp/http_websocket.py", line 646, in _send_frame
self._write(header + mask + message)
File "/usr/local/lib/python3.10/site-packages/aiohttp/http_websocket.py", line 663, in _write
raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
Maybe relevant, just before this stack trace is a bunch of logs like:
[2022-11-17 00:28:28][discord.gateway][WARNING] - WebSocket in shard ID None is ratelimited, waiting 59.98 seconds
This exception also seems to happen after my bot is restarted. If it doesn't happen just after my bot is started, then it doesn't ever seem to happen. Whenever I see it, it's always just after the bot has been restarted.
Intents
3276543
System Information
- Python v3.10.5-final
- discord.py v2.0.1-final
- aiohttp v3.8.1
- system info: Darwin 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
Checklist
- I have searched the open issues for duplicates.
- I have shown the entire traceback, if possible.
- I have removed my token from display, if visible.
Additional Context
Here's the actual tasks cog code from my bot: https://github.com/lexicalunit/spellbot/blob/main/src/spellbot/cogs/tasks_cog.py