This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch xuanwo/fix-dotnet-executor-inflight-lifetime
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 7dc697d291746f04a3ed83acd46f8bf3c3c875d8
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 17:05:12 2026 +0100

    fix(bindings/dotnet): keep custom executor alive for in-flight async tasks 
and avoid drop panic on runtime threads
---
 bindings/dotnet/src/executor.rs |  94 +++++++++++++++++++--
 bindings/dotnet/src/operator.rs | 177 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 265 insertions(+), 6 deletions(-)

diff --git a/bindings/dotnet/src/executor.rs b/bindings/dotnet/src/executor.rs
index 4724fae26d..e44f04e518 100644
--- a/bindings/dotnet/src/executor.rs
+++ b/bindings/dotnet/src/executor.rs
@@ -28,7 +28,7 @@ use crate::utils::config_invalid_error;
 static DEFAULT_EXECUTOR: OnceLock<Arc<Executor>> = OnceLock::new();
 
 pub struct Executor {
-    runtime: tokio::runtime::Runtime,
+    runtime: Option<tokio::runtime::Runtime>,
 }
 
 impl Executor {
@@ -53,19 +53,51 @@ impl Executor {
                 )
             })?;
 
-        Ok(Self { runtime })
+        Ok(Self {
+            runtime: Some(runtime),
+        })
+    }
+
+    fn runtime(&self) -> &tokio::runtime::Runtime {
+        self.runtime
+            .as_ref()
+            .expect("executor runtime is only taken in Drop")
     }
 
     pub fn block_on<F: Future>(&self, future: F) -> F::Output {
-        self.runtime.block_on(future)
+        self.runtime().block_on(future)
     }
 
-    pub fn spawn<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>
+    pub fn spawn<F>(self: &Arc<Self>, future: F) -> 
tokio::task::JoinHandle<F::Output>
     where
         F: Future + Send + 'static,
         F::Output: Send + 'static,
     {
-        self.runtime.spawn(future)
+        // Keep the executor alive until the spawned future finishes so freeing
+        // the caller's handles does not cancel in-flight FFI callbacks.
+        let keep_alive = Arc::clone(self);
+        self.runtime().spawn(async move {
+            let _keep_alive = keep_alive;
+            future.await
+        })
+    }
+}
+
+impl Drop for Executor {
+    fn drop(&mut self) {
+        let Some(runtime) = self.runtime.take() else {
+            return;
+        };
+
+        // Dropping a Tokio runtime inside an async runtime context panics and
+        // aborts across `extern "C"`. Use non-blocking shutdown when the last
+        // reference drops on a runtime thread (for example when the final
+        // in-flight task finishes or a completion callback frees handles).
+        if tokio::runtime::Handle::try_current().is_ok() {
+            runtime.shutdown_background();
+        } else {
+            drop(runtime);
+        }
     }
 }
 
@@ -132,8 +164,58 @@ pub unsafe extern "C" fn executor_free(executor: *mut 
c_void) {
     }
 
     // SAFETY: reclaims the reference handed out by `executor_create`.
