Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Searched open and closed issues for NewHnswSync, DownloadAll, and HNSW temp-file leaks. #25630 is closed and is a different bug: HnswModel.SaveToFile used to leak one FD per save. That path still closes the CreateTemp FD immediately (model.go:236) and the defer at :241-244 unlinks the file unless Path was committed. This report is the DownloadAll error path, which never reaches SaveToFile.
Branch Name
main
Commit ID
Other Environment Information
- Code inspection only. Not re-executed on a cluster.
- ISCP HNSW sync runs on a
SqlProcesswhoseProcis nil.
Actual Behavior
NewHnswSync drops every model file that DownloadAll already committed when a later model fails.
DownloadAll (pkg/vectorindex/hnsw/sync.go:235-245) calls LoadIndex(..., view=false) and then Unload for each metadata model. view=false success keeps the temp file (pkg/vectorindex/hnsw/model.go:792-807 removes it only when err != nil || view). A clean load is not dirty, so Unload → SaveToFile returns immediately (model.go:186-188) and leaves Path set (model.go:946-977).
If model 0 finishes and model 1's LoadIndex or Unload returns, DownloadAll returns. NewHnswSync then does return nil, err (sync.go:219-222) without Destroy. runHnsw only destroys a non-nil sync (pkg/iscp/index_consumer.go:308-314), so that cleanup never runs. Model 1's own LoadIndex defer removes model 1's file. Model 0's file is not referenced anymore. HnswModel.Destroy (model.go:152-159) would have removed it.
Those files are not in the directory the caller resolved:
LoadMetadata(pkg/vectorindex/hnsw/search.go:280) builds models with an emptyTmpDir.DownloadAllnever copiessync.tmpDironto them.hnswSpillDirreturns""whenProc == nil(model.go:500-508), which is the ISCPSqlContext.os.CreateTemp("", "hnsw")therefore usesos.TempDir().
NewSync does resolve a LOCAL fileservice directory (pkg/iscp/index_sqlwriter.go:544-551) and stores it on sync.tmpDir (sync.go:212-213). Later NewHnswModelForBuild calls pass s.tmpDir (sync.go:442). The download path does not. HostSpillDir.sweepOnce only removes __vectorindex/<service>, and only once per process, so it never sees these /tmp/hnsw* files.
Each ISCP retry of an index with two or more metadata rows (a model that already rolled over) adds another file of FileSize bytes. The FD is closed at model.go:867. The leak is the directory entry.
On the success path the same missing TmpDir still parks every downloaded model in os.TempDir() for the whole consumer lifetime. runHnsw's defer Destroy removes them only when NewSync returned non-nil.
HnswSearch.LoadIndex does destroy every model when a later load fails (search.go:318-321). NewHnswSync does not.
Expected Behavior
On the DownloadAll error branch, call sync.Destroy() before return nil, err. Set each loaded model's TmpDir from sync.tmpDir before LoadIndex, so the file is created in the directory the ISCP writer already resolved and a crash leftover is somewhere sweepOnce already reclaims.
Steps to Reproduce
Not run on a cluster. From the current DownloadAll loop:
- Metadata table has at least two HNSW model rows.
- ISCP
runHnswcallsNewSync→NewHnswSync. - Model 0
LoadIndex(view=false)andUnloadsucceed. ItsCreateTempfile stays atPath. - Model 1 fails (stream SQL error, checksum mismatch, or
fallocate). NewHnswSyncreturnsnil, err. Model 0's file remains underos.TempDir().
A single model whose own LoadIndex fails is cleaned by that call's defer. A fully successful NewHnswSync is cleaned by runHnsw's defer Destroy.
Additional information
setupModel (sync.go:466) calls LoadIndex on the last model and ignores the error. That is a separate dropped-error path and is not what leaves the file behind; the orphan is the return nil, err in NewHnswSync.
Activity
fix(hnsw): stop NewHnswSync orphaning model files and spill to the re…
Ariznawlll commentedon Oct 2, 2026
测试版本
maind50a2d475a406829f67127fef7b2ad4abc1c1d3d原问题验证
在上述 commit 运行 issue 对应的定向回归。HNSW model 文件落在 resolved spill dir;DownloadAll 失败时已提交的临时文件被清理。
Happy Path 覆盖
覆盖成功 download/load 和 sweep。
Unhappy Path 覆盖
注入 download failure,断言 spill 文件数回到 0;无 orphan。
白盒验证
关联修复 #29334 已进入当前
main。直接执行并检查的定向测试为:pkg/vectorindex/hnsw/sync_spilldir_test.go::TestDownloadAllPlacesModelsInResolvedSpillDir、TestNewHnswSyncRemovesCommittedFilesOnDownloadAllError。回归测试
本问题依赖内部故障注入、并发时序、catalog/optimizer 状态或多组件协议边界,无法由普通 SQL golden 稳定构造,因此不放入 MOTR;回归保留在 MatrixOne UT/BVT 层。对应定向测试在精确 commit 上通过,且未修改用户工作区。
测试证据
d50a2d475a406829f67127fef7b2ad4abc1c1d3dpkg/vectorindex/hnsw/sync_spilldir_test.go::TestDownloadAllPlacesModelsInResolvedSpillDir、TestNewHnswSyncRemovesCommittedFilesOnDownloadAllError测试结论
测试通过。