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


##########
bindings/java/src/executor.rs:
##########
@@ -186,10 +184,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:
   ```suggestion
   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