From f02e12d024c039ab8728a66a564cfb617a98932a Mon Sep 17 00:00:00 2001 From: Peter van Dijk Date: Thu, 26 Aug 2021 11:51:28 +0200 Subject: [PATCH] lmdb-safe: resizing while there might be open transactions is unsafe --- ext/lmdb-safe/lmdb-safe.cc | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/ext/lmdb-safe/lmdb-safe.cc b/ext/lmdb-safe/lmdb-safe.cc index 7b968e20d6..7a023fd05a 100644 --- a/ext/lmdb-safe/lmdb-safe.cc +++ b/ext/lmdb-safe/lmdb-safe.cc @@ -176,18 +176,9 @@ MDB_txn *MDBRWTransactionImpl::openRWTransaction(MDBEnv *env, MDB_txn *parent, i if(env->getROTX() || env->getRWTX()) throw std::runtime_error("Duplicate RW transaction"); - for(int tries =0 ; tries < 3; ++tries) { // it might happen twice, who knows - if(int rc=mdb_txn_begin(env->d_env, parent, flags, &result)) { - if(rc == MDB_MAP_RESIZED && tries < 2) { - // "If the mapsize is increased by another process (..) mdb_txn_begin() will return MDB_MAP_RESIZED. - // call mdb_env_set_mapsize with a size of zero to adopt the new size." - mdb_env_set_mapsize(env->d_env, 0); - continue; - } - throw std::runtime_error("Unable to start RW transaction: "+std::string(mdb_strerror(rc))); - } - break; - } + if(int rc=mdb_txn_begin(env->d_env, parent, flags, &result)) + throw std::runtime_error("Unable to start RW transaction: "+std::string(mdb_strerror(rc))); + env->incRWTX(); return result; } @@ -245,19 +236,10 @@ MDB_txn *MDBROTransactionImpl::openROTransaction(MDBEnv *env, MDB_txn *parent, i /* A transaction and its cursors must only be used by a single thread, and a thread may only have a single transaction at a time. If MDB_NOTLS is in use, this does not apply to read-only transactions. */ MDB_txn *result = nullptr; - for(int tries =0 ; tries < 3; ++tries) { // it might happen twice, who knows - if(int rc=mdb_txn_begin(env->d_env, parent, MDB_RDONLY | flags, &result)) { - if(rc == MDB_MAP_RESIZED && tries < 2) { - // "If the mapsize is increased by another process (..) mdb_txn_begin() will return MDB_MAP_RESIZED. - // call mdb_env_set_mapsize with a size of zero to adopt the new size." - mdb_env_set_mapsize(env->d_env, 0); - continue; - } - - throw std::runtime_error("Unable to start RO transaction: "+string(mdb_strerror(rc))); - } - break; - } + + if(int rc=mdb_txn_begin(env->d_env, parent, MDB_RDONLY | flags, &result)) + throw std::runtime_error("Unable to start RO transaction: "+string(mdb_strerror(rc))); + env->incROTX(); return result; -- 2.47.2