-
-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Task garbage collection - Heisenbug #2179
Comments
I am experiencing precisely this bug and I got to the same conclusion after doing some research! I'm getting one of these every one or two days at random, usually on slash commands that take a while to run (after responding the interaction).
In some cases, the tasks appear to get garbage collected while awaiting one or other time consuming coroutine. This usually results in getting GeneratorExit raised as well. |
With some guidance I could attempt a fix as I'm not very familiar with pycord's codebase. Please correct me if I'm wrong, but in the case of event scheduling wouldn't storing the task in a set (possibly named Client.event_scheduled_tasks), and adding a done callback that removes the task from the set before returning it here be enough? Line 418 in 845f7c7
|
I have saw that same error happening for the on_ready event. But not very consistent or easy to reproduce. It did it a lot more in on_connect when I was trying to initialte the db connections(had to do the db init somwhere else) |
I have the same issue |
I am also facing the same issues.. pretty bad that some users thought our app doesnt work after failed 3 times in a row |
Okay it seems like this problem is coming up every time with a specific command in my bot now (it is a bit long running), I'm not sure what changed (code was kept the same for this command). With some guidance I could attempt a pr for this. @Lulalaby @Dorukyum
|
I'm unsure about that tbh. |
That would work. Just make sure the tasks get removed from the set (through a done callback) after they're done. |
Summary
When creating tasks with
create_task
, PyCord does not save a reference to a task.Reproduction Steps
When creating tasks with
create_task
, PyCord does not save a reference to a task, making it a targetfor the garbage collector, if the task is doing long and complex work.
This is well documented by the
asyncio.create_task
:Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done. For reliable “fire-and-forget” background tasks, gather them in a collection
This can eventually result in a "Task was destroyed but it is pending!" error being printed out on screen, because the task was garbage collected. It could also just go though without warning.
Minimal Reproducible Code
Expected Results
The library should save references to task and only remove the reference after the task has been finished.
Actual Results
It doesn't, giving opportunity for the task to be destroyed during execution.
Intents
No intents were passed
System Information
Checklist
Additional Context
No response
The text was updated successfully, but these errors were encountered: