-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
asyncio: MicroPython vs. CPython create_task() garbage collection behavior #12299
Comments
My understanding of the code is that MP does not suffer from this behaviour. All tasks are put on the task queue (code) which provides a reference protecting the task from GC. Confirmation from @dpgeorge or @jimmo would be good. For the It would be good if this bug in CPython's |
I have added this note to the tutorial pending definitive confirmation from the maintainers. |
It is both of these things :) We should be providing examples that are also good/valid CPython, but yes, MicroPython does not have this problem because of what Peter said, (and because it does not support weak references). Related to this, CPython's reference counting also enables CPython to do immediate detection of un-waited tasks when the task goes out of scope.
Tasks do implement |
@jimmo Thanks for that. I will amend the sample scripts in the tutorial to save task references for the CPython workround. I can confirm that tasks can now be added to a |
Status update, as of today, 2024-01-15, tested with MicroPython v1.22.1:
|
(Not sure if I should label this a docs bug, feature request, or discussion. Apologies if I missed the mark.)
CPython exhibits a bit of a counterintuitive behavior, which requires users to store the result of
asyncio.create_task()
, otherwise face the danger of the task being garbage collected.Will McGugan of Textual blogged about this here:
https://textual.textualize.io/blog/2023/02/11/the-heisenbug-lurking-in-your-async-code/
Vincent Bernat reported this as a documentation bug against CPython, python/cpython#88831 last year, and subsequently the CPython documentation was adjusted to say:
Is MicroPython exhibiting the same behavior? If so, should the documentation be adjusted?
TypeError: unsupported type for __hash__: 'Task'
. Should__hash__
be added for Tasks? Or should the documentation mention some other way to do this?Not a MicroPython bug per se, but also worth mentioning: @peterhinch's (excellent!) async tutorial also has examples where Tasks are not stored. At one point it's mentioned that "[t]he .create_task method returns the Task instance which may be saved for status checking or cancellation" (emphasis mine). May, or should?
The text was updated successfully, but these errors were encountered: