【Azure Function App】升级 Python 运行时 3.9 到3.10 后遇见的问题

问题描述

Azure Function 原来运行在 Python 3.9。由于 Python 3.9 即将停止维护,需要升级到 Python 3.10。

第一次升级时,只是在 Function App 页面中进入 Settings → Configuration → General settings,将 Python 版本从 3.9 修改为 3.10。

Function App 表面上可以正常启动,但绑定的 Service Bus topic-subscription 消息不再被正常消费。

观察到的主要错误如下:

Loading function failed. ExceptionType: System.TimeoutException
Timeout value of 00:30:00 exceeded by function 'Functions.func1'
Executed 'Functions.func1' (Failed, Duration=1800022ms)
Message processing error (Action=ProcessMessageCallback, EntityPath=<topic-name>/Subscriptions/<subscription-name>)

后台日志中还可以看到 Python Worker 被强制终止:

The process with PID XXXX (language worker) was forcefully terminated.
Exit code: 137 (SIGKILL)

之后重新部署了新的 package,但消息仍然无法消费。这次错误变成了函数加载阶段立即失败:

[Error] Executed 'Functions.func1' (Failed, Duration=1ms)
Exception: AttributeError: attribute '__default__' of 'typing.ParamSpec' objects is not writable

堆栈中可以看到错误发生在导入 azure.storage.blob 的过程中,最终落到 typing_extensions.py

File ".../azure/storage/blob/_blob_client.py", line 22, in <module>
    from azure.core.tracing.decorator import distributed_trace
File ".../azure/core/tracing/decorator.py", line 37, in <module>
    P = ParamSpec("P")
File ".../typing_extensions.py", line 1474, in _set_default
    type_param.__default__ = None
AttributeError: attribute '__default__' of 'typing.ParamSpec' objects is not writable

 

问题解答

这次问题主要分成两个阶段。

1. 只修改 Function App 运行时版本,依赖包没有重新构建

第一个错误的重点是:

System.TimeoutException
Duration=1800022ms
exit code 137 / SIGKILL

这说明函数并不是业务代码执行慢,而是在 Worker 加载函数时卡住,最终被 Host 强制终止。

常见原因是应用使用 WEBSITE_RUN_FROM_PACKAGE 方式部署,zip 包中的 Python 依赖是在旧的 Python 3.9 环境下安装或编译的。

只在 Portal 中把运行时改成 Python 3.10 后,云端解释器变成了 3.10,但包里的原生依赖仍可能是 3.9 的构建产物,例如 grpcioprotobufcryptography 等,从而导致函数加载异常或超时。

 

建议处理方式:

  1. 在本地准备目标 Python 版本环境,例如 Python 3.10。

  2. 删除旧依赖包,不复用 Python 3.9 环境下生成的 package。

  3. 在 Python 3.10 环境下重新安装依赖:

    pip install --upgrade pip
    pip install -r requirements.txt
    
  4. 重新打包并部署。

 

2. typing_extensions 与实际 Python 运行时不兼容

第二个错误的重点是:

AttributeError: attribute '__default__' of 'typing.ParamSpec' objects is not writable

如果堆栈中出现类似下面的路径:

 /azure-functions-host/workers/python/3.13/...

说明当前实际运行环境可能涉及 Python 3.13。旧版 typing_extensions 与 Python 3.13 中 typing.ParamSpec.__default__ 的行为不兼容,可能在导入 azure.storage.blobazure.core.tracing.decorator 等依赖时直接失败。

建议处理方式:

  1. requirements.txt 中显式指定较新的 typing_extensions 版本:

    typing_extensions>=4.12.0
    
  2. 重新安装依赖并重新打包:

    pip install --upgrade pip
    pip install -r requirements.txt
    
  3. 部署后检查 Function App 实际 Python 运行时版本,确认它和预期一致。

  4. 如果目标是 Python 3.10,需要确认配置没有实际运行到 Python 3.13。

  5. 再次验证函数是否可以正常加载,以及 Service Bus topic-subscription 中的消息是否可以正常消费

 

总结

Azure Functions 升级 Python 运行时时,不建议只在 Portal 中修改 Python 版本。

更稳妥的顺序是:

准备目标 Python 版本环境
→ 重新安装依赖
→ 重新打包
→ 部署到 Staging Slot 验证
→ 确认实际运行时版本
→ Swap 到生产环境

这次排查中,两个关键点分别是:

  1. System.TimeoutExceptionDuration=1800022msexit code 137:优先检查是否复用了旧 Python 版本构建出来的依赖包。
  2. AttributeError: attribute '__default__' of 'typing.ParamSpec' objects is not writable:优先检查 typing_extensions 版本,以及实际运行的 Python 版本是否符合预期。

 

 

参考资料

更新 Azure Functions 中的语言堆叠版本 : https://docs.azure.cn/zh-cn/azure-functions/update-language-versions?tabs=azure-portal%2Clinux&pivots=programming-language-python

attribute '__default__' of 'typing.ParamSpec' objects is not writable on Python 3.13 : https://github.com/python/typing_extensions/issues/404

 

posted @ 2026-06-24 19:22  编码者卢布  阅读(13)  评论(0)    收藏  举报