cleanup data and dataaz when unmapping db on Windows platform

Signed-off-by: Benjamin Wang <wachao@vmware.com>
pull/362/head
Benjamin Wang 2023-01-09 14:18:02 +08:00
parent ad85400db2
commit 63d0cb428d
2 changed files with 30 additions and 2 deletions

View File

@ -107,8 +107,11 @@ func munmap(db *DB) error {
}
addr := (uintptr)(unsafe.Pointer(&db.data[0]))
var err1 error
if err := syscall.UnmapViewOfFile(addr); err != nil {
return os.NewSyscallError("UnmapViewOfFile", err)
err1 = os.NewSyscallError("UnmapViewOfFile", err)
}
return nil
db.data = nil
db.datasz = 0
return err1
}

View File

@ -10,11 +10,13 @@ import (
"math/rand"
"os"
"path/filepath"
"reflect"
"sync"
"testing"
"time"
"unsafe"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
bolt "go.etcd.io/bbolt"
@ -1311,6 +1313,29 @@ func TestDB_BatchTime(t *testing.T) {
}
}
// TestDBUnmap verifes that `dataref`, `data` and `datasz` must be reset
// to zero values respectively after unmapping the db.
func TestDBUnmap(t *testing.T) {
db := btesting.MustCreateDB(t)
require.NoError(t, db.DB.Close())
// Ignore the following error:
// Error: copylocks: call of reflect.ValueOf copies lock value: go.etcd.io/bbolt.DB contains sync.Once contains sync.Mutex (govet)
//nolint:govet
v := reflect.ValueOf(*db.DB)
dataref := v.FieldByName("dataref")
data := v.FieldByName("data")
datasz := v.FieldByName("datasz")
assert.True(t, dataref.IsNil())
assert.True(t, data.IsNil())
assert.True(t, datasz.IsZero())
// We need to reopen the db, otherwise MustCheck may panic.
db.DB = nil
db.MustReopen()
}
func ExampleDB_Update() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)