dosubot[bot] commented on issue #733: URL: https://github.com/apache/hugegraph-toolchain/issues/733#issuecomment-4375833973
<!-- Greeting --> Hi @SYaoJun! I'm [Dosu](https://go.dosu.dev/dosubot) and Iām helping the hugegraph-toolchain team. <!-- Answer --> You're correct that there's a thread-safety issue here, though with a nuance worth noting. `DateUtil` doesn't actually use `SimpleDateFormat` directly ā it uses `SafeDateFormat`, a wrapper around Joda-Time's `DateTimeFormatter` [[1]](https://github.com/apache/hugegraph/blob/9126c80e414c073b60fc074361b211cfb409e0eb/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/date/SafeDateFormat.java). However, the race condition you identified is still valid. The problem is in the `parse()` method [[2]](https://github.com/apache/hugegraph-toolchain/blob/bc49e6683fcc8f00e32d290cd558a0320c1850a3/hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DateUtil.java#L35-L38): ```java public static Date parse(String source, String df, String timeZone) { SafeDateFormat dateFormat = getDateFormat(df); dateFormat.setTimeZone(timeZone); return dateFormat.parse(source); } ``` `SafeDateFormat` instances are cached in a shared `ConcurrentHashMap` keyed by format pattern [[3]](https://github.com/apache/hugegraph-toolchain/blob/bc49e6683fcc8f00e32d290cd558a0320c1850a3/hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DateUtil.java#L28). While `setTimeZone()` is individually synchronized in `SafeDateFormat` [[4]](https://github.com/apache/hugegraph/blob/9126c80e414c073b60fc074361b211cfb409e0eb/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/date/SafeDateFormat.java#L42-L47), the `setTimeZone()` + `parse()` sequence is **not atomic**. This means exactly the scenario you described can occur ā Thread B can change the timezone between Thread A's `setTimeZone()` and `parse()` calls. The `now()` method has a related issue: it uses whatever timezone was last set on the cached formatter, which is non-deterministic in a concurrent context [[5]](https://github.com/apache/hugegraph-toolchain/blob/bc49e6683fcc8f00e32d290cd558a0320c1850a3/hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DateUtil.java#L49-L51). A fix could involve either: 1. Synchronizing the entire `setTimeZone()` + `parse()`/`format()` operation on the `SafeDateFormat` instance 2. Using `ThreadLocal<SafeDateFormat>` instances instead of a shared cache 3. Creating a new formatter per call when a non-default timezone is specified <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fhugegraph-toolchain).* --- Docs are dead. Just use [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-tagline&utm_term=apache%2Fhugegraph-toolchain). [](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-feedback&utm_term=apache%2Fhugegraph-toolchain&message_id=c3946a63-4107-43fb-b2ef-2fab1e8d914d) [](https://github.dosu.com/apache/hugegraph-toolchain?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-ask-repo&utm_term=apache%2Fhugegraph-toolchain) [](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-share-team&utm_term=apache%2Fhugegraph-toolchain) -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
