diff --git a/mdbx.h b/mdbx.h index d39e6e34..00bf8813 100644 --- a/mdbx.h +++ b/mdbx.h @@ -2509,7 +2509,16 @@ LIBMDBX_API int mdbx_txn_abort(MDBX_txn *txn); * * [in] txn A transaction handle returned by mdbx_txn_begin(). * - * Returns A non-zero error value on failure and 0 on success. */ + * Returns A non-zero error value on failure and 0 on success, some + * possible errors are: + * - MDBX_PANIC = a fatal error occurred earlier and the environment + * must be shut down. + * - MDBX_BAD_TXN = transaction is already fihished or never began. + * - MDBX_EBADSIGN = transaction object has invalid signature, + * e.g. transaction was already terminated + * or memory was corrupted. + * - MDBX_THREAD_MISMATCH = given transaction is not owned by current thread. + * - MDBX_EINVAL = transaction handle is NULL. */ LIBMDBX_API int mdbx_txn_reset(MDBX_txn *txn); /* Renew a read-only transaction. @@ -2522,9 +2531,14 @@ LIBMDBX_API int mdbx_txn_reset(MDBX_txn *txn); * * Returns A non-zero error value on failure and 0 on success, some * possible errors are: - * - MDBX_PANIC = a fatal error occurred earlier and the environment - * must be shut down. - * - MDBX_EINVAL = an invalid parameter was specified. */ + * - MDBX_PANIC = a fatal error occurred earlier and the environment + * must be shut down. + * - MDBX_BAD_TXN = transaction is already fihished or never began. + * - MDBX_EBADSIGN = transaction object has invalid signature, + * e.g. transaction was already terminated + * or memory was corrupted. + * - MDBX_THREAD_MISMATCH = transaction is running by other thread. + * - MDBX_EINVAL = transaction handle is NULL. */ LIBMDBX_API int mdbx_txn_renew(MDBX_txn *txn); /* The fours integers markers (aka "canary") associated with the environment. diff --git a/src/core.c b/src/core.c index 38d93803..35eb5509 100644 --- a/src/core.c +++ b/src/core.c @@ -6138,8 +6138,6 @@ static __always_inline int check_txn_rw(const MDBX_txn *txn, int bad_bits) { } int mdbx_txn_renew(MDBX_txn *txn) { - int rc; - if (unlikely(!txn)) return MDBX_EINVAL; @@ -6149,8 +6147,12 @@ int mdbx_txn_renew(MDBX_txn *txn) { if (unlikely((txn->mt_flags & MDBX_RDONLY) == 0)) return MDBX_EINVAL; - if (unlikely(txn->mt_owner != 0)) - return MDBX_THREAD_MISMATCH; + int rc; + if (unlikely(txn->mt_owner != 0 || !(txn->mt_flags & MDBX_TXN_FINISHED))) { + rc = mdbx_txn_reset(txn); + if (unlikely(rc != MDBX_SUCCESS)) + return rc; + } rc = mdbx_txn_renew0(txn, MDBX_RDONLY); if (rc == MDBX_SUCCESS) {