Skip to content

Improve TimerTask#122

Merged
berndverst merged 12 commits intomicrosoft:mainfrom
andystaples:andystaples/add-task-cancellation
Apr 8, 2026
Merged

Improve TimerTask#122
berndverst merged 12 commits intomicrosoft:mainfrom
andystaples:andystaples/add-task-cancellation

Conversation

@andystaples
Copy link
Copy Markdown
Contributor

Adds the following to improve TimerTask:

  1. Adds support for task cancellation, enabling patterns like:
    def orchestrator(ctx: task.OrchestrationContext, _):
        approval: task.Task[bool] = ctx.wait_for_external_event('Approval')
        timeout = ctx.create_timer(timedelta(seconds=3))
        winner = yield task.when_any([approval, timeout])
        if winner == approval:
            # Explicitly cancel the timer so it does not linger
            timeout.cancel()
            return "approved"
        else:
            return "timed out"
  1. Adds support for LongTimers with configurable max_timer_interval, so that backends with limits on timer duration can be used

Resolves #95
Resolves #101

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances the Durable Task Python SDK’s timer functionality by adding (1) task cancellation support for timers/external-event waits and (2) long-timer chunking to accommodate backends with maximum timer duration limits (resolving #95 and #101).

Changes:

  • Introduces CancellableTask/TaskCanceledError and wires cancellation into timer + external event tasks.
  • Adds LongTimerTask and updates orchestration execution to chunk long timers based on a maximum interval (default 3 days).
  • Expands orchestration executor tests to cover long-timer chunking, cancellation after when_any, and long retry timers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
durabletask/worker.py Implements long-timer chunk scheduling in the runtime context and executor; adds a maximum_timer_interval configuration knob.
durabletask/task.py Adds cancellable task primitives and long-timer task implementation.
tests/durabletask/test_orchestration_executor.py Adds unit tests for long-timer chunking/cancellation and retry behavior with long timers.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
Copy Markdown
Member

@berndverst berndverst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task cancellation support is a great addition — the CancellableTask pattern with cancel handlers for timers and external events is well-designed and the tests are thorough.

However, the long timer implementation introduces a separate LongTimerTask class that diverges from the .NET design. In the .NET SDK (TaskOrchestrationContextWrapper.cs#L266-L283), long timer chunking is handled entirely within the CreateTimer method using a simple loop — there is no separate timer type. The chunking is an implementation detail of the orchestration context, not a property of the task itself.

In Python we should not have two different classes for timers. Instead, the existing TimerTask should be updated to support long timers internally. The chunking logic should live in create_timer_internal and the timer-fired event handler, with TimerTask optionally holding the final_fire_at and maximum_timer_interval state when needed. This keeps the type hierarchy simple and aligns with the .NET design where the long timer behavior is transparent to consumers.

Note: The PR has a merge conflict (mergeable_state: dirty).

Disclaimer: This review was generated by GitHub Copilot on behalf of Bernd.

@berndverst berndverst merged commit 7c82f24 into microsoft:main Apr 8, 2026
15 checks passed
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

Successfully merging this pull request may close these issues.

Add support for long timers Provide an API to cancel a Task

3 participants