-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement DropGuard to ensure future Completion
To manage state transitions safely across `await` boundaries, we introduced a `DropGuard` wrapper. This ensures that even if a future is dropped before completion, it will continue executing in the background via `tokio::spawn`. This approach allows us to avoid the pitfalls of incomplete state transitions in critical futures without the overhead of spawning all futures immediately. The `DropGuard` checks if the inner future has completed; if not, it safely spawns the future in the background during its drop. This solution strikes a balance between stack-based future management and the necessity to complete crucial futures.
- Loading branch information
Showing
5 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
pub struct DropGuard<F: Future> { | ||
future: Option<Pin<Box<F>>>, | ||
} | ||
|
||
impl<F: Future> DropGuard<F> { | ||
pub fn new(future: F) -> Self { | ||
Self { | ||
future: Some(Box::pin(future)), | ||
} | ||
} | ||
} | ||
|
||
impl<F: Future> Future for DropGuard<F> { | ||
type Output = F::Output; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
if let Some(future) = self.future.as_mut() { | ||
match future.as_mut().poll(cx) { | ||
Poll::Ready(output) => { | ||
self.future = None; // Set future to None after it completes | ||
Poll::Ready(output) | ||
} | ||
Poll::Pending => Poll::Pending, | ||
} | ||
} else { | ||
panic!("Future already completed"); | ||
} | ||
} | ||
} | ||
|
||
impl<F: Future> Drop for DropGuard<F> { | ||
fn drop(&mut self) { | ||
if let Some(future) = self.future.take() { | ||
// Block on the future to ensure it completes. | ||
futures::executor::block_on(future); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters