Skip to content

[Bug]: LocalETLFS.write leaves .tmp files behind when Close or Rename fails #29264

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

66da387

Other Environment Information

Actual Behavior

LocalETLFS.write (pkg/fileservice/local_etl_fs.go 123–198) creates os.CreateTemp(l.rootPath, ".tmp.*") and defers close+remove only when the outer err is non-nil. The function result is an unnamed error, so a bare return or a shadowed err does not set that variable.

These returns leave the temp file in the ETL root while the caller still receives the error:

  • n != size and every entry has Size >= 0: return moerr.NewSizeNotMatchNoCtx(path.File) (166–176). io.CopyBuffer already returned nil, so the outer err is nil.
  • f.Close() fails: if err := f.Close(); err != nil { return err } (178–180). The := shadows the outer err. The fd stays open as well, because the defer does not run.
  • os.Rename fails: if err := os.Rename(f.Name(), nativePath); err != nil { return err } (190–192). Close already succeeded, so this is a closed .tmp.* left in rootPath.

io.CopyBuffer and ensureDir do assign the outer err, so those failures are cleaned up. syncDir runs only after Rename has already published the file; a failure there is not a temp leak.

LocalFS.write (local_fs.go 327–413) is the same sequence with a named (bytesWritten int, err error). Its return 0, err assigns the named result before the defer, so Close, Rename, and size-mismatch all remove the temp.

LocalETLFS.NewWriter (584–640) and LocalFS.NewWriter (1098–1163) have the same hole on a later Close. The constructor defer sees err == nil because NewWriter returns the writer successfully. closeFunc then returns f.Close, ensureDir, or os.Rename errors without os.Remove. On LocalFS.NewWriter, f.Sync failure returns before Close, so the fd and the temp both remain. ensureDir inside that closeFunc assigns the constructor's captured err, which the already-finished defer never observes.

ETL export and table dump reach LocalETLFS.write through FileService.Write (pkg/util/export/etl/csv.go FSWriter.Write, pkg/fileservice/file_service_writer.go asyncWriteFunc, pkg/frontend/table_dump.go). FileServiceWriter.Abort says a failed local write discards the temp. That is true when CopyBuffer fails. It is false when Close or Rename fails.

Expected Behavior

Any error returned by LocalETLFS.write before Rename succeeds should close and remove the .tmp.* file, matching LocalFS.write. NewWriter.Close on both local implementations should remove the temp when Sync, Close, ensureDir, or Rename fails, and should not leave the fd open.

Steps to Reproduce

Code path. One deterministic case is a size mismatch after a successful copy:

  1. NewLocalETLFS on an empty directory.
  2. Write an IOVector whose entry Size is larger than len(Data) is not required; the reader itself returns SizeNotMatch in that case and that path does clean up. To hit the unassigned return, make io.CopyBuffer return (n, nil) with n != size and every Size >= 0. The post-copy check at lines 166–176 then returns without setting err.
  3. The practical production trigger is f.Close or os.Rename returning an error after the bytes have been copied (for example ENOSPC/EIO on close while flushing a large ETL or table_dump object). Write returns that error. The .tmp.* file remains under the ETL root. A retry creates another one.

NewWriter then Close with a failing Rename (destination parent removed after CreateTemp, or a permission error) leaves the same .tmp.*. LocalFS.NewWriter does this too.

Additional information

This is an error-path leak, not a successful write of a short file. The caller observes the error. syncDir failure after a successful Rename is outside this bug: the final name already exists, and removing the old temp name would be a no-op.

Activity

  1. added
    kind/bug
    on Sep 22, 2026
  2. added theissue type on Sep 22, 2026
  3. added this to the MO-202609 milestone on Sep 22, 2026
  4. self-assigned this
    on Sep 23, 2026
  5. added a commit that references this issue on Sep 24, 2026
    9841d29
  6. Ariznawlll commented on Sep 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:5be6cd90b501da225be591ffffe6000bf01f515e
    • 部署方式:本地源码定向 UT(Go 1.26.5,macOS arm64)
    • 测试日期:2026-09-27

    原问题验证

    该问题依赖内部 reader 生命周期或 I/O 故障注入,公开 SQL 无法稳定制造目标失败点。已在最新 main 上运行对应定向测试 TestLocalETLFSWriteFailureRemovesTemp,连续 3 次通过。

    Happy Path 覆盖

    1. LocalETLFS 写失败后清理临时文件。

    Unhappy Path 覆盖

    1. size mismatch、reader error、rename error 三个注错分支均无残留 .tmp。

    白盒验证

    确认修复提交 9841d29e2f1ee953daec281d508048b0ce485f5c 是当前 main 的祖先,并直接执行该提交新增的定向测试,结果 3/3 PASS。

    回归测试

    回归已落在 MatrixOne UT:TestLocalETLFSWriteFailureRemovesTemp。未加入 MOTR,因为该场景必须注入内部 reader/I/O 失败,SQL 黑盒无法可靠触发;按覆盖矩阵应保留在 UT 层。

    测试证据

    • MatrixOne main commit:5be6cd90b501da225be591ffffe6000bf01f515e
    • 修复提交:9841d29e2f1ee953daec281d508048b0ce485f5c
    • 定向 Go 测试:3/3 PASS

    测试结论

    测试通过。

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