-    // Operators hold their own clones, so in-flight work keeps its runtime.
+    // Operators and in-flight spawned tasks hold their own clones, so work
+    // keeps its runtime alive until completion.
     unsafe {
         drop(Arc::from_raw(executor as *const Executor));
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use std::sync::mpsc;
+    use std::time::Duration;
+
+    #[test]
+    fn spawn_keeps_runtime_alive_after_external_drop() {
+        let executor = Arc::new(Executor::new(1).expect("create executor"));
+        let (started_tx, started_rx) = mpsc::channel();
+        let (release_tx, release_rx) = tokio::sync::oneshot::channel::<()>();
+
+        let handle = executor.spawn(async move {
+            started_tx.send(()).expect("signal task started");
+            release_rx.await.expect("receive release signal");
+            42usize
+        });
+
+        started_rx
+            .recv_timeout(Duration::from_secs(5))
+            .expect("spawned task should start");
+
+        // Drop the caller's last handle while the spawned task is suspended.
+        drop(executor);
+
+        release_tx
+            .send(())
+            .expect("runtime must not cancel in-flight task when caller drops 
executor");
+
+        let output = futures::executor::block_on(handle).expect(
+            "in-flight task must finish without cancellation or panic on final 
executor drop",
+        );
+        assert_eq!(output, 42);
+    }
+
+    #[test]
+    fn dropping_executor_inside_runtime_does_not_panic() {
+        let host = Arc::new(Executor::new(1).expect("create host executor"));
+        let target = Arc::new(Executor::new(1).expect("create target 
executor"));
+
+        let handle = host.spawn(async move {
+            drop(target);
+        });
+
+        futures::executor::block_on(handle)
+            .expect("dropping executor on a Tokio worker thread must not 
panic");
+    }
+}
diff --git a/bindings/dotnet/src/operator.rs b/bindings/dotnet/src/operator.rs
index b621955bf3..db944b1990 100644
--- a/bindings/dotnet/src/operator.rs
+++ b/bindings/dotnet/src/operator.rs
@@ -2527,3 +2527,180 @@ fn operator_list_with_options_async_inner(
 
     Ok(())
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use std::collections::HashMap;
+    use std::ffi::CString;
+    use std::ptr;
+    use std::sync::atomic::{AtomicI64, Ordering};
+    use std::sync::{Mutex as StdMutex, OnceLock, mpsc};
+
+    use crate::buffer::read_buffer_copy_to;
+    use crate::executor::{executor_create, executor_free};
+    use crate::result::{opendal_metadata_result_release, 
opendal_read_result_release};
+
+    struct CallbackContext {
+        tx: mpsc::Sender<Result<Vec<u8>, i32>>,
+        op_to_free: Option<*mut OperatorHandle>,
+        executor_to_free: Option<*mut c_void>,
+    }
+
+    // SAFETY: Raw handles are transferred once to the completion callback,
+    // which frees each handle at most once.
+    unsafe impl Send for CallbackContext {}
+
+    fn callback_registry() -> &'static StdMutex<HashMap<i64, CallbackContext>> 
{
+        static REGISTRY: OnceLock<StdMutex<HashMap<i64, CallbackContext>>> = 
OnceLock::new();
+        REGISTRY.get_or_init(|| StdMutex::new(HashMap::new()))
+    }
+
+    fn register_callback(ctx: CallbackContext) -> i64 {
+        static NEXT_ID: AtomicI64 = AtomicI64::new(1);
+        let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
+        callback_registry()
+            .lock()
+            .expect("lock callback registry")
+            .insert(id, ctx);
+        id
+    }
+
+    extern "C" fn test_read_callback(context: i64, result: OpendalReadResult) {
+        let ctx = callback_registry()
+            .lock()
+            .expect("lock callback registry")
+            .remove(&context)
+            .expect("registered callback context");
+
+        let payload = if result.error.has_error != 0 {
+            Err(result.error.code)
+        } else {
+            let mut bytes = vec![0u8; result.buffer.len];
+            if !bytes.is_empty() {
+                let copied = unsafe {
+                    read_buffer_copy_to(result.buffer.handle, 0, 
bytes.as_mut_ptr(), bytes.len())
+                };
+                bytes.truncate(copied);
+            }
+            Ok(bytes)
+        };
+
+        opendal_read_result_release(result);
+
+        if let Some(op_ptr) = ctx.op_to_free {
+            unsafe { operator_free(op_ptr) };
+        }
+        if let Some(executor_ptr) = ctx.executor_to_free {
+            unsafe { executor_free(executor_ptr) };
+        }
+
+        let _ = ctx.tx.send(payload);
+    }
+
+    fn create_memory_operator(executor_ptr: *const c_void) -> *mut 
OperatorHandle {
+        let scheme = CString::new("memory").expect("scheme cstr");
+        let op_res = unsafe {
+            operator_construct(scheme.as_ptr(), ptr::null(), ptr::null(), 0, 
executor_ptr)
+        };
+        assert_eq!(op_res.error.has_error, 0, "construct memory operator");
+        assert!(!op_res.ptr.is_null());
+        op_res.ptr as *mut OperatorHandle
+    }
+
+    fn write_test_object(op_ptr: *const OperatorHandle, path: &str, data: 
&[u8]) {
+        let path_cstr = CString::new(path).expect("path cstr");
+        let write_res = unsafe {
+            operator_write_bytes_with_options(
+                op_ptr,
+                path_cstr.as_ptr(),
+                data.as_ptr(),
+                data.len(),
+                ptr::null(),
+            )
+        };
+        assert_eq!(write_res.error.has_error, 0, "write test object");
+        opendal_metadata_result_release(write_res);
+    }
+
+    #[test]
+    fn async_ffi_read_completes_after_handles_freed() {
+        let exec_res = executor_create(1);
+        assert_eq!(exec_res.error.has_error, 0);
+        let executor_ptr = exec_res.ptr;
+
+        let op_ptr = create_memory_operator(executor_ptr);
+        write_test_object(op_ptr, "inflight.txt", b"hello-inflight");
+
+        let path_cstr = CString::new("inflight.txt").expect("path cstr");
+        let stream_res = operator_input_stream_create(op_ptr, 
path_cstr.as_ptr(), ptr::null());
+        assert_eq!(stream_res.error.has_error, 0);
+        let stream_ptr = stream_res.ptr;
+
+        // Hold the stream's async mutex so the spawned read task 
deterministically
+        // suspends at `.await` while we free every handle on the calling 
thread.
+        let inner = unsafe { (&*(stream_ptr as *const 
InputStream)).inner.clone() };
+        let guard = futures::executor::block_on(inner.lock());
+
+        let (tx, rx) = mpsc::channel();
+        let ctx_id = register_callback(CallbackContext {
+            tx,
+            op_to_free: None,
+            executor_to_free: None,
+        });
+
+        let start_res = unsafe {
+            operator_input_stream_read_next_async(stream_ptr, 
Some(test_read_callback), ctx_id)
+        };
+        assert_eq!(start_res.error.has_error, 0);
+
+        // Free stream, operator, and executor while the async read is still 
in flight.
+        unsafe {
+            operator_input_stream_free(stream_ptr);
+            operator_free(op_ptr);
+            executor_free(executor_ptr);
+        }
+
+        // Release the mutex so the in-flight task can finish and invoke the 
callback.
+        drop(guard);
+
+        let payload = rx
+            .recv_timeout(Duration::from_secs(5))
+            .expect("in-flight async FFI callback must still fire after 
handles are freed")
+            .expect("async FFI read should succeed");
+        assert_eq!(payload, b"hello-inflight");
+    }
+
+    #[test]
+    fn freeing_operator_and_executor_inside_async_callback_does_not_panic() {
+        let exec_res = executor_create(1);
+        assert_eq!(exec_res.error.has_error, 0);
+        let executor_ptr = exec_res.ptr;
+
+        let op_ptr = create_memory_operator(executor_ptr);
+        write_test_object(op_ptr, "callback-free.txt", b"hello-callback");
+
+        let path_cstr = CString::new("callback-free.txt").expect("path cstr");
+        let (tx, rx) = mpsc::channel();
+        let ctx_id = register_callback(CallbackContext {
+            tx,
+            op_to_free: Some(op_ptr),
+            executor_to_free: Some(executor_ptr),
+        });
+
+        let start_res = operator_read_with_options_async(
+            op_ptr,
+            path_cstr.as_ptr(),
+            ptr::null(),
+            Some(test_read_callback),
+            ctx_id,
+        );
+        assert_eq!(start_res.error.has_error, 0);
+
+        let payload = rx
+            .recv_timeout(Duration::from_secs(5))
+            .expect("async FFI callback should complete when freeing handles 
on worker thread")
+            .expect("async FFI read should succeed");
+        assert_eq!(payload, b"hello-callback");
+    }
+}

Reply via email to