cat
cat copied to clipboard
com.dianping.cat.message.spi.codec.NativeMessageCodec 的 encodeMessage 可能发生 ConcurrentModificationException 异常
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
解决方案
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);
}
}
你好,目前看3.1.0版本的写法已经进行了调整,可以考虑升级