Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Branch Name
main
Commit ID
Other Environment Information
- Hardware parameters:
- OS type:
- Others: code inspection of origin/main. No open issue covers LocalETLFS temp cleanup. [Bug]: fulltext FixedBytePool.Spill leaves the temp file behind when Write fails #29251 is fulltext FixedBytePool.Spill, [Bug]: HNSW NewHnswSync orphans model files when DownloadAll fails, and loaded models ignore the resolved spill directory #29253 is HNSW model files.
FileServiceWriter.Abortdocuments that a failed localWriteremoves its temp file.
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 != sizeand every entry hasSize >= 0:return moerr.NewSizeNotMatchNoCtx(path.File)(166–176).io.CopyBufferalready returned nil, so the outererris nil.f.Close()fails:if err := f.Close(); err != nil { return err }(178–180). The:=shadows the outererr. The fd stays open as well, because the defer does not run.os.Renamefails:if err := os.Rename(f.Name(), nativePath); err != nil { return err }(190–192). Close already succeeded, so this is a closed.tmp.*left inrootPath.
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:
NewLocalETLFSon an empty directory.WriteanIOVectorwhose entrySizeis larger thanlen(Data)is not required; the reader itself returnsSizeNotMatchin that case and that path does clean up. To hit the unassigned return, makeio.CopyBufferreturn(n, nil)withn != sizeand everySize >= 0. The post-copy check at lines 166–176 then returns without settingerr.- The practical production trigger is
f.Closeoros.Renamereturning an error after the bytes have been copied (for exampleENOSPC/EIOon close while flushing a large ETL ortable_dumpobject).Writereturns 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
fix: clean failed fulltext spill and ETL temp files (#29358)
Ariznawlll commentedon Sep 27, 2026
测试版本
main5be6cd90b501da225be591ffffe6000bf01f515e原问题验证
该问题依赖内部 reader 生命周期或 I/O 故障注入,公开 SQL 无法稳定制造目标失败点。已在最新 main 上运行对应定向测试
TestLocalETLFSWriteFailureRemovesTemp,连续 3 次通过。Happy Path 覆盖
Unhappy Path 覆盖
.tmp。白盒验证
确认修复提交
9841d29e2f1ee953daec281d508048b0ce485f5c是当前 main 的祖先,并直接执行该提交新增的定向测试,结果 3/3 PASS。回归测试
回归已落在 MatrixOne UT:
TestLocalETLFSWriteFailureRemovesTemp。未加入 MOTR,因为该场景必须注入内部 reader/I/O 失败,SQL 黑盒无法可靠触发;按覆盖矩阵应保留在 UT 层。测试证据
5be6cd90b501da225be591ffffe6000bf01f515e9841d29e2f1ee953daec281d508048b0ce485f5c测试结论
测试通过。