Skip to content

Commit

Permalink
rt: drop runtime before driver during shutdown (tokio-rs#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah-Kennedy committed Oct 29, 2022
1 parent 31b9ceb commit 507186d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Drop for Driver {
while self.num_operations() > 0 {
// If waiting fails, ignore the error. The wait will be attempted
// again on the next loop.
_ = self.wait();
let _ = self.wait();
self.tick();
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ pub struct Runtime {

/// Tokio runtime, always current-thread
rt: tokio::runtime::Runtime,

/// This is here for drop order reasons.
///
/// We can't unset the driver in the runtime drop method, because the inner runtime needs to
/// be dropped first so that there are no tasks running.
///
/// The rust drop order rules guarantee that the inner runtime will be dropped first, and
/// this last.
_driver_guard: RuntimeContextGuard,
}

/// Spawns a new asynchronous task, returning a [`JoinHandle`] for it.
Expand Down Expand Up @@ -80,6 +89,7 @@ impl Runtime {
uring_fd: driver_fd,
local,
rt,
_driver_guard: RuntimeContextGuard,
})
}

Expand Down Expand Up @@ -114,7 +124,9 @@ impl Runtime {
}
}

impl Drop for Runtime {
struct RuntimeContextGuard;

impl Drop for RuntimeContextGuard {
fn drop(&mut self) {
CONTEXT.with(|rc| rc.unset_driver())
}
Expand Down

0 comments on commit 507186d

Please sign in to comment.