Skip to content

Commit

Permalink
fix(simple): timeout usage
Browse files Browse the repository at this point in the history
The previous implementation of timeouts for dropping had a bug -- the
timeout value was saved *after* calling `std::mem::take`, and was
referring to the new `T::default()` instance's value of `timeout`,
which is always `None`.

This commit fixes the timeout usage by taking the timeout value before
`std::mem::take`.
  • Loading branch information
t3hmrman committed Jan 31, 2024
1 parent 7ff6cee commit 12935c3
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions crates/async-dropper-simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,20 @@ impl<T: AsyncDrop + Default + Send + 'static> Drop for AsyncDropper<T> {
impl<T: AsyncDrop + Default + Send + 'static> Drop for AsyncDropper<T> {
fn drop(&mut self) {
if !self.dropped {
// Set the original instance to be dropped
self.dropped = true;

// Save the timeout on the original instance
let timeout = self.timeout;

// Swap out the current instance with default
// (i.e. `this` is now original instance, and `self` is a default instance)
let mut this = std::mem::take(self);
this.dropped = true;

// Set the default instance to note that it's dropped
self.dropped = true;

// Create task
let timeout = self.timeout;
let task = tokio::spawn(async move {
this.inner.async_drop().await;
});
Expand All @@ -84,11 +93,20 @@ impl<T: AsyncDrop + Default + Send + 'static> Drop for AsyncDropper<T> {
impl<T: AsyncDrop + Default + Send + 'static> Drop for AsyncDropper<T> {
fn drop(&mut self) {
if !self.dropped {
// Set the original instance to be dropped
self.dropped = true;

// Save the timeout on the original instance
let timeout = self.timeout;

// Swap out the current instance with default
// (i.e. `this` is now original instance, and `self` is a default instance)
let mut this = std::mem::take(self);
this.dropped = true;

// Set the default instance to note that it's dropped
self.dropped = true;

// Create task
let timeout = self.timeout.clone();
let task = async_std::task::spawn(async move {
this.inner.async_drop().await;
});
Expand Down

0 comments on commit 12935c3

Please sign in to comment.