fix(loader): fix thread-safety issue in DateUtil with concurrent date parsing#734
Open
SYaoJun wants to merge 2 commits intoapache:masterfrom
Open
fix(loader): fix thread-safety issue in DateUtil with concurrent date parsing#734SYaoJun wants to merge 2 commits intoapache:masterfrom
SYaoJun wants to merge 2 commits intoapache:masterfrom
Conversation
Signed-off-by: Jason <libevent@yeah.net>
There was a problem hiding this comment.
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
ConcurrentHashMapcache ofSafeDateFormatwith aThreadLocal<HashMap<...>>cache (per-thread formatter instances). - Update
DateUtil.now()to explicitly setConstants.TIME_ZONEbefore 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); | ||
|
|
imbajin
reviewed
May 5, 2026
Signed-off-by: Jason <libevent@yeah.net>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Does this PR potentially affect the following parts?
Documentation Status
Doc - TODODoc - DoneDoc - No Need