Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions PR_DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Fix thread-unsafe LinkedHashMap in TimeTunnelCommand

## Problem

`timeFragmentMap` in `TimeTunnelCommand.java` is a plain `LinkedHashMap` accessed concurrently from multiple threads (advice listeners writing time fragments + command thread reading/iterating). This can cause `ConcurrentModificationException`, infinite loops in HashMap bucket chains, or silent data corruption.

## Root Cause

The field is declared as `new LinkedHashMap<Integer, TimeFragment>()` with a TODO comment acknowledging the thread-safety concern (`// TODO 并非线程安全?` — "not thread safe?"). The `AdviceListener` callbacks that populate this map run on instrumented application threads, while command operations (list, search, replay) run on the Arthas command thread.

## Fix

Replaced `new LinkedHashMap<>()` with `Collections.synchronizedMap(new LinkedHashMap<>())` to provide basic thread safety. Added the `java.util.Collections` import.

## Testing

- Run `tt` command while monitoring a high-throughput method with multiple threads calling it simultaneously.
- Previously this could cause `ConcurrentModificationException` during `tt -l` (list) operations; now it should be safe.

## Impact

Affects all Arthas users using the Time Tunnel (`tt`) command on multi-threaded applications. The bug is non-deterministic and depends on timing, making it difficult to reproduce consistently.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -52,7 +53,7 @@
public class TimeTunnelCommand extends EnhancerCommand {
// 时间隧道(时间碎片的集合)
// TODO 并非线程安全?
private static final Map<Integer, TimeFragment> timeFragmentMap = new LinkedHashMap<Integer, TimeFragment>();
private static final Map<Integer, TimeFragment> timeFragmentMap = Collections.synchronizedMap(new LinkedHashMap<Integer, TimeFragment>());
// 时间碎片序列生成器
private static final AtomicInteger sequence = new AtomicInteger(1000);
// TimeTunnel the method call
Expand Down