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

suiliangliang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git


The following commit(s) were added to refs/heads/main by this push:
     new 145ddd9d fix(java): fix register class async jit thread safety (#2365)
145ddd9d is described below

commit 145ddd9ddf9c0425cecfc33044bb4c5b5ad2182b
Author: Shawn Yang <[email protected]>
AuthorDate: Tue Jun 24 12:19:39 2025 +0800

    fix(java): fix register class async jit thread safety (#2365)
    
    ## What does this PR do?
    
    fix register class async jit thread safety
    
    ## Related issues
    
    Closes #2364
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/fory/issues/new/choose) describing the
    need to do so and update the document if necessary.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    -->
---
 .../org/apache/fory/resolver/ClassResolver.java    |  9 +++++
 .../org/apache/fory/builder/JITContextTest.java    | 39 ++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git 
a/java/fory-core/src/main/java/org/apache/fory/resolver/ClassResolver.java 
b/java/fory-core/src/main/java/org/apache/fory/resolver/ClassResolver.java
index 5fb44794..42c4c556 100644
--- a/java/fory-core/src/main/java/org/apache/fory/resolver/ClassResolver.java
+++ b/java/fory-core/src/main/java/org/apache/fory/resolver/ClassResolver.java
@@ -1394,6 +1394,15 @@ public class ClassResolver implements TypeResolver {
   }
 
   private void createSerializerAhead(Class<?> cls) {
+    try {
+      fory.getJITContext().lock();
+      createSerializer0(cls);
+    } finally {
+      fory.getJITContext().unlock();
+    }
+  }
+
+  private void createSerializer0(Class<?> cls) {
     ClassInfo classInfo = getClassInfo(cls);
     ClassInfo deserializationClassInfo;
     if (metaContextShareEnabled && needToWriteClassDef(classInfo.serializer)) {
diff --git 
a/java/fory-core/src/test/java/org/apache/fory/builder/JITContextTest.java 
b/java/fory-core/src/test/java/org/apache/fory/builder/JITContextTest.java
index 4be17e42..415d4ade 100644
--- a/java/fory-core/src/test/java/org/apache/fory/builder/JITContextTest.java
+++ b/java/fory-core/src/test/java/org/apache/fory/builder/JITContextTest.java
@@ -24,10 +24,14 @@ import static org.testng.Assert.assertTrue;
 
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
 import lombok.Data;
 import org.apache.fory.Fory;
 import org.apache.fory.ForyTestBase;
+import org.apache.fory.ThreadSafeFory;
 import org.apache.fory.config.CompatibleMode;
 import org.apache.fory.config.Language;
 import org.apache.fory.logging.Logger;
@@ -167,6 +171,41 @@ public class JITContextTest extends ForyTestBase {
     serDeCheck(fory, o);
   }
 
+  @Test(timeOut = 60000)
+  public void testThreadSafetyWithManyForyInstances() throws Exception {
+    final int threadCount = 1000;
+    final CountDownLatch latch = new CountDownLatch(threadCount);
+    final List<Throwable> errors = Collections.synchronizedList(new 
ArrayList<>());
+
+    for (int i = 0; i < threadCount; i++) {
+      new Thread(
+              () -> {
+                try {
+                  ThreadSafeFory fory =
+                      Fory.builder()
+                          .withLanguage(Language.JAVA)
+                          .requireClassRegistration(true)
+                          .withAsyncCompilation(true)
+                          .withCompatibleMode(CompatibleMode.COMPATIBLE)
+                          .buildThreadSafeForyPool(2, 4, 1, 
java.util.concurrent.TimeUnit.MINUTES);
+                  fory.register(BeanB.class, true);
+                  // fory.register(BeanA.class, true);
+                } catch (Throwable t) {
+                  errors.add(t);
+                } finally {
+                  latch.countDown();
+                }
+              })
+          .start();
+    }
+    latch.await();
+    // print stack trace in errors
+    for (Throwable error : errors) {
+      error.printStackTrace();
+    }
+    assertTrue(errors.isEmpty(), "No exceptions should be thrown: " + errors);
+  }
+
   @Data
   public static final class TestAccessLevel {
     PkgAccessLevel f1;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to