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

SpyAPI.atEnter 可能抛出 ConcurrentModificationException #2952

Open
hengyunabc opened this issue Nov 20, 2024 · 2 comments
Open

SpyAPI.atEnter 可能抛出 ConcurrentModificationException #2952

hengyunabc opened this issue Nov 20, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@hengyunabc
Copy link
Collaborator

java.arthas.SpyAPI.atEnter 可能抛出 ConcurrentModificationException 。版本 3.6.5

image

@hengyunabc hengyunabc added the bug Something isn't working label Nov 20, 2024
@lichaosuns
Copy link

这里看起来是注册AdviceListener和迭代访问多线程出发导致的,大佬有什么解决的思路么?
我的想法是:
1、使用 CopyOnWriteArrayList,适合读多写少
2、使用显示的lock进行线程同步

@trytocatch
Copy link

我觉得CopyOnWriteArrayList是个不错的办法,修改是很低频的操作,主要还是在方法调用的时候,读这个list

另外,相应的还发现个问题,像AdviceListenerManager#registerTraceAdviceListener,没有被synchronized(this)保护

        public void registerTraceAdviceListener(String className, String owner, String methodName, String methodDesc,
                AdviceListener listener) {

            className = className.replace('/', '.');
            String key = keyForTrace(className, owner, methodName, methodDesc);

            List<AdviceListener> listeners = map.get(key);
            if (listeners == null) {
                listeners = new ArrayList<AdviceListener>();
                map.put(key, listeners);
            }
            if (!listeners.contains(listener)) {
                listeners.add(listener);
            }
        }

其它修改该List的地方基本都加了锁,不知道这里是不是疏忽了?如果没有锁保护的话,这里的map.put也是存在并发问题的。
假如两个线程都执行到这里,都new了一个ArrayList来添加AdviceListener,然后两个线程都执行put操作,那么先put的List便被替换掉了,然后继续往此List添加AdviceListener便无意义了,便丢失了,这里可以使用putIfAbsent,或computeIfAbsent来保证原子性,类似的map操作还有十来处,个别的有锁保护,有没则没有

看要不要我按此思路来修复一下 @hengyunabc

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

3 participants