[Bug] context.thread_local_storage.invocation_id does not correlate thread logs in Application Insights on Python 3.13
维护者通常 3 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- azure, python
调研方向
首先,在 Python 3.12 和 3.13 上使用 asyncio.to_thread 复现 async HTTP 触发器,然后检查 azure_functions_runtime.utils.executor 中的 invocation_id_cv workaround,以及 context.thread_local_storage.invocation_id 的使用方式。当后台线程日志在 Python 3.13 上通过受支持的 API 以相同的 InvocationId 出现在 Application Insights 中时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Expected Behavior
According to the docs, setting context.thread_local_storage.invocation_id
inside a background thread enables log correlation for that thread:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#log-custom-telemetry
After calling:
context.thread_local_storage.invocation_id = context.invocation_id
...all logging.info() calls from that thread should appear in Application
Insights correlated to the function invocation (same InvocationId).
Actual Behavior
On Python 3.13, [THREAD] logs are completely absent from Application Insights.
[MAIN] logs appear correctly. The identical code works on Python 3.12.
Python 3.12 ✅
"Logging from main handler" → appears, InvocationId=37b57bfd-...
"Logging from a background thread" → appears, InvocationId=37b57bfd-...
Python 3.13 ❌
"Logging from main handler" → appears, InvocationId=48b27079-...
"Logging from a background thread" → ABSENT from App Insights entirely
Steps to Reproduce
- Deploy Azure Function App with Python 3.13, Azure Functions Runtime v4, Dedicated plan
- Use the code below (async HTTP trigger + asyncio.to_thread)
- Trigger the function via HTTP
- Check Application Insights traces — background thread log is missing
- Redeploy the same code with Python 3.12 — background thread log appears correctly
Relevant code being tried
import azure.functions as func
import logging
import asyncio
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
def work(context: func.Context):
context.thread_local_storage.invocation_id = context.invocation_id
logging.info("Logging from a background thread")
@app.route(route="test")
async def test_async(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
await asyncio.to_thread(work, context)
logging.info("Logging from main handler")
return func.HttpResponse("ok")
Relevant log output
requirements.txt file
azure-functions
Where are you facing this problem?
Production Environment (explain below)
Function app name
No response
Additional Information
Root cause: azure-functions-runtime (used on Python 3.13) has its own separate
threading.local object for log routing. context.thread_local_storage points to
azure-functions-worker's threading.local — a different object that the new
runtime's logging proxy never reads.
Workaround (confirmed working on Python 3.13):
from azure_functions_runtime.utils.executor import invocation_id_cv
def work():
logging.info("Logging from a background thread")
@app.route(route="test")
async def test_async(req, context):
invocation_id_cv.set(context.invocation_id) # copied by asyncio.to_thread
await asyncio.to_thread(work)
asyncio.to_thread copies ContextVars automatically, so the proxy finds the
invocation_id correctly.
Note: invocation_id_cv is an internal API — a public API for this use case
would be appreciated.
- 主要语言
- Python
- 星标
- 357
- 派生
- 117
- 平均合并
- 4 天 11 小时
- 30 天内合并 PR
- 4
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
Azure/azure-functions-python-worker 的其他 Issue
-
[Bug] ASGI cookie conversion serializes an absent Domain attribute and breaks __Host- cookies可能已有人在做 @tharun06 于 24 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
Azure/azure-functions-python-worker#1902 ·
维护者通常 3 天内回复
-
难度 5/5 一周以上 新手友好度 28/100
Azure/azure-functions-python-worker#1908 · 2 条评论 ·
维护者通常 3 天内回复
-
难度 4/5 3-5 天 新手友好度 55/100
Azure/azure-functions-python-worker#1906 · 1 条评论 ·
维护者通常 3 天内回复
-
难度 5/5 一周以上 新手友好度 35/100
Azure/azure-functions-python-worker#1904 ·
维护者通常 3 天内回复
-
[Python 3.14 proxy worker] Missing sys.modules["__main__"] breaks multiprocessing spawn可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 4/5 3-5 天 新手友好度 55/100
Azure/azure-functions-python-worker#1903 ·
维护者通常 3 天内回复
查看 Azure/azure-functions-python-worker 的全部 Issue
相似的 Issue
-
python triage
难度 2/5 1-3 小时 新手友好度 72/100
microsoft/semantic-kernel#14615 ·
维护者通常 3 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
-
bug
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
shackett/shackett-utils#19 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
UKGovernmentBEIS/control-arena#917 ·
维护者通常 1 天内回复