Skip to content

Commit

Permalink
Problem: memiavl has resource leaks (#1075)
Browse files Browse the repository at this point in the history
* Problem: memiavl has resource leaks

Solution:
- add defers and close functions

* Update CHANGELOG.md

Signed-off-by: yihuang <[email protected]>

* code changes

* Update CHANGELOG.md

Co-authored-by: mmsqe <[email protected]>
Signed-off-by: yihuang <[email protected]>

* add more reruns to flaky test

---------

Signed-off-by: yihuang <[email protected]>
Co-authored-by: mmsqe <[email protected]>
  • Loading branch information
yihuang and mmsqe authored Jun 21, 2023
1 parent 368524d commit ed5ce30
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [#1042](https://github.com/crypto-org-chain/cronos/pull/1042) Avoid channel get changed when concurrent subscribe happens ([ethermint commit](https://github.com/crypto-org-chain/ethermint/commit/72bbe0a80dfd3c586868e2f0b4fbed72593c45bf)).
- [#1058](https://github.com/crypto-org-chain/cronos/pull/1058) Fix decode log for multi topics in websocket subscribe ([ethermint commit](https://github.com/crypto-org-chain/ethermint/commit/2136ad029860c819942ad1836dd3f42585002233)).
- [#1062](https://github.com/crypto-org-chain/cronos/pull/1062) Update cometbft `v0.34.29` with several minor bug fixes and low-severity security-fixes.
- [#1075](https://github.com/crypto-org-chain/cronos/pull/1075) Add missing close in memiavl to avoid resource leaks.

### Features

Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def cronos_mempool(tmp_path_factory):
)


@pytest.mark.flaky
@pytest.mark.flaky(max_runs=5)
def test_mempool(cronos_mempool):
w3: Web3 = cronos_mempool.w3
filter = w3.eth.filter("pending")
Expand Down
12 changes: 11 additions & 1 deletion memiavl/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// especially since callers may export several IAVL stores in parallel (e.g. the Cosmos SDK).
const exportBufferSize = 32

func (db *DB) Snapshot(height uint64, protoWriter protoio.Writer) error {
func (db *DB) Snapshot(height uint64, protoWriter protoio.Writer) (returnErr error) {
if height > math.MaxUint32 {
return fmt.Errorf("height overflows uint32: %d", height)
}
Expand All @@ -31,6 +31,11 @@ func (db *DB) Snapshot(height uint64, protoWriter protoio.Writer) error {
if err != nil {
return errors.Wrapf(err, "invalid height: %d", height)
}
defer func() {
if err := db.Close(); returnErr == nil {
returnErr = err
}
}()

mtree = &db.MultiTree
} else {
Expand All @@ -45,6 +50,11 @@ func (db *DB) Snapshot(height uint64, protoWriter protoio.Writer) error {
if err != nil {
return errors.Wrapf(err, "snapshot don't exists: height: %d", version)
}
defer func() {
if err := mtree.Close(); returnErr == nil {
returnErr = err
}
}()
}

for _, tree := range mtree.trees {
Expand Down
7 changes: 6 additions & 1 deletion memiavl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (i *importer) Add(n *iavl.ExportNode) error {
return nil
}

func updateMetadataFile(dir string, height int64) error {
func updateMetadataFile(dir string, height int64) (returnErr error) {
entries, err := os.ReadDir(dir)
if err != nil {
return err
Expand All @@ -238,6 +238,11 @@ func updateMetadataFile(dir string, height int64) error {
if err != nil {
return err
}
defer func() {
if err := snapshot.Close(); returnErr == nil {
returnErr = err
}
}()
storeInfos = append(storeInfos, storetypes.StoreInfo{
Name: name,
CommitId: storetypes.CommitID{
Expand Down
1 change: 1 addition & 0 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery {
if err != nil {
return sdkerrors.QueryResult(err, false)
}
defer db.Close()
}

path := req.Path
Expand Down

0 comments on commit ed5ce30

Please sign in to comment.