core/rawdb: fix file descriptor leak in freezer error paths (#34735)

In openFreezerFileForAppend, if Seek fails after the file is
successfully opened, the file handle is not closed, leaking a
descriptor.

Similarly in newTable, if opening the meta file fails, the
already-opened index file is not closed. And if newMetadata fails, both
the index and meta files are leaked.

Under repeated error conditions (e.g., corrupted filesystem), these
leaks accumulate and may exhaust the OS file descriptor limit, causing
cascading failures.
This commit is contained in:
rayoo 2026-04-20 11:06:17 +08:00 committed by GitHub
parent 8c7d61fcfe
commit 5af5510b1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 0 deletions

View file

@ -157,6 +157,7 @@ func newTable(path string, name string, readMeter, writeMeter *metrics.Meter, si
} }
meta, err = openFreezerFileForReadOnly(filepath.Join(path, fmt.Sprintf("%s.meta", name))) meta, err = openFreezerFileForReadOnly(filepath.Join(path, fmt.Sprintf("%s.meta", name)))
if err != nil { if err != nil {
index.Close()
return nil, err return nil, err
} }
} else { } else {
@ -166,6 +167,7 @@ func newTable(path string, name string, readMeter, writeMeter *metrics.Meter, si
} }
meta, err = openFreezerFileForAppend(filepath.Join(path, fmt.Sprintf("%s.meta", name))) meta, err = openFreezerFileForAppend(filepath.Join(path, fmt.Sprintf("%s.meta", name)))
if err != nil { if err != nil {
index.Close()
return nil, err return nil, err
} }
} }
@ -173,6 +175,8 @@ func newTable(path string, name string, readMeter, writeMeter *metrics.Meter, si
// is detected. // is detected.
metadata, err := newMetadata(meta) metadata, err := newMetadata(meta)
if err != nil { if err != nil {
meta.Close()
index.Close()
return nil, err return nil, err
} }
// Create the table and repair any past inconsistency // Create the table and repair any past inconsistency

View file

@ -134,6 +134,7 @@ func openFreezerFileForAppend(filename string) (*os.File, error) {
} }
// Seek to end for append // Seek to end for append
if _, err = file.Seek(0, io.SeekEnd); err != nil { if _, err = file.Seek(0, io.SeekEnd); err != nil {
file.Close()
return nil, err return nil, err
} }
return file, nil return file, nil