Title: Python 3.14 Compatibility: NameError in _outbound_schedule_activity due to eager closure-cell inspection

未关闭 适合新手
#1,517 10 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
python
领域
backend

调研方向

从 temporalio/worker/_workflow_instance.py 中的 _outbound_schedule_activity 开始,检查在构造 _ActivityHandle 期间 run_activity 如何捕获 handle。在 Python 3.14 开发环境中复现最小的 Hello World activity workflow,然后验证 activity 的调度能够完成,且不会出现报告中的 NameError。

由索引模型根据 Issue 内容生成。

描述

bug
What are you really trying to do?

I am upgrading a production service to use the latest Python environments (specifically testing against Python 3.14 alpha/dev builds). I encountered a regression where Temporal workflows fail immediately when scheduling activities due to changes in how Python 3.14 handles asyncio.Task initialization and closure cell inspection. I am reporting this so the SDK can be made compatible with the upcoming Python 3.14 release.


Describe the bug

In Python 3.14, any workflow calling await workflow.execute_activity(...) fails with a NameError.

This is caused by a "binding lag" in _workflow_instance.py. The variable handle is annotated but not assigned a value before it is referenced inside the run_activity closure. While previous Python versions (up to 3.13) deferred the inspection of these cells until the coroutine actually executed, Python 3.14’s asyncio.Task constructor (specifically when set_name is called) eagerly inspects the cells. Because the task is created in the same expression that binds the name, the inspection happens while the cell is still empty, triggering the error.

Traceback:

  File ".../temporalio/worker/_workflow_instance.py", line 1921, in _outbound_schedule_activity
    handle = _ActivityHandle(self, input, run_activity())
  File ".../temporalio/worker/_workflow_instance.py", line 3047, in _ActivityHandle.__init__
    instance._register_task(self, name=f"activity: {input.activity}")
  File ".../temporalio/worker/_workflow_instance.py", line 2458, in _register_task
    task.set_name(name)
NameError: cannot access free variable 'handle' where it is not associated with a value in enclosing scope


Minimal Reproduction
  1. Use a Python 3.14 environment (e.g., cgr.dev/chainguard/python:latest-dev).
  2. Run any standard "Hello World" workflow.
  3. Attempt to execute an activity.
  4. The worker will catch a NameError during the _outbound_schedule_activity call.

The problematic code pattern in _workflow_instance.py:

handle: _ActivityHandle  # Created but UNBOUND

async def run_activity() -> Any:
    while True:
        handle._started = True  # Closure reference
        ...

# Python 3.14 fails here because run_activity() 
# is inspected during Task creation inside the _ActivityHandle init
handle = _ActivityHandle(self, input, run_activity()) 


Environment/Versions
  • OS and processor: Linux (verified on x86_64/Aarch64 via Chainguard images)
  • Temporal Version: SDK versions 1.21.1 through 1.26.0 (current stable range)
  • Python Version: 3.14.0a (early 2026 dev builds)
  • Deployment: Docker-based workers

Additional context

A temporary runtime patch confirms that pre-binding the variable resolves the issue:

handle: Optional[_ActivityHandle] = None # Explicit binding fixes the NameError

This issue will likely affect all users as they begin migrating to Python 3.14 later this year, or those using "floating" rolling-release base images in their CI/CD pipelines.

主要语言
Python
星标
1.2k
派生
241
平均合并
3 天 2 小时
30 天内合并 PR
49

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

temporalio/sdk-python 的其他 Issue

查看 temporalio/sdk-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。