This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 42a66a93a9 fix(executors/tokio): return None from sleep outside Tokio
runtime (#8925)
42a66a93a9 is described below
commit 42a66a93a9d02ca3f08046c9d39565a5ec86d746
Author: Xuanwo <[email protected]>
AuthorDate: Tue Nov 24 03:21:13 2026 +0800
fix(executors/tokio): return None from sleep outside Tokio runtime (#8925)
---
core/executors/tokio/src/lib.rs | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/core/executors/tokio/src/lib.rs b/core/executors/tokio/src/lib.rs
index e3d40d48e9..13a9a2ffc2 100644
--- a/core/executors/tokio/src/lib.rs
+++ b/core/executors/tokio/src/lib.rs
@@ -35,6 +35,8 @@ use opendal_core::raw::BoxedStaticFuture;
///
/// On non-`wasm32` targets, [`Execute::try_execute`] returns an error when no
/// Tokio runtime is active on the current thread instead of panicking.
+/// [`Execute::sleep`] returns `None` when no Tokio runtime is active on the
+/// current thread.
///
/// On `wasm32`, operations that submit background tasks must run inside a
/// [`tokio::task::LocalSet`] because the executor uses
[`tokio::task::spawn_local`].
@@ -70,6 +72,7 @@ impl Execute for TokioExecutor {
}
fn sleep(&self, duration: Duration) -> Option<BoxedStaticFuture<()>> {
+ tokio::runtime::Handle::try_current().ok()?;
Some(Box::pin(tokio::time::sleep(duration)))
}
}
@@ -129,4 +132,22 @@ mod tests {
.expect("task should finish before timeout")
.expect("task should send completion");
}
+
+ #[test]
+ fn test_sleep_outside_tokio_runtime() {
+ let executor = TokioExecutor::default();
+ assert!(executor.sleep(Duration::from_millis(1)).is_none());
+ }
+
+ #[tokio::test]
+ async fn test_sleep_inside_tokio_runtime() {
+ let executor = TokioExecutor::default();
+ let sleep = executor
+ .sleep(Duration::from_millis(10))
+ .expect("sleep should return a future inside a Tokio runtime");
+
+ timeout(Duration::from_secs(1), sleep)
+ .await
+ .expect("sleep should finish before timeout");
+ }
}