Skip to main content

Overview

PyWorkflow supports graceful workflow cancellation. When you cancel a workflow, it will terminate at the next checkpoint rather than being forcefully killed, allowing for proper cleanup.

Graceful Termination

Workflows stop at safe checkpoints, not mid-operation.

Cleanup Support

Catch CancellationError to perform cleanup before terminating.

Shield Critical Code

Use shield() to protect code that must complete.

CLI & API

Cancel via CLI command or programmatic API.

Cancelling a Workflow

Use cancel_workflow() to request cancellation:

How Cancellation Works

Cancellation in PyWorkflow is checkpoint-based. The workflow is cancelled at the next checkpoint, not immediately.

Cancellation Checkpoints

Cancellation is checked at these points:
Important: Cancellation does NOT interrupt a step that is already executing.If a step takes a long time (e.g., a 10-minute API call), the workflow will only detect cancellation after that step completes. This is by design to avoid leaving operations in an inconsistent state.

Cooperative Cancellation for Long-Running Steps

For steps that run for a long time, you can add cooperative cancellation checks:
The check_cancellation() method is async because in durable mode it queries the storage backend’s cancellation flag, enabling detection of external cancellation requests (e.g., from cancel_workflow()). It checks the in-memory flag first as a fast path, then falls back to storage if needed. This allows the step to respond to cancellation requests between chunks rather than waiting until the entire dataset is processed.

Handling Cancellation

Workflows can catch CancellationError to perform cleanup before terminating:

The shield() Context Manager

Use shield() to protect critical code from cancellation:
While inside a shield() block:
  • await ctx.check_cancellation() will not raise CancellationError
  • The cancellation request is preserved
  • Cancellation will take effect after exiting the shield
Don’t use shield() for long-running operations as it defeats the purpose of graceful cancellation.

Workflow States

When a workflow is cancelled, its status transitions to CANCELLED:

Monitoring Cancelled Workflows

Use the CLI to view cancelled workflows:
Example output:

Best Practices

If your workflow allocates resources or makes changes that need to be reversed, catch CancellationError and clean up:
Only use shield() for truly critical operations like database commits or compensation logic. Long-running shielded operations delay cancellation.
For steps that process large amounts of data, add periodic cancellation checks:
Include a reason when cancelling for better debugging and audit trails:

API Reference

cancel_workflow()

Returns: True if cancellation was initiated, False if workflow is already in a terminal state.

CancellationError

shield()

Context manager that prevents cancellation checks from raising within its scope.

Next Steps

Fault Tolerance

Learn about automatic recovery from worker crashes.

Hooks

Wait for external events in your workflows.

CLI Guide

Manage workflows from the command line.

Sleep

Pause workflows with durable sleep.