Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(libmdbx): renew tx on timeout when dropping cursor #10840

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/storage/libmdbx-rs/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,11 @@ where
K: TransactionKind,
{
fn drop(&mut self) {
// Ignore the error, because we're dropping the cursor anyway.
let _ = self.txn.txn_execute(|_| unsafe { ffi::mdbx_cursor_close(self.cursor) });
// To be able to close a cursor of a timed out transaction, we need to renew it first.
// Hence the usage of `txn_execute_renew_on_timeout` here.
let _ = self
.txn
.txn_execute_renew_on_timeout(|_| unsafe { ffi::mdbx_cursor_close(self.cursor) });
}
}

Expand Down
22 changes: 21 additions & 1 deletion crates/storage/libmdbx-rs/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ where
self.inner.txn_execute(f)
}

/// Executes the given closure once the lock on the transaction is acquired. If the transaction
/// is timed out, it will be renewed first.
///
/// Returns the result of the closure or an error if the transaction renewal fails.
#[inline]
pub(crate) fn txn_execute_renew_on_timeout<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(*mut ffi::MDBX_txn) -> T,
{
self.inner.txn_execute_renew_on_timeout(f)
}

/// Returns a copy of the raw pointer to the underlying MDBX transaction.
#[doc(hidden)]
#[cfg(test)]
Expand Down Expand Up @@ -321,6 +333,14 @@ where
{
self.txn.txn_execute_fail_on_timeout(f)
}

#[inline]
fn txn_execute_renew_on_timeout<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(*mut ffi::MDBX_txn) -> T,
{
self.txn.txn_execute_renew_on_timeout(f)
}
}

impl<K> Drop for TransactionInner<K>
Expand Down Expand Up @@ -596,7 +616,7 @@ impl TransactionPtr {
///
/// Returns the result of the closure or an error if the transaction renewal fails.
#[inline]
fn txn_execute_renew_on_timeout<F, T>(&self, f: F) -> Result<T>
pub(crate) fn txn_execute_renew_on_timeout<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(*mut ffi::MDBX_txn) -> T,
{
Expand Down
Loading