Skip to content

[Bug]: HNSW NewHnswSync orphans model files when DownloadAll fails, and loaded models ignore the resolved spill directory #29253

Closed

Description

@Ariznawlll

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

66da387

Other Environment Information

  • Code inspection only. Not re-executed on a cluster.
  • ISCP HNSW sync runs on a SqlProcess whose Proc is 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 empty TmpDir.
  • DownloadAll never copies sync.tmpDir onto them.
  • hnswSpillDir returns "" when Proc == nil (model.go:500-508), which is the ISCP SqlContext.
  • os.CreateTemp("", "hnsw") therefore uses os.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:

  1. Metadata table has at least two HNSW model rows.
  2. ISCP runHnsw calls NewSync → NewHnswSync.
  3. Model 0 LoadIndex(view=false) and Unload succeed. Its CreateTemp file stays at Path.
  4. Model 1 fails (stream SQL error, checksum mismatch, or fallocate).
  5. NewHnswSync returns nil, err. Model 0's file remains under os.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

  1. added
    kind/bug
    on Sep 22, 2026
  2. added this to the MO-202609 milestone on Sep 23, 2026
  3. added theissue type on Sep 23, 2026
  4. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:干净源码 worktree;定向 Go UT/集成回归(涉及 Proxy/CN 的路径使用对应组件级并发与协议模拟)
    • 测试日期:2026-10-02

    原问题验证

    在上述 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 上通过,且未修改用户工作区。

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 定向测试:pkg/vectorindex/hnsw/sync_spilldir_test.go::TestDownloadAllPlacesModelsInResolvedSpillDir、TestNewHnswSyncRemovesCommittedFilesOnDownloadAllError
    • 当前结果:HNSW model 文件落在 resolved spill dir;DownloadAll 失败时已提交的临时文件被清理。
    • 关联 PR:#29334

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions