Skip to content
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

Killing greenlet returned from future_to_greenlet throws AttributeError: 'coroutine' object has no attribute 'cancel' #11

Open
OpaqueOrangutan opened this issue Feb 15, 2023 · 1 comment

Comments

@OpaqueOrangutan
Copy link

OpaqueOrangutan commented Feb 15, 2023

I'm chasing down a 100% CPU consumption problem that is somewhere in gevent's hub run while running greenlets from future_to_greenlet.

While investigating, I wanted to make sure that greenlets were killed after being joined. (For context, I'm creating and running these futures in parallel using locust so it's a bit tricky to make sure everything is getting cleaned up.)

My code looked something like this:

future = my_async_function()
greenlet = asyncio_gevent.future_to_greenlet(future)
greenlet.start()
greenlet.join(timeout=timeout_seconds)
greenlet.kill()

But I was receiving an AttributeError: 'coroutine' object has no attribute 'cancel' during greenlet.kill(), thrown from https://github.com/gfmio/asyncio-gevent/blob/main/asyncio_gevent/future_to_greenlet.py#L61

Should this instead be future.close()? Coroutine documentation is here. This seems to work for me. My example now looks like:

future = my_async_function()
greenlet = asyncio_gevent.future_to_greenlet(future, autocancel_future=False)
greenlet.start()
greenlet.join(timeout=timeout_seconds)
greenlet.kill()
future.close()
@gfmio
Copy link
Owner

gfmio commented Mar 4, 2023

Thanks for raising this issue. I think the problem is that I expected the object in the file you link ed to be a future, not a coroutine object. asyncio.Future objects have a cancel() method.

To fix your code right now, I would suggest using asyncio.wrap_future to wrap your coroutine object inside a future.

And separately, I'll update the library to also support coroutine objects, because I can see why this would be unexpected/confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants