Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]2.0.11 fastjson1并发执行JSON.toJSONString异常 #661

Closed
void-vegetable opened this issue Aug 9, 2022 · 3 comments
Closed

[BUG]2.0.11 fastjson1并发执行JSON.toJSONString异常 #661

void-vegetable opened this issue Aug 9, 2022 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@void-vegetable
Copy link

void-vegetable commented Aug 9, 2022

问题描述

ForkJoinPool环境下对某些对象序列化时抛出异常
fastjson2暂未发现该问题

环境信息

  • OS信息: [e.g.:Windows]
  • JDK信息: [e.g.:Openjdk 1.8.0_321]
  • 版本信息:[e.g.:Fastjson 2.0.11]

重现步骤

junit-platform.properties配置如下

junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent
junit.jupiter.execution.parallel.mode.classes.default=concurrent
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=2

并发执行ParallelTest中的测试用例,或者执行ContrastTest中的testParallel可能抛出异常

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;


public class ParallelTest {
    @Data
    static class Response<T> {
        T data;

        Response(T data) {
            this.data = data;
        }
    }

    @Data
    static class A {
        // 没有属性不会异常
        int a;
    }

    @Test
    void testB() {
        for (int i = 0; i < 100000; i++) {
            System.out.println(JSON.toJSONString(new Response<>(new A())));
        }
    }

    @Test
    void testList() {
        for (int i = 0; i < 100000; i++) {
            // Collections.singletonList(0)改成0也会异常,异常概率小一些
            System.out.println(JSON.toJSONString(new Response<>(Collections.singletonList(0))));
        }
    }
}

class ContrastTest {
    @Test
    void testParallel() {
        // 在ForkJoinPool中异常
        Stream.of(() -> {
                    for (int i = 0; i < 100000; i++) {
                        System.out.println(JSON.toJSONString(new ParallelTest.Response<>(Collections.singletonList(0))));
                    }
                },
                (Runnable)() -> {
                    for (int i = 0; i < 100000; i++) {
                        System.out.println(JSON.toJSONString(new ParallelTest.Response<>(new ParallelTest.A())));
                    }
                }).parallel().forEach(Runnable::run);
    }

    @Test
    void normal() throws InterruptedException {
        // 普通线程池中执行正常
        ExecutorService pool = Executors.newFixedThreadPool(2);
        CountDownLatch cdl = new CountDownLatch(2);
        pool.execute(() -> {
            for (int i = 0; i < 100000; i++) {
                System.out.println(JSON.toJSONString(new ParallelTest.Response<>(Collections.singletonList(0))));
            }
            cdl.countDown();
        });
        pool.execute(() -> {
            for (int i = 0; i < 100000; i++) {
                System.out.println(JSON.toJSONString(new ParallelTest.Response<>(new ParallelTest.A())));
            }
            cdl.countDown();
        });
        cdl.await();
        pool.shutdown();
    }
}

期待的正确结果

不抛出异常

相关日志输出

com.alibaba.fastjson.JSONException: toJSONString error

	at com.alibaba.fastjson.JSON.toJSONString(JSON.java:776)
	at ParallelTest.testList(ParallelTest.java:47)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@void-vegetable void-vegetable added the bug Something isn't working label Aug 9, 2022
wenshao added a commit that referenced this issue Aug 9, 2022
@wenshao wenshao added this to the 2.0.12 milestone Aug 15, 2022
@wenshao
Copy link
Member

wenshao commented Aug 15, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.12-SNAPSHOT/
问题已经修复,请帮忙用2.0.12-SNAPSHOT版本验证,2.0.12版本预计在8月21日前发布

@void-vegetable
Copy link
Author

我使用fastjson1-2.0.11,通过额外引入fastjson2-2.0.12-SNAPSHOT能够解决这个问题,是否可以不用额外引入fastjson2的依赖?

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.12-SNAPSHOT/fastjson-2.0.12-20220815.174204-18.jar
然后我下载了上面这个fastjson的依赖,调用JSON.toJSONString时引发了异常
java.lang.NoClassDefFoundError: com/alibaba/fastjson2/support/AwtRederModule

@wenshao
Copy link
Member

wenshao commented Aug 20, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.12
问题已修复,请用新版本

@wenshao wenshao closed this as completed Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants