Skip to content

Commit 340fc51

Browse files
committed
downgrade txn on OwnedLockGuard drop
1 parent cc9ffd4 commit 340fc51

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

libsql-wal/src/transaction.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,28 @@ pub struct WriteTransaction<F> {
155155
}
156156

157157
pub struct TxGuardOwned<F> {
158-
_lock: async_lock::MutexGuardArc<Option<u64>>,
159-
inner: WriteTransaction<F>,
158+
_lock: Option<async_lock::MutexGuardArc<Option<u64>>>,
159+
inner: Option<WriteTransaction<F>>,
160+
}
161+
162+
impl<F> Drop for TxGuardOwned<F> {
163+
fn drop(&mut self) {
164+
let _ = self._lock.take();
165+
self.inner.take().expect("already dropped").downgrade();
166+
}
160167
}
161168

162169
impl<F> Deref for TxGuardOwned<F> {
163170
type Target = WriteTransaction<F>;
164171

165172
fn deref(&self) -> &Self::Target {
166-
&self.inner
173+
self.inner.as_ref().expect("guard used after drop")
167174
}
168175
}
169176

170177
impl<F> DerefMut for TxGuardOwned<F> {
171178
fn deref_mut(&mut self) -> &mut Self::Target {
172-
&mut self.inner
179+
self.inner.as_mut().expect("guard used after drop")
173180
}
174181
}
175182

@@ -238,8 +245,8 @@ impl<F> WriteTransaction<F> {
238245
match *g {
239246
// we still hold the lock, we can proceed
240247
Some(id) if self.id == id => TxGuardOwned {
241-
_lock: g,
242-
inner: self,
248+
_lock: Some(g),
249+
inner: Some(self),
243250
},
244251
// Somebody took the lock from us
245252
Some(_) => todo!("lock stolen"),

0 commit comments

Comments
 (0)