Skip to content

Commit 7368b34

Browse files
authored
core/rawdb: capture open file error and fix resource leak (#33147)
1 parent 5f4cc3f commit 7368b34

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

core/rawdb/eradb/eradb.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ func (db *Store) openEraFile(epoch uint64) (*era.Era, error) {
303303
}
304304
// Sanity-check start block.
305305
if e.Start()%uint64(era.MaxEra1Size) != 0 {
306+
e.Close()
306307
return nil, fmt.Errorf("pre-merge era1 file has invalid boundary. %d %% %d != 0", e.Start(), era.MaxEra1Size)
307308
}
308309
log.Debug("Opened era1 file", "epoch", epoch)

core/rawdb/freezer_batch.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ func newFreezerBatch(f *Freezer) *freezerBatch {
5151

5252
// Append adds an RLP-encoded item of the given kind.
5353
func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) error {
54-
return batch.tables[kind].Append(num, item)
54+
if table := batch.tables[kind]; table != nil {
55+
return table.Append(num, item)
56+
}
57+
return errUnknownTable
5558
}
5659

5760
// AppendRaw adds an item of the given kind.
5861
func (batch *freezerBatch) AppendRaw(kind string, num uint64, item []byte) error {
59-
return batch.tables[kind].AppendRaw(num, item)
62+
if table := batch.tables[kind]; table != nil {
63+
return table.AppendRaw(num, item)
64+
}
65+
return errUnknownTable
6066
}
6167

6268
// reset initializes the batch.

core/rawdb/freezer_table.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,8 +1196,7 @@ func (t *freezerTable) sizeNolock() (uint64, error) {
11961196
}
11971197

11981198
// advanceHead should be called when the current head file would outgrow the file limits,
1199-
// and a new file must be opened. The caller of this method must hold the write-lock
1200-
// before calling this method.
1199+
// and a new file must be opened. This method acquires the write-lock internally.
12011200
func (t *freezerTable) advanceHead() error {
12021201
t.lock.Lock()
12031202
defer t.lock.Unlock()
@@ -1218,7 +1217,9 @@ func (t *freezerTable) advanceHead() error {
12181217
return err
12191218
}
12201219
t.releaseFile(t.headId)
1221-
t.openFile(t.headId, openFreezerFileForReadOnly)
1220+
if _, err := t.openFile(t.headId, openFreezerFileForReadOnly); err != nil {
1221+
return err
1222+
}
12221223

12231224
// Swap out the current head.
12241225
t.head = newHead

0 commit comments

Comments
 (0)