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

调用【com.alibaba.fastjson2.JSON#parseObject(byte[], java.lang.Class<T>, com.alibaba.fastjson2.filter.Filter, com.alibaba.fastjson2.JSONReader.Feature...)】出现报错【java.util.ConcurrentModificationException】 #3311

Open
iAmClever opened this issue Jan 24, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@iAmClever
Copy link

问题描述

服务里面引用了canal-client1.1.8、rocketmq-client 4.9.8,以及fastjson2 2.0.54,启动的时候会去初始化canal-client 以及 rocketMq的producer。在启动过程中fastjson2出现报错【java.util.ConcurrentModificationException】

Image

环境信息

  • OS信息: [Ubuntu 20.04.2 LTS]
  • JDK信息: [JDK 11]
  • fastjson version:[e.g.:Fastjson2 2.0.54]
  • mysql version: 8.0.29
  • canal version: 1.1.8
  • rocketmq-client version: 4.9.8

重现步骤

  1. 引入rocketmq,并去使用它
  2. 在spring容器启动过程中,去使用【com.alibaba.fastjson2.JSON#parseObject(byte[], java.lang.Class, com.alibaba.fastjson2.filter.Filter, com.alibaba.fastjson2.JSONReader.Feature...)】
  3. 出现【java.util.ConcurrentModificationException】错误

原因

rocketmq 在启动的过程会去注册多个module,如下
Image

然后在启动的同时,由于我们使用了canal client,会去调用到【com.alibaba.fastjson2.JSON#parseObject(byte[], java.lang.Class, com.alibaba.fastjson2.filter.Filter, com.alibaba.fastjson2.JSONReader.Feature...)】,最后触发【com.alibaba.fastjson2.reader.ObjectReaderProvider#getObjectReaderInternal】,会去遍历modules,由于modules不是线程安全的,导致报错!

Image

解决方案

我的想法有两个:
1.使用for (int i = 0; i < modules.size(); i++) {} 去避免报错,但这种只能应对添加module导致的报错,无法避免移除module导致的报错(虽然很少会有人会去移除module)

2.(推荐)把modules类型改成线程安全的CopyOnWriteArrayList,虽然会影响到添加/删除modules的效率,但对于modules来说,查询场景远远大于添加/删除场景,这样可以彻底解决【java.util.ConcurrentModificationException】错误

如果认定这是一个 bug,我可以尝试提交修改的 PR ~~~

@iAmClever iAmClever added the bug Something isn't working label Jan 24, 2025
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

1 participant