Repository navigation
Description
Summary
Each successful HnswModel.SaveToFile call leaks the file descriptor returned by os.CreateTemp. The model records only f.Name() and never closes f; later Destroy removes the pathname but cannot close the lost descriptor.
Repeated HNSW CDC saves, builds, unloads, or reindex operations can therefore accumulate descriptors until the process hits its open-file limit.
Version
Commit: 7f7de555c
Code path
pkg/vectorindex/hnsw/model.go:
f, err := os.CreateTemp("", "hnsw")
if err != nil {
return err
}
err = idx.Index.Save(f.Name())
// checksum and model destruction
idx.Path = f.Name()
return nilThere is no f.Close() or deferred close on the success path. Several error paths after creation also return without closing the descriptor.
White-box reproduction
Disable GC/finalizers, count the process's open descriptors with fcntl(F_GETFD), and run 20 model save/destroy cycles:
oldGC := debug.SetGCPercent(-1)
defer debug.SetGCPercent(oldGC)
before := openFDCount()
for i := 0; i < 20; i++ {
model, _ := NewHnswModelForBuild[float32]("fd-test", cfg, 1, 64)
require.NoError(t, model.Add(int64(i), []float32{1, 2, 3}))
require.NoError(t, model.SaveToFile())
require.NoError(t, model.Destroy())
}
after := openFDCount()Actual result
open descriptors before=4 after=24 delta=20
The descriptor increase exactly matches the number of successful saves. Destroy() does not reduce it.
Expected result
Every descriptor created by SaveToFile must be closed on all success and failure paths. Saving by pathname through usearch does not require MatrixOne to keep the original *os.File open.
Suggested fix and verification
- Close
fimmediately after creation if only its pathname is needed, or use a deferred close with explicit error handling. - Remove the temporary pathname on checksum/save failures.
- Add an FD-count regression test with GC disabled and tests for save/checksum failure cleanup.
Activity
test(vectorindex): cover SaveToFile error/cleanup path to pass covera…
fix(vectorindex): close temp file fd in HnswModel.SaveToFile (#25630) (…
Ariznawlll commentedon Jul 15, 2026
最新 main 详细验证
origin/main@f1c0a92d882dbdad469ffd2c96f607796207c797。本 issue 对应测试过程
*os.File被 finalizer 掩盖。/dev/fd数量,多次创建、保存和销毁 HNSW index。SaveToFile没有持续增加打开描述符。Path不被设置、临时文件数不增加。结果
TestSaveToFileNoFDLeak、TestSaveToFileCleanupOnError:均 PASS 3/3。ok github.com/matrixorigin/matrixone/pkg/vectorindex/hnsw 0.534s。fix(vectorindex): close temp file fd in HnswModel.SaveToFile (matrixo…
fix(vectorindex): close temp file fd in HnswModel.SaveToFile (matrixo…
fix(vectorindex): close temp file fd in HnswModel.SaveToFile (matrixo…