Xuanwo commented on code in PR #5746:
URL: https://github.com/apache/opendal/pull/5746#discussion_r1990477048


##########
bindings/java/src/executor.rs:
##########
@@ -43,7 +43,8 @@ thread_local! {
 /// This function could be only called by java vm when unload this lib.
 #[no_mangle]
 pub unsafe extern "system" fn JNI_OnUnload(_: JavaVM, _: *mut c_void) {
-    let _ = RUNTIME.take();
+    // This is fine as the JVM is shutting down anyway

Review Comment:
   Hi, please don't change this unless we have good reasons.



##########
bindings/java/src/executor.rs:
##########
@@ -186,10 +187,25 @@ pub(crate) fn executor_or_default<'a>(
 ///
 /// This function could be only when the lib is loaded.
 unsafe fn default_executor<'a>(env: &mut JNIEnv<'a>) -> Result<&'a Executor> {
-    RUNTIME.get_or_try_init(|| {
-        make_tokio_executor(
-            env,
-            available_parallelism().map(NonZeroUsize::get).unwrap_or(1),
+    // Return the executor if it's already initialized
+    if let Some(runtime) = RUNTIME.get() {
+        return Ok(runtime);
+    }
+
+    // Try to initialize the executor
+    let executor = make_tokio_executor(
+        env,
+        available_parallelism().map(NonZeroUsize::get).unwrap_or(1),
+    )?;
+    
+    // It's okay if another thread initialized it first
+    let _ = RUNTIME.set(executor);
+    
+    // Now RUNTIME should be initialized
+    Ok(RUNTIME.get().ok_or_else(|| {
+        opendal::Error::new(
+            opendal::ErrorKind::Unexpected,
+            "Failed to initialize default executor",
         )
-    })
+    })?)

Review Comment:
   Maybe we can use ` get_or_init` here to save an extra error check:
   
   ```rust
   Ok(RUNTIME.get_or_init(|| executor))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to