Skip to content

Commit

Permalink
HOTFIX Mem leak while calling SetOptions (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
linxGnu authored Jan 30, 2024
1 parent f0bad2d commit a642c03
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ libs:

.PHONY: test
test:
go test -v -count=1 -tags testing,grocksdb_no_link
go test -race -v -count=1 -tags testing,grocksdb_no_link
1 change: 0 additions & 1 deletion backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func CreateBackupEngineWithPath(db *DB, path string) (be *BackupEngine, err erro
return
}


// CreateNewBackup takes a new backup from db.
func (b *BackupEngine) CreateNewBackup() (err error) {
var cErr *C.char
Expand Down
12 changes: 12 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,12 @@ func (db *DB) SetOptions(keys, values []string) (err error) {
)
err = fromCError(cErr)

// free before return
for i := range cKeys {
C.free(unsafe.Pointer(cKeys[i]))
C.free(unsafe.Pointer(cValues[i]))
}

return
}

Expand Down Expand Up @@ -1490,6 +1496,12 @@ func (db *DB) SetOptionsCF(cf *ColumnFamilyHandle, keys, values []string) (err e
)
err = fromCError(cErr)

// free before return
for i := range cKeys {
C.free(unsafe.Pointer(cKeys[i]))
C.free(unsafe.Pointer(cValues[i]))
}

return
}

Expand Down
40 changes: 33 additions & 7 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ func TestOpenDb(t *testing.T) {
require.True(t, success)
}

func TestSetOptions(t *testing.T) {
t.Parallel()

db := newTestDB(t, nil)
defer db.Close()

for i := 0; i < 100; i++ {
require.Error(t, db.SetOptions([]string{"a"}, []string{"b"}))
runtime.GC()
}
}

func TestSetOptionsCF(t *testing.T) {
t.Parallel()

db, cfh, cleanup := newTestDBMultiCF(t, []string{"default", "custom"}, nil)
defer cleanup()

for i := 0; i < 100; i++ {
require.Error(t, db.SetOptionsCF(cfh[1], []string{"a"}, []string{"b"}))
runtime.GC()
}
}

func TestDBCRUD(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -320,13 +344,15 @@ func TestLoadLatestOpts(t *testing.T) {
require.NoError(t, db.Flush(NewDefaultFlushOptions()))
db.Close()

o, err := LoadLatestOptions(dir, NewDefaultEnv(), true, NewLRUCache(1))
runtime.GC()
require.NoError(t, err)
require.NotEmpty(t, o.ColumnFamilyNames())
require.NotEmpty(t, o.ColumnFamilyOpts())
o.Destroy()
runtime.GC()
for i := 0; i < 10; i++ {
o, err := LoadLatestOptions(dir, NewDefaultEnv(), true, NewLRUCache(1))
runtime.GC()
require.NoError(t, err)
require.NotEmpty(t, o.ColumnFamilyNames())
require.NotEmpty(t, o.ColumnFamilyOpts())
o.Destroy()
runtime.GC()
}

_, err = LoadLatestOptions("", nil, true, nil)
require.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2665,7 +2665,6 @@ func LoadLatestOptions(path string, env *Env, ignoreUnknownOpts bool, cache *Cac
}

cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))

var env_ *C.rocksdb_env_t
if env != nil {
Expand Down Expand Up @@ -2705,6 +2704,7 @@ func LoadLatestOptions(path string, env *Env, ignoreUnknownOpts bool, cache *Cac
}
}

C.free(unsafe.Pointer(cPath))
return
}

Expand Down

0 comments on commit a642c03

Please sign in to comment.