Skip to content

[BUG] GsonUtils TimestampTypeAdapter uses non-thread-safe SimpleDateFormat causing concurrent serialization exceptions #6286

@fanpipi

Description

@fanpipi

Is there an existing issue for this?

  • 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)
  1. 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

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

ShenYu version(s): master

Debug logs

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions