Skip to content

fix(loader): fix thread-safety issue in DateUtil with concurrent date parsing#734

Open
SYaoJun wants to merge 2 commits intoapache:masterfrom
SYaoJun:fix_dateUtil
Open

fix(loader): fix thread-safety issue in DateUtil with concurrent date parsing#734
SYaoJun wants to merge 2 commits intoapache:masterfrom
SYaoJun:fix_dateUtil

Conversation

@SYaoJun
Copy link
Copy Markdown
Contributor

@SYaoJun SYaoJun commented May 5, 2026

Purpose of the PR

Main Changes

Replace ConcurrentHashMap with ThreadLocal so each thread has its
own isolated SafeDateFormat instances, eliminating race conditions.

Ensure DateUtil.now() explicitly sets the default timezone before
formatting to avoid timezone pollution from previous parse() calls.

Verifying these changes

  • Trivial rework / code cleanup without any test coverage. (No Need)
  • Already covered by existing tests, such as (please modify tests here).
  • Need tests and can be verified as follows:
    • Dtest=DateUtilTest

Does this PR potentially affect the following parts?

  • Nope
  • Dependencies (add/update license info)
  • Modify configurations
  • The public API
  • Other affects (typed here)

Documentation Status

  • Doc - TODO
  • Doc - Done
  • Doc - No Need

Signed-off-by: Jason <libevent@yeah.net>
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label May 5, 2026
@github-actions github-actions Bot added the loader hugegraph-loader label May 5, 2026
@dosubot dosubot Bot added the bug Something isn't working label May 5, 2026
@imbajin imbajin requested a review from Copilot May 5, 2026 03:32
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses the thread-safety issue in hugegraph-loader’s DateUtil by avoiding shared SafeDateFormat instances across threads and by ensuring now() resets the formatter timezone before formatting, preventing timezone state leakage from prior parses.

Changes:

  • Replace the shared ConcurrentHashMap cache of SafeDateFormat with a ThreadLocal<HashMap<...>> cache (per-thread formatter instances).
  • Update DateUtil.now() to explicitly set Constants.TIME_ZONE before formatting.
  • Add unit tests intended to cover timezone behavior and concurrent parsing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DateUtil.java Switch formatter caching to per-thread and explicitly reset timezone in now()
hugegraph-loader/src/test/java/org/apache/hugegraph/loader/test/unit/DateUtilTest.java Add tests for now() timezone behavior and concurrent parsing

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

public void testNowUsesDefaultTimeZone() {
String pattern = "Z";

DateUtil.parse("1970-01-01 +0000", "yyyy-MM-dd Z", "GMT");
Comment on lines +102 to +110
executor.submit(() -> {
try {
String timeZone = threadId % 2 == 0 ? "GMT+8" : "GMT+0";
for (int j = 0; j < iterations; j++) {
Date result = DateUtil.parse(dateStr, pattern, timeZone);
if (result == null) {
errors.incrementAndGet();
}
}
Comment on lines 28 to 30
private static final ThreadLocal<java.util.HashMap<String, SafeDateFormat>> DATE_FORMATS =
ThreadLocal.withInitial(java.util.HashMap::new);

Comment thread hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DateUtil.java Outdated
@SYaoJun SYaoJun requested a review from imbajin May 5, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working loader hugegraph-loader size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] DateUtil uses shared SimpleDateFormat instances causing thread-safety issues in concurrent date parsing

3 participants