Skip to content

asyncio: add Task.cancelling_since() to expose first cancellation timestamp #150060

@prince8273

Description

@prince8273

Summary

Add asyncio.Task.cancelling_since(), a method returning the event loop time
at which Task.cancel() was first called, or None if cancellation has not
been requested.

Motivation

Task.cancelling() exposes how many cancellation requests were made, but not
when the first request happened. That makes it difficult to:

  • measure how long a task remains pending after cancellation is requested
  • debug timeout and cancellation latency
  • emit metrics around delayed cancellation handling in long-running async systems

Proposed API

task.cancelling_since() -> float | None

Returns loop.time() captured at the first cancel() call, or None if
cancel() has not been called. Repeated cancel() calls do not overwrite the
original timestamp.

Notes

This records when cancellation was requested, not when the task actually
finished cancelling.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions