Model loading can fork a multi-threaded process and do illegal things causing an intermittent segfault in just the right conditions

未关闭
#6,201 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 4 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend, databases

调研方向

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 close self._engine_adapter and any cached engine_adapters.
  • Calling gc.collect() in create_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 (forkserver or spawn). Python 3.14 made forkserver the POSIX default for this reason. That likely needs the pool initargs (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

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

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

SQLMesh/sqlmesh 的其他 Issue

查看 SQLMesh/sqlmesh 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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