[Bug] No clean way to cancel an activity and wait until it's cancelled
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 28/100
- Issue 类型
- 缺陷
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- python
调研方向
从链接的最小复现开始,尤其关注 run_workflow.py 及其 activity 取消路径。运行无参数、-w、-s 和 -w -s 变体,以比较取消时机、heartbeat 行为和 WAIT_CANCELLATION_COMPLETED。从这些入口跟踪 SDK 对 activity 取消的处理;完成标准是取消能够中断长时间运行的 coroutine,并且 workflow 无需宽泛的异常处理即可等待干净的取消。
由索引模型根据 Issue 内容生成。
描述
What are you really trying to do?
Start an activity from a workflow, cancel it, wait for clean cancellation acknowledgement, exit the workflow.
Describe the bug
There appear to be several issues.
- If you call
handle.cancel()on an activity handle from the workflow, theCancelledErrorwill only be raised in the activity after anactivity.heartbeat()call, followed by anawait something()call. Otherwise the activity itself doesn't seem to receiveCancelledError. This kinda sucks -- both that you need to callheartbeat()for cancellation requests to get through, and the fact that long-running coros do not get interrupted correctly. I don't understand the underlying implementation, but if the reason we need toheartbeat()is that workers don't want to poll for cancellation, still, when you callactivity.heartbeat(), the client library could check for cancellation and immediately calltask.cancel()on the task running the activity, no? Currently the work-around seems to be to litter activity code withasyncio.sleep(0.1). - From the workflow code, if you want to cleanly wait for the activity cancellation, you have to catch
ActivityError, which is a broader exception type than I actually want to catch (presumably there are otherActivityErrors which I don't want to catch). - If you use
WAIT_CANCELLATION_COMPLETED, if you try to cancel an activity that's currently awaiting a long-running coro (likeasyncio.sleep(10)), the activity won't receive aCancelledErroruntil it sends a heartbeat, which it can't do until the coro it's waiting on finishes, so your workflow won't finish until the activity's long-running coro finishes.
Minimal Reproduction
https://github.com/andmis/snippets/tree/temporal-python-sdk-cannot-cleanly-cancel-activities
Using python run_workflow.py with no args, the workflow exits promptly, but the activity never receives CancelledError, completes, and we get warning log spam:
2024-12-09 19:44:06.116611+00:00 (Workflow) Starting, use_sleep=False use_wait=False
2024-12-09 11:44:06.124587 (Activity) Running sandbox_activity
2024-12-09 19:44:07.130422+00:00 (Workflow) Cancelling activity
2024-12-09 19:44:07.130422+00:00 (Workflow) Activity cancelled
2024-12-09 19:44:07.130422+00:00 (Workflow) Exiting
2024-12-09 11:44:12.868185 (Activity) Completing sandbox_activity
2024-12-09T19:44:12.881460Z WARN temporal_sdk_core::worker::activities: Activity not found on completion. This may happen if the activity has already been cancelled but completed anyway. task_token=TaskToken(CiRhOTc2ZWZkMy1iM2NiLTQwZmMtOWYzZi1jNjk0MzU2NjMzN2ESEHNhbmRib3gtd29ya2Zsb3caJDVhMjAyNWI3LWRhY2MtNDg4OC04NjA5LWI3NGQ3MzI2MzA3YyAFKAEyATFCEHNhbmRib3hfYWN0aXZpdHlKCAgBEIeKQBgB) details=Status { code: NotFound, message: "workflow execution already completed", details: b"\x08\x05\x12$workflow execution already completed\x1aB\n@type.googleapis.com/temporal.api.errordetails.v1.NotFoundFailure", metadata: MetadataMap { headers: {"content-type": "application/grpc"} }, source: None }
Using python run_workflow.py -w, the workflow waits until the activity's long-running sleep finishes, despite the activity being cancelled (note timestamps), and the activity completes rather than being cancelled:
2024-12-09 19:45:15.119427+00:00 (Workflow) Starting, use_sleep=False use_wait=True
2024-12-09 11:45:15.127826 (Activity) Running sandbox_activity
2024-12-09 19:45:16.130733+00:00 (Workflow) Cancelling activity
2024-12-09 11:45:24.371849 (Activity) Completing sandbox_activity
2024-12-09 19:45:24.384356+00:00 (Workflow) Exiting
Using python run_workflow.py -w -s, the workflow waits for the long-running coro in the activity to finish, which is bad, and the activity does cancel rather than completing, but -s sucks:
2024-12-09 19:47:00.283375+00:00 (Workflow) Starting, use_sleep=True use_wait=True
2024-12-09 11:47:00.291593 (Activity) Running sandbox_activity
2024-12-09 19:47:01.301447+00:00 (Workflow) Cancelling activity
2024-12-09 11:47:06.547612 (Activity) Cancelling sandbox_activity
2024-12-09 19:47:06.551643+00:00 (Workflow) Activity cancelled
2024-12-09 19:47:06.551643+00:00 (Workflow) Exiting
Using python run_workflow.py -s results in the workflow exiting cleanly and promptly (since we aren't using WAIT_CANCELLATION_COMPLETED), and the activity cancels rather than completing, but still waits on the long-running coro:
2024-12-09 19:48:40.102363+00:00 (Workflow) Starting, use_sleep=True use_wait=False
2024-12-09 11:48:40.110083 (Activity) Running sandbox_activity
2024-12-09 19:48:41.121725+00:00 (Workflow) Cancelling activity
2024-12-09 19:48:41.121725+00:00 (Workflow) Activity cancelled
2024-12-09 19:48:41.121725+00:00 (Workflow) Exiting
2024-12-09 11:48:47.629316 (Activity) Cancelling sandbox_activity
Environment/Versions
OS and processor: macOS, M1
Temporal version: 1.1.2
Python SDK version: 1.8.0
Are you using Docker or Kubernetes or building Temporal from source? No
- 主要语言
- Python
- 星标
- 1.2k
- 派生
- 241
- 平均合并
- 3 天 2 小时
- 30 天内合并 PR
- 49
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
temporalio/sdk-python 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 74/100
temporalio/sdk-python#1517 · 10 条评论 ·
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
temporalio/sdk-python#496 ·
-
难度 5/5 一周以上 新手友好度 25/100
temporalio/sdk-python#1890 ·
-
难度 4/5 3-5 天 新手友好度 52/100
temporalio/sdk-python#1881 · 1 条评论 ·
-
bug
temporalio/sdk-python#1817 · 1 条评论 · 已指派 1 人 ·
查看 temporalio/sdk-python 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 88/100
use-agent-os/agent-os#3314 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
BasedHardware/omi#15662 · 1 条评论 ·
-
documentation help wanted
难度 2/5 1-3 小时 新手友好度 90/100
-
难度 2/5 1-3 小时 新手友好度 62/100
AiursoftWeb/AnduinOS-2#19 ·