Skip to content

Cancelling coroutines: can't pend throw to just-started generator.  #5242

@kevinkk525

Description

@kevinkk525

My code heavily relies on coroutines which get canceled quite often.
Occasionally it happens, that a coroutine gets put on the loop but then canceled before it gets executed for the first time.
Apparently this is not possible as it throws a TypeError:

import uasyncio as asyncio
>>> async def test():
...     print("test")
>>> t=test()
>>> async def cancel():
...     asyncio.cancel(t)
>>> loop=asyncio.get_event_loop()
>>> loop.create_task(cancel())
>>> loop.create_task(t)
>>> loop.run_forever()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "uasyncio/core.py", line 109, in run_forever
  File "<stdin>", line 2, in cancel
  File "uasyncio/core.py", line 273, in cancel
TypeError: can't pend throw to just-started generator

This is incompatible to CPython and causes some problems for me because suddenly there is no way to cancel this coroutine unless I wait until it yields for the first time.
An example of where this might happen is if an asyncio task is created when an MQTT connection is established and the same task gets cancelled when the connection breaks. So if the connection breaks until the task gets executed, it would run into this error.

Is this fixable or just the way generators work in micropython?

Metadata

Metadata

Assignees

No one assigned

    Labels

    py-coreRelates to py/ directory in source

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy