Skip to content

[Bug]: HnswModel.SaveToFile leaks one file descriptor per save #25630

Closed

Description

@Ariznawlll

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 nil

There 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 f immediately 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

  1. added this to the MO-202607 milestone on Jul 11, 2026
  2. added theissue type on Jul 11, 2026
  3. added
    kind/bug
    severity/s0
    and removed on Jul 12, 2026
  4. Ariznawlll commented on Jul 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 详细验证

    • 源码基线:origin/main@f1c0a92d882dbdad469ffd2c96f607796207c797。
    • 执行命令:
    go test ./pkg/vectorindex/hnsw \
      -run '^Test(SaveToFileNoFDLeak|SaveToFileCleanupOnError|SyncInsertDuplicateKeyFailsCleanly)$' \
      -count=3 -v

    本 issue 对应测试过程

    1. 禁用 GC,避免泄漏的 *os.File 被 finalizer 掩盖。
    2. 记录 /dev/fd 数量,多次创建、保存和销毁 HNSW index。
    3. 比较前后 FD 数量,断言 SaveToFile 没有持续增加打开描述符。
    4. 另注入 checksum 失败,断言错误返回、Path 不被设置、临时文件数不增加。

    结果

    • TestSaveToFileNoFDLeak、TestSaveToFileCleanupOnError:均 PASS 3/3。
    • package:ok github.com/matrixorigin/matrixone/pkg/vectorindex/hnsw 0.534s。
    • 结论:FD 与失败清理的定向回归在最新 main 通过。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions