discord.py rewrite logout() and close() fail to cleanup task

Created on 3 Apr 2019  路  6Comments  路  Source: Rapptz/discord.py

Summary

The methods logout() and close() don't clean up the task Client._run_event()
I have seen the other entries and it appears the developer doesn't believe this is an issue with discord.py

None of my code appears in the stacktrace and appears to happen specifically due to
Client._run_event()
/usr/local/lib/python3.6/dist-packages/discord.py-1.0.0a1792+gdea3ba5-py3.6.egg/discord/client.py:218

This only happens in the rewrite.
I switched to the rewrite to use cogs and ran into this issue.
It prevents me from properly restarting my bot using a specific flag to make the bot kill itself and restart.

Reproduction Steps

Using discord.py rewrite

discord.__version__
'1.0.0a'

import discord
import asyncio
from config import token
client = discord.Client()

@client.event
async def on_message(message):

    if message.content.startswith("logout"):
        await client.logout()
    elif message.content.startswith("close"):
        await client.close()

@client.event
async def on_ready():
    print("Logged in as " + client.user.name + " /---/ " + str(client.user.id))
client.run(token)`

Expected Results

bot closes with no pending tasks being destroyed.

Actual Results

Task was destroyed but it is pending!
task: wait_for=()]> cb=[shield.._done_callback() at /usr/lib/python3.6/asyncio/tasks.py:688]>
Exception ignored in:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/discord.py-1.0.0a1792+gdea3ba5-py3.6.egg/discord/gateway.py", line 539, in close_connection
File "/home/kyle/.local/lib/python3.6/site-packages/websockets/protocol.py", line 855, in close_connection
File "/home/kyle/.local/lib/python3.6/site-packages/websockets/protocol.py", line 880, in wait_for_connection_lost
File "/usr/lib/python3.6/asyncio/tasks.py", line 342, in wait_for
File "/usr/lib/python3.6/asyncio/base_events.py", line 549, in call_later
File "/usr/lib/python3.6/asyncio/base_events.py", line 559, in call_at
File "/usr/lib/python3.6/asyncio/base_events.py", line 366, in _check_closed
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: wait_for=()]>>

Checklist

  • [X] I have searched the open issues for duplicates.
  • [X] I have shown the entire traceback, if possible.
  • [X] I have removed my token from display, if visible.

System Information

  • discord.py version: '1.0.0a'
  • Python version: 3.6
  • Operating system: Ubuntu Bionic Beaver

Edit(fixed formatting)

Most helpful comment

@EvPayne Since you seem to be acting entitled and trying to pin me for something I'll give you a reply:

  1. This is not an important bug. It does not break anything. These are resource warnings that happen and do not actually cause any issues. Nothing breaks internally or externally and if it did then whatever depends on it is finnicky to begin with.
  2. The branch itself doesn't matter, I don't know what "fork" you're talking about but if you need some brushing up in Git terminology then it's "branch" for the other development branch not "fork".
  3. There is no planet where this is a high priority "bug" that requires the quantifier of "needs to be fixed". There are dozens of much more important bugs open than this one.

Regardless, I took the time to redesign and simplify the loop cleanup process but I haven't tested it in all platforms. Feel free to test it yourself.

All 6 comments

I did report this to Danny and he explained that it isn't a bug, so it isn't his problem. Here is context.

This is an issue with discord.py rewrite.
My code doesn't start the task. His code does.
Saying I'm responsible for the task is like saying I committed murder when someone paralyzes me, uses my hand to pull the trigger on a gun to kill someone without them touching the gun.

The only way to fix this is to edit his code.

Here's some code to use a restart command, although shutdown I don't have..

try:
        await bot.close()
    except:
        pass
    finally:
        os.system("py -3 boot.py")

I use py -3 to boot my bot, if you use python bot.py then change it.

(Edit: I'm currently unaware of the situation but when I saw close I assumed restart so here ya go if you were, you just have to edit some stuff | Also use @client.command(pass_context=True))

@WhoTheOOF This is not relevant to this issue. Don't use os.system to restart your bot. The old interpreter stays open during this call, so over time you will stack endless interpreters, wasting memory.

If you need your bot to restart on exit, write something like a systemd service file so your errors are reported properly and your interpreter is allowed to exit gracefully.

@WhoTheOOF The restart I mentioned isn't really all that important.
I can easily do that by raising an exception and doing a systemctl restart bot

I do plan to integrate my bot with systemd for easy daemonizing

My main focus is trying to understand how this, which is clearly a bug with discord.py is being passed off from the developer as "not a bug"
This needs to be fixed. (important note: IN THE REWRITE)
It doesn't happen in the regular fork.
It only happens in the rewrite. 1.0.0a

@EvPayne Since you seem to be acting entitled and trying to pin me for something I'll give you a reply:

  1. This is not an important bug. It does not break anything. These are resource warnings that happen and do not actually cause any issues. Nothing breaks internally or externally and if it did then whatever depends on it is finnicky to begin with.
  2. The branch itself doesn't matter, I don't know what "fork" you're talking about but if you need some brushing up in Git terminology then it's "branch" for the other development branch not "fork".
  3. There is no planet where this is a high priority "bug" that requires the quantifier of "needs to be fixed". There are dozens of much more important bugs open than this one.

Regardless, I took the time to redesign and simplify the loop cleanup process but I haven't tested it in all platforms. Feel free to test it yourself.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ams2990 picture ams2990  路  14Comments

ThePiGuy24 picture ThePiGuy24  路  17Comments

haaddaa1 picture haaddaa1  路  14Comments

Shea4 picture Shea4  路  21Comments

downloadpizza picture downloadpizza  路  14Comments