No safe way to set the CPU thread count from Python: _unsafe_reset_threadpool after load_method crashes or hangs XNNPACK
维护者通常 1 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
调研方向
Start with the Python binding for _unsafe_reset_threadpool and the thread pool lifetime rules in threadpool.h; trace how loaded methods retain the pool pointer. The issue includes a reproducer using XNNPACK and Runtime.load_method, so use it to check the reset-before-load and reset-after-load cases. Done means Python has a supported way to choose the thread count or rejects a reset after loading before it can crash or hang; an open linked pull request indicates work is already underway.
由索引模型根据 Issue 内容生成。
描述
🐛 Describe the bug
From Python, _unsafe_reset_threadpool(n) is the only way to set the thread count that XNNPACK uses. Called after a method is loaded, it makes the following execute calls segfault or hang forever (all threads waiting on a futex). Called before load_method, it works.
import torch
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
from executorch.exir import to_edge_transform_and_lower
from executorch.extension.pybindings.portable_lib import (
_threadpool_get_thread_count,
_unsafe_reset_threadpool,
)
from executorch.runtime import Runtime
model = torch.nn.Sequential(torch.nn.Linear(256, 256), torch.nn.ReLU(), torch.nn.Linear(256, 256)).eval()
x = torch.randn(64, 256)
ep = torch.export.export(model, (x,))
pte = to_edge_transform_and_lower(ep, partitioner=[XnnpackPartitioner()]).to_executorch()
method = Runtime.get().load_program(pte.buffer).load_method("forward")
_unsafe_reset_threadpool(2 * _threadpool_get_thread_count())
for _ in range(20):
method.execute([x]) # segfaults or hangs
On Linux x86_64 with 8 threads by default, a reset to 16 after load_method crashed or hung in 10 of 11 runs, and a reset to 4 in 3 of 5. The same resets before load_method passed 10 of 10. Linux aarch64 shows the same pattern, including the hang and an abort with The futex facility returned an unexpected error code. A reset to the current count does nothing, so it always looks fine.
Cause: the XNNPACK runtime gets the thread pool pointer when the method is loaded (xnn_create_runtime_v4(..., get_pthreadpool(), ...)), and the reset frees that pool and creates a new one.
threadpool.h warns that other code may still hold the old pointer and says to reset before loading. The Python function has no such note and does not stop the call. This is different from #14321 (a macOS crash from two copies of pthreadpool): here there is one copy, and the trigger is the order of the calls.
Ask:
- A supported way to set the thread count from Python, for example a runtime or backend option.
- Until then, raise an error instead of crashing when the reset comes after a method is loaded.
Versions
- executorch 1.6.0 (PyPI) and the 2026-11-05 nightly
- torch 2.15.0, and 2.16 nightly with the ExecuTorch nightly
- Linux x86_64 and Linux aarch64, CPU
- 主要语言
- Python
- 星标
- 5.2k
- 派生
- 1.2k
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 700
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytorch/executorch 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 64/100
pytorch/executorch#24767 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 74/100
pytorch/executorch#24434 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
pytorch/executorch#24410 ·
维护者通常 1 天内回复
-
enhancement triaged
难度 2/5 半天 新手友好度 68/100
pytorch/executorch#21640 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 45/100
pytorch/executorch#24766 ·
维护者通常 1 天内回复
查看 pytorch/executorch 的全部 Issue
相似的 Issue
-
ChatBedrockConverse changes the keys of JSON tool results to camelCase (`order_id` becomes `orderId`)可能已有人在做 @geyazheng 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
langchain-ai/langchain-aws#1469 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
维护者通常 1 天内回复
-
b4b usability
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
fix(schedule): align list_tasks since cursor filter boundary from strict > to >=可能已有人在做 @bfs2021 今天认领。 未关闭agent-ready bug p2 schedule
难度 2/5 1-3 小时 新手友好度 83/100
allenporter/supernote#540 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
frazane/scoringrules#164 ·
维护者通常 1 天内回复