fanpipi opened a new issue, #6286:
URL: https://github.com/apache/shenyu/issues/6286
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Current Behavior
GsonUtils中TimestampTypeAdapter使用非线程安全的SimpleDateFormat导致并发序列化异常。
When using GsonUtils for concurrent serialization operations involving
classes with Timestamp fields, exceptions like the following may occur:
在使用 GsonUtils 进行并发序列化操作时,如果涉及包含 Timestamp 类型的类,可能会抛出类似以下异常:
`java.lang.NumberFormatException: For input string: "502.E5022E2"
`
`java.lang.NumberFormatException: multiple points
`
### Expected Behavior
After investigation, the root cause was found to be the use of the
non-thread-safe SimpleDateFormat in GsonUtils.TimestampTypeAdapter. In
high-concurrency scenarios, multiple threads accessing and modifying the same
SimpleDateFormat instance can lead to race conditions, resulting in the above
exceptions.
Suggested Fix:
1. Replace SimpleDateFormat with a thread-safe alternative, such as:
- Using DateTimeFormatter (recommended)
- Or creating a new SimpleDateFormat instance each time (poor performance,
not recommended)
2. Update the implementation of TimestampTypeAdapter to ensure thread safety.
I would like to contribute a fix for this issue and submit a PR. Please
confirm if this approach is acceptable. Thank you!
### Steps To Reproduce
RUN
```jsx
public` static void main(String[] args) {
String source =
"{\"groupType\":\"DISCOVER_UPSTREAM\",\"eventType\":\"UPDATE\",\"data\":[{\"selectorId\":\"2013139628408594432\",\"pluginName\":\"divide\",\"selectorName\":\"test\",\"upstreamDataList\":[{\"id\":\"2018848401582067712\",\"dateCreated\":\"2026-02-04
00:45:41\",\"dateUpdated\":\"2026-02-04
00:45:41\",\"discoveryHandlerId\":\"2013139629532667904\",\"protocol\":\"http://\",\"url\":\"127.0.0.1:8080\",\"status\":0,\"weight\":1,\"namespaceId\":\"649330b6-c2d7-4edc-be8e-8a54df9eb385\"}],\"namespaceId\":\"649330b6-c2d7-4edc-be8e-8a54df9eb385\"}]}";
WebsocketData<?> websocketData =
GsonUtils.getInstance().fromJson(source, WebsocketData.class);
String json =
GsonUtils.getInstance().toJson(websocketData.getData());
System.out.println(json);
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 100; i++) {
final int taskId = i;
executorService.submit(() -> {
try {
final List<DiscoverySyncData> discoverySyncData =
GsonUtils.getInstance().fromList(json, DiscoverySyncData.class);
System.out.println("Task " + taskId + ": " +
discoverySyncData);
} catch (Exception e) {
// 打印异常日志
System.out.println("Task failed with exception: " + e);
}
});
}
executorService.shutdown();
}
```
### Environment
```markdown
ShenYu version(s): master
```
### Debug logs
_No response_
### Anything else?
_No response_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]