Repository navigation
Breaking: propagate Python Strands tool cancellation - #497
Merged
Merged
Conversation
campoy
added a commit
that referenced
this pull request
Nov 14, 2026
… heartbeat timeout (#504) ## What was changed `TemporalAgent`, `TemporalModel` and `TemporalSandbox` now default `heartbeat_timeout` to 10 seconds instead of `None`, so the model and sandbox activities heartbeat by default. - The activities already heartbeat on a timer at half the timeout (`auto_heartbeater`). A timeout you set wins, and `heartbeat_timeout=None` turns heartbeats off, as before. - The timer now starts when the activity does, not at its first `await`. Before, a model factory that blocked the event loop for over half the timeout made the attempt fail with a heartbeat timeout. Now it takes the whole timeout, which is when the warning below fires. - `strands_get_model_config` is now scheduled without a heartbeat timeout. It builds the model and reads its config synchronously, so it can't heartbeat, and a model factory slower than the timeout would fail it. - A model factory slower than the heartbeat timeout now logs a warning on the worker. The factory runs on the worker's event loop, so the heartbeat timer can't run while it builds the model, and that attempt may fail with a heartbeat timeout. The next attempt reuses the built model. - The docstrings and the README say what the default does and how to turn it off. They also say that a model factory or model call that blocks the event loop longer than the timeout fails. In Strands 1.56.0, `SageMakerAIModel` and `LlamaAPIModel` call their synchronous clients on the loop, so they need `heartbeat_timeout=None` (or a longer timeout). `activity_as_tool` and `activity_as_hook` run your own Activities, so they stay as they are. ### Why not build models on a thread? An automated review round suggested it, so a slow factory couldn't block the heartbeat timer. But that broke factories that need a running event loop, such as one that builds an `aiohttp` session, and the fix for that made this PR much bigger. A factory slower than 10 seconds is rare, and the retry reuses the model it built, so a warning that says what happened is enough. ## Why? Without a heartbeat timeout, these activities never heartbeat. So a cancel never reaches a running model call or sandbox command, and it runs to the end. A lost worker also goes unnoticed until the start-to-close timeout. #491 has the details, and the measurements behind 10 seconds. Does this break anything? Not replay: when core replays a scheduled Activity, it only compares its ID and type, and the replay tests pass. But a call that didn't set `heartbeat_timeout` now heartbeats, which in Temporal Cloud is at most one Action every 8 seconds of a running call. `TemporalMCPClient` isn't in this PR. Since #435, Strands requires `temporalio-mcp` 0.4, whose Activities heartbeat (#494). But Strands' `TemporalMCPClient` still passes `heartbeat_timeout=None`, so its Activities don't heartbeat yet. A follow-up can give it the 10-second default too. This works with #497, which makes a cancelled tool call cancel the agent. I merged its branch on top of this one, and its cancellation tests pass with these defaults. ## Checklist 1. Closes #498 2. How was this tested: - `test_model` now checks the heartbeat timeouts in History: none for `strands_get_model_config`, 10 seconds for `invoke_model`. Without the change it fails with `assert [None, None] == [None, datetime.timedelta(seconds=10)]`. - `test_sandbox_operations_are_durable_and_factory_runs_for_each_activity` now checks that all seven sandbox Activities get 10 seconds. Without the change, they get none. - `test_heartbeat_timer_counts_from_the_start` blocks the event loop for 0.7 seconds under a 1-second heartbeat timeout, and checks that the first heartbeat comes before the timeout. With the old timer, the first heartbeat would come 1.2 seconds in, so the test fails with `assert ([])`. - `test_slow_model_factory_logs_a_warning` builds a model with a factory that takes 0.3 seconds, under a 0.1-second heartbeat timeout, and checks the warning. A fast factory logs nothing. Without the warning it fails with `assert ("Building model 'slow' took" in '') == True`. - Locally, `make test` (262 passed, 1 skipped), `make lint` and the conventions check pass, on Python 3.12 with uv 0.12.24. With #497 merged in, its two cancellation test files and these tests pass (53 passed). 3. Any docs updates needed? The docstrings and the README are updated here. Release notes should mention the new default, and how to turn it off.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cancelling a Workflow during a Strands activity or MCP tool call currently leaves the concurrent executor waiting forever: the tool converts cancellation into an error result, then waits for an acknowledgment from a parent that has stopped consuming events. With the sequential executor, the agent instead continues and can complete successfully after cancellation.
Record the tool task's cancellation request count before each activity call. Translate Temporal activity cancellation to
asyncio.CancelledErroronly when that count increases during the call. Independently reported activity cancellation remains a failed tool result with the matchingtoolUseId, including when the task caught an earlier cancellation. Both tool kinds now let a cancelled Workflow close asCANCELED, while preservingTRY_CANCEL,WAIT_CANCELLATION_COMPLETED, andABANDONbehavior.Add twelve integration cases covering both tool kinds and executors, cleanup ordering, no subsequent model call after Workflow cancellation, and explicit history replay. Eight isolated cases cover independently reported cancellation and previously caught task cancellation, verifying that the next model call retains its matching tool result.
Breaking change: there is no replay patch. Existing histories that processed workflow or tool-task cancellation as an error result can fail replay on this version. Keep affected in-flight runs on their original workers with Worker Versioning. To recover a run stuck during tool cancellation, reset it to before the affected tool call and run it on upgraded workers. The README includes this guidance.
Validation:
make lintmake test PYTEST_ARGS='tests/test_cancellation.py tests/test_independent_cancellation.py tests/test_tool_errors.py tests/test_interrupt_exception.py -n 2 -q --tb=short'— 27 passed