[BUG] ConsulSyncDataService uses non-thread-safe HashMap for shared state accessed by 7 concurrent watcher threads
- Dominant language
- Java
- Stars
- 8.8k
- Forks
- 3.1k
- Avg merge
- 7d 1h
- Merged PRs (30d)
- 85
Description
- Severity: Medium
- Location:
`shenyu-sync-data-center/shenyu-sync-data-consul/src/main/java/org/apache/shenyu/sync/data/consul/ConsulSyncDataService.java:63,65` (HashMap fields), `:98` (7-thread `ScheduledThreadPoolExecutor`), `:103-111` (7 scheduled `watcherData0` tasks)
-
Description:
`consulIndexes` (`Map`) and `cacheConsulDataKeyMap` (`Map>`) are plain `HashMap`. The executor has 7 threads and schedules 7 independent `watchConfigKeyValues` tasks (one per watcher path). Each task performs `consulIndexes.get`, `consulIndexes.containsValue` (full-map scan), `consulIndexes.put`, `cacheConsulDataKeyMap.get`, `cacheConsulDataKeyMap.put` on these shared maps. `HashMap` is not safe for concurrent access — structural modifications (put during resize) can corrupt internal structure or lose data; `containsValue` racing with `put` produces inconsistent reads.
-
Impact:
Under normal multi-path consul operation, concurrent access corrupts data, loses index updates (watcher misses config changes or re-processes them), inconsistent gateway cache state.
-
Suggested fix:
Change both to `ConcurrentHashMap`.
-
Confidence: High
- Related existing: none — #5545 (consul client metadata register fails) is a different component/root cause.
---
_Identified during the 2026-08-02 deep re-scan; full list in [`docs/scan2-2026-08-02/06-medium-tiers.md`](docs/scan2-2026-08-02/06-medium-tiers.md)._
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in shenyu-sync-data-center/shenyu-sync-data-consul/src/main/java/org/apache/shenyu/sync/data/consul/ConsulSyncDataService.java, especially the map fields at lines 63-65 and the executor and watcher tasks at lines 98-111. Trace how watcherData0 tasks access both shared maps, then verify that concurrent watcher operation is safe and that index and cached data updates remain consistent. Done means both shared maps no longer use non-thread-safe HashMap.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend, distributed-systems
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100