Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ffi/firewood.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,6 @@ func (db *Database) Close(ctx context.Context) error {
return nil
}

go runtime.GC()

done := make(chan struct{})
go func() {
db.outstandingHandles.Wait()
Expand Down
11 changes: 10 additions & 1 deletion ffi/firewood_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,16 @@ func newTestDatabase(tb testing.TB, opts ...Option) *Database {
db, err := newDatabase(tb.TempDir(), opts...)
r.NoError(err)
tb.Cleanup(func() {
r.NoError(db.Close(oneSecCtx(tb)))
err := db.Close(oneSecCtx(tb))
if errors.Is(err, ErrActiveKeepAliveHandles) {
// force a GC to clean up dangling handles that are preventing the
// database from closing, then try again. Intentionally not looping
// since a subsequent attempt is unlikely to succeed if the first
// one didn't.
runtime.GC()
err = db.Close(oneSecCtx(tb))
}
r.NoError(err)
})
return db
}
Expand Down
28 changes: 26 additions & 2 deletions ffi/tests/eth/eth_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ package eth
import (
"context"
"encoding/binary"
"errors"
"math/rand"
"runtime"
"slices"
"testing"
"time"

"github.com/ava-labs/firewood-go-ethhash/ffi"
firewood "github.com/ava-labs/firewood-go-ethhash/ffi"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/rawdb"
Expand Down Expand Up @@ -62,13 +66,33 @@ type merkleTriePair struct {
pendingFwdBatch []firewood.BatchOp
}

// oneSecCtx returns `tb.Context()` with a 1-second timeout added. Any existing
// cancellation on `tb.Context()` is removed, which allows this function to be
// used inside a `tb.Cleanup()`
func oneSecCtx(tb testing.TB) context.Context {
ctx := context.WithoutCancel(tb.Context())
ctx, cancel := context.WithTimeout(ctx, time.Second)
tb.Cleanup(cancel)
return ctx
}

func newFirewoodDB(t *testing.T) *firewood.Database {
t.Helper()
r := require.New(t)

db, err := firewood.New(t.TempDir(), firewood.EthereumNodeHashing)
require.NoError(t, err, "firewood.New()")
r.NoError(err, "firewood.New()")
t.Cleanup(func() {
require.NoErrorf(t, db.Close(context.Background()), "%T.Close()", db)
err := db.Close(oneSecCtx(t))
if errors.Is(err, ffi.ErrActiveKeepAliveHandles) {
// force a GC to clean up dangling handles that are preventing the
// database from closing, then try again. Intentionally not looping
// since a subsequent attempt is unlikely to succeed if the first
// one didn't.
runtime.GC()
err = db.Close(oneSecCtx(t))
}
r.NoError(err, "%T.Close()", db)
})
return db
}
Expand Down
28 changes: 26 additions & 2 deletions ffi/tests/firewood/merkle_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ package firewood

import (
"context"
"errors"
"fmt"
"math/rand"
"path/filepath"
"runtime"
"slices"
"testing"
"time"

"github.com/ava-labs/firewood-go/ffi"
firewood "github.com/ava-labs/firewood-go/ffi"

"github.com/ava-labs/avalanchego/database"
Expand Down Expand Up @@ -50,14 +54,34 @@ var stepMap = map[byte]string{
commitProposal: "commitProposal",
}

// oneSecCtx returns `tb.Context()` with a 1-second timeout added. Any existing
// cancellation on `tb.Context()` is removed, which allows this function to be
// used inside a `tb.Cleanup()`
func oneSecCtx(tb testing.TB) context.Context {
ctx := context.WithoutCancel(tb.Context())
ctx, cancel := context.WithTimeout(ctx, time.Second)
tb.Cleanup(cancel)
return ctx
}

func newTestFirewoodDatabase(t *testing.T) *firewood.Database {
t.Helper()
r := require.New(t)

dbFile := filepath.Join(t.TempDir(), "test.db")
db, err := newFirewoodDatabase(dbFile)
require.NoError(t, err)
r.NoError(err, "firewood.New()")
t.Cleanup(func() {
require.NoError(t, db.Close(context.Background())) //nolint:usetesting // t.Context() will already be cancelled
err := db.Close(oneSecCtx(t))
if errors.Is(err, ffi.ErrActiveKeepAliveHandles) {
// force a GC to clean up dangling handles that are preventing the
// database from closing, then try again. Intentionally not looping
// since a subsequent attempt is unlikely to succeed if the first
// one didn't.
runtime.GC()
err = db.Close(oneSecCtx(t))
}
r.NoError(err, "%T.Close()", db)
})
return db
}
Expand Down