Coder Social home page Coder Social logo

Comments (5)

minrk avatar minrk commented on June 12, 2024 1

Yup, I got it. The short answer is that we are trying to cancel coroutines, which are unscheduled tasks, when only Futures can be cancelled. The quick fix is to call task = ensure_future(task) to ensure the coroutines are actually scheduled tasks which can be cancelled (this would be a no-op if they were already Futures).

The bigger question is whether we should be cancelling them in the first place. It might actually make more sense to wait for them all to complete before raising. But that could end up delaying reporting of an error.

from kubespawner.

minrk avatar minrk commented on June 12, 2024 1

If these are futures, won't we then also see additional "CancellationErrors" raised from them when doing .cancel() on them?

You don't generally see CancelledError messages unless you await a future that has been cancelled (usually a bug, since once you've cancelled it, you shouldn't be awaiting it). If you're inside a task being cancelled, there will be no traceback.

For example:

import asyncio
async def printer(name):
    for i in range(10):
        await asyncio.sleep(1)
        print(name)
        
async def main():
    tasks = [
        asyncio.ensure_future(
            printer(name))
            for name in ("1", "2", "3")
        ]
    # create single task wrapping subtasks
    gather_task = asyncio.gather(*tasks)
    print("waiting")
    try:
        await asyncio.wait_for(gather_task, timeout=1.5)
    except asyncio.TimeoutError:
        print("timeout waiting (expected)")
    # ensure all the sub-tasks are cancelled
    # when wait_for hits timeout, it cancels gather_task
    # when gather_task is cancelled, it cancels all of its sub-tasks
    assert gather_task.done()
    for task in tasks:
        assert task.done()
        assert task.cancelled()
        
    # any of `await task` here would raise CancelledError,
    # but no other
    print("all done and cancelled")

if __name__ == "__main__":
    asyncio.run(main())

will produce the output:

waiting
1
2
3
timeout waiting (expected)
all done and cancelled

No tracebacks or anything. If any code were to await task for one of the cancelled tasks after it was cancelled, it would raise CancelledError

from kubespawner.

consideRatio avatar consideRatio commented on June 12, 2024

@minrk I think you have the asyncio experience to more clearly understand what is going on and how to handle it properly, can you help?

I figure there is a difference between a future and a coroutine, and we are using future logic on coroutines here. Do we need the coroutines to be made into futures? Do we need to do .cancel() if one future of them has errored?

from kubespawner.

consideRatio avatar consideRatio commented on June 12, 2024

Based on https://superfastpython.com/asyncio-gather/#Example_of_Canceling_All_Tasks_in_gather, is the action to first stash the future returned by asyncio.gather in a variable, and then await it, allowing us to do .cancel() on the future variable?

Hmm... I can't think clearly about this. What is the key motivation for doing the .cancel() logic in the first place before re-raising? If these are futures, won't we then also see additional "CancellationErrors" raised from them when doing .cancel() on them?

from kubespawner.

consideRatio avatar consideRatio commented on June 12, 2024

Ah, thank you again @minrk - now I feel quite confident about the cancellation errors!!

from kubespawner.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.