From db406229bf8be2f4d4f3b56d135c77b9a9e9d7c8 Mon Sep 17 00:00:00 2001 From: Christopher Turner Date: Fri, 13 Nov 2026 13:10:49 +0700 Subject: [PATCH 1/2] Free the env when open fails --- src/rocksdb.cc | 5 ++++- test/CMakeLists.txt | 1 + test/open-locked.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 test/open-locked.c diff --git a/src/rocksdb.cc b/src/rocksdb.cc index 1ee8831..c855759 100644 --- a/src/rocksdb.cc +++ b/src/rocksdb.cc @@ -1086,9 +1086,10 @@ rocksdb__on_open(uv_work_t *handle) { column_families[i] = ColumnFamilyDescriptor(column_family.name, options); } + // freed on return unless open succeeds auto env = NewCompositeEnv(std::make_shared()); - options.env = env.release(); + options.env = env.get(); auto handles = std::vector(req->len); @@ -1124,6 +1125,8 @@ rocksdb__on_open(uv_work_t *handle) { db->read_only = read_only; if (status.ok()) { + env.release(); // freed in rocksdb__on_close() + db->handle = ptr.release(); db->lock = lock; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f6200db..cd048ae 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,6 +14,7 @@ list(APPEND tests open-close open-close-immediately open-close-sync + open-locked read-only-wal-filter write-flush write-read diff --git a/test/open-locked.c b/test/open-locked.c new file mode 100644 index 0000000..2fe0a36 --- /dev/null +++ b/test/open-locked.c @@ -0,0 +1,48 @@ +#include +#include +#include + +#include "../include/rocksdb.h" + +int +main() { + int e; + + uv_loop_t *loop = uv_default_loop(); + + rocksdb_options_t options = { + .create_if_missing = true, + }; + + rocksdb_column_family_descriptor_t descriptor = rocksdb_column_family_descriptor("default", NULL); + + rocksdb_t db; + rocksdb_column_family_t *family; + + rocksdb_open_t open; + e = rocksdb_open(loop, &db, &open, "test/fixtures/open-locked.db", &options, &descriptor, &family, 1, NULL, NULL); + assert(e == 0); + assert(open.error == NULL); + rocksdb_open_cleanup(&open); + + // fails on the lock held by the first open + { + rocksdb_t db; + rocksdb_column_family_t *family; + + rocksdb_open_t open; + e = rocksdb_open(loop, &db, &open, "test/fixtures/open-locked.db", &options, &descriptor, &family, 1, NULL, NULL); + assert(e == 0); + assert(open.error != NULL); + rocksdb_open_cleanup(&open); + } + + e = rocksdb_column_family_destroy(&db, family); + assert(e == 0); + + rocksdb_close_t close; + e = rocksdb_close(&db, &close, NULL, NULL); + assert(e == 0); + assert(close.error == NULL); + rocksdb_close_cleanup(&close); +} From 8b9ab6b38a482377e4175bf090525e456feba533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 13 Nov 2026 07:26:26 +0100 Subject: [PATCH 2/2] Apply batched suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kasper Isager Dalsgarð --- src/rocksdb.cc | 3 +-- test/open-locked.c | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/rocksdb.cc b/src/rocksdb.cc index c855759..858317e 100644 --- a/src/rocksdb.cc +++ b/src/rocksdb.cc @@ -1086,7 +1086,6 @@ rocksdb__on_open(uv_work_t *handle) { column_families[i] = ColumnFamilyDescriptor(column_family.name, options); } - // freed on return unless open succeeds auto env = NewCompositeEnv(std::make_shared()); options.env = env.get(); @@ -1125,7 +1124,7 @@ rocksdb__on_open(uv_work_t *handle) { db->read_only = read_only; if (status.ok()) { - env.release(); // freed in rocksdb__on_close() + env.release(); db->handle = ptr.release(); db->lock = lock; diff --git a/test/open-locked.c b/test/open-locked.c index 2fe0a36..d71939d 100644 --- a/test/open-locked.c +++ b/test/open-locked.c @@ -25,7 +25,7 @@ main() { assert(open.error == NULL); rocksdb_open_cleanup(&open); - // fails on the lock held by the first open + // Fails on the lock held by the first open. { rocksdb_t db; rocksdb_column_family_t *family;