com.dianping.cat.message.spi.codec.NativeMessageCodec 的 encodeMessage 可能发生 ConcurrentModificationException 异常

8wtpewkr  于 4个月前  发布在  其他
关注(0)|答案(2)|浏览(45)

Describe the bug
com.dianping.cat.message.spi.codec.NativeMessageCodec 的
encodeMessage 可能发生 ConcurrentModificationException 异常

private void encodeMessage(Context ctx, ByteBuf buf, Message msg) {
		if (msg instanceof Transaction) {
			Transaction transaction = (Transaction) msg;
			List<Message> children = transaction.getChildren();
			for (Message child : children) {
				if (child != null) {
					encodeMessage(ctx, buf, child);
				}
			}
		}
               ...
	}

children 数组大小可能发生变化,当children数组发生变化时会报出 ConcurrentModificationException 异常

Smartphone (please complete the following information):

  • Version 3.0.0
hgc7kmma

hgc7kmma1#

你好,目前看3.1.0版本的写法已经进行了调整,可以考虑升级

uklbhaso

uklbhaso2#

解决方案

private void encodeMessage(Context ctx, ByteBuf buf, Message msg) {
        if (msg instanceof Transaction) {
            Transaction transaction = (Transaction) msg;
            List<Message> children = transaction.getChildren();

            Codec.TRANSACTION_START.encode(ctx, buf, msg);

            // 异步场景下children 可能动态变化
            try {
                Iterator<Message> childIterator = children.iterator();
                while (childIterator.hasNext()) {
                    Message child = childIterator.next();
                    if (child != null) {
                        encodeMessage(ctx, buf, child);
                    }
                }
            } catch (java.util.ConcurrentModificationException e) {
                logger.warn("{} {}", "[CAT]", "sys_excp", e);
            }

            Codec.TRANSACTION_END.encode(ctx, buf, msg);
        } 
}

相关问题