Model loading can fork a multi-threaded process and do illegal things causing an intermittent segfault in just the right conditions
维护者通常 4 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with create_process_pool_executor in sqlmesh/utils/process.py, then trace its uses in sqlmesh/core/loader.py and sqlmesh/core/model/cache.py. Read Context.close() in sqlmesh/core/context.py and check how it handles the engine adapter and cached adapters. Reproduce the reported worker crash with the SQLMesh templater tests and DuckDB setup; done means closing contexts releases their connections and model loading no longer triggers the fork-related crash.
由索引模型根据 Issue 内容生成。
描述
I found this while working on sqlfluff, finding a random failure in CI. This is incredibly difficult to reliably reproduce, and even when I can reproduce it it's a small percentage thing. Reproducing it basically requires sqlfluff's test suite including pinned versions, so I'm just going to tell you what you are doing that is undefined behavior so you can fix it.
I'm sure that the problem is real, and the workaround definitely fixes it. That much I can vouch for.
Workaround on the sqlfluff side: sqlfluff#9075 (the change).
The details below about why exactly it happens, I'm less sure about.
Model loading forks worker processes from a process that has running threads, and the workers then run Python. POSIX does not allow the child of a multi-threaded fork() to call anything but a short list of async-signal-safe functions, so this is undefined behavior. One way it shows up is an intermittent segfault in a worker. This is likely the crash that #6145 worked around.
Where: create_process_pool_executor always uses the fork start method:
https://github.com/SQLMesh/sqlmesh/blob/7975a8d5841fe8f31217e27752e5cbb892f16ffc/sqlmesh/utils/process.py#L69
It is used for model loading and the optimized query cache:
https://github.com/SQLMesh/sqlmesh/blob/7975a8d5841fe8f31217e27752e5cbb892f16ffc/sqlmesh/core/loader.py#L778
https://github.com/SQLMesh/sqlmesh/blob/7975a8d5841fe8f31217e27752e5cbb892f16ffc/sqlmesh/core/model/cache.py#L190
How it crashes: a Context that is no longer used waits in a reference cycle for the garbage collector, still holding an open DuckDB connection. When a new context loads, the pool forks. If the collector then runs in a worker, the worker frees the old connection. DuckDB's destructor joins its worker threads, which do not exist in the child, and the worker segfaults.
Context.close() does not prevent this. It closes the snapshot evaluator and the state sync, but not the context's own engine adapter, which is what loading opens. A context that has only rendered never builds a snapshot evaluator, so its DuckDB connection stays open after close():
https://github.com/SQLMesh/sqlmesh/blob/7975a8d5841fe8f31217e27752e5cbb892f16ffc/sqlmesh/core/context.py#L2751
Fixes:
Context.close()should also closeself._engine_adapterand any cachedengine_adapters.- Calling
gc.collect()increate_process_pool_executor, before the pool forks, frees dropped objects in the process that owns their threads. This is what sqlfluff now does before creating a context, and it stopped the crash there (0 of 60 runs, against about 10% before). It narrows the problem; the fork is still undefined behavior. - The real fix is a start method that does not fork the multi-threaded process (
forkserverorspawn). Python 3.14 madeforkserverthe POSIX default for this reason. That likely needs the poolinitargs(console, cache) to be picklable.
Evidence
Native backtrace from a crashed worker (same in 12 cores):
#5 __pthread_clockjoin_ex
#6 std::thread::join()
#7 duckdb::TaskScheduler::RelaunchThreadsInternal(int, bool)
#8 duckdb::TaskScheduler::~TaskScheduler()
#9 duckdb::DatabaseInstance::~DatabaseInstance()
#11 duckdb::ClientContext::~ClientContext()
#13 duckdb::DuckDBPyConnection::~DuckDBPyConnection()
#18 subtype_dealloc
...
#24 gc_collect_main
The parent then fails with A process in the process pool was terminated abruptly and SchemaError: Failed to update model schemas from process_models. Python 3.12 warns at each fork: DeprecationWarning: This process ... is multi-threaded, use of fork() may lead to deadlocks in the child.
Seen in the sqlfluff SQLMesh templater tests, which create one context per test against a DuckDB :memory: gateway. Whether it fires depends on when the garbage collector runs, so it comes and goes with unrelated changes to allocation patterns.
Versions: sqlmesh 0.235.4 (code unchanged on main at 7975a8d), duckdb 1.5.6, sqlglot 30.8.0, CPython 3.12.16, Linux x86_64.
- 主要语言
- Python
- 星标
- 3.3k
- 派生
- 471
- 平均合并
- 6 天 7 小时
- 30 天内合并 PR
- 35
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
SQLMesh/sqlmesh 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 86/100
维护者通常 4 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 4 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100
SQLMesh/sqlmesh#6008 · 1 条评论 ·
维护者通常 4 天内回复
-
VSCode Extension: SQLMesh LSP crashes on startup: ImportError: cannot import name 'WorkspaceDiagnosticRefreshRequest' with lsprotocol 2025.x可能已有人在做 @boomesh11 于 15 天前认领。 未关闭Bug
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 4 天内回复
-
Improvement
难度 1/5 1 小时以内 新手友好度 85/100
SQLMesh/sqlmesh#5696 · 6 条评论 ·
维护者通常 4 天内回复
相似的 Issue
-
[BUG][Low–Medium] founder_numbers.py silently reads a SAFE discount or pool target given as a fraction (0.2) as 0.2%可能已有人在做 @sumitkhond111-del 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 72/100
bmad-code-org/BMAD-METHOD#3358 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 86/100
open-mmlab/mmcv#3352 ·
-
难度 1/5 1 小时以内 新手友好度 68/100
OpenGithubs/weekly#263 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
comp/cli P3 type/feature
难度 2/5 1-3 小时 新手友好度 76/100
NousResearch/hermes-agent#162784 ·
维护者通常 1 天内回复