Skip to content

Replace thread-unsafe SimpleDateFormat with DateTimeFormatter in AbstractMockHttpServletRequestBuilder#36749

Closed
SebTardif wants to merge 1 commit intospring-projects:mainfrom
SebTardif:fix/thread-unsafe-simpledateformat-mock-request
Closed

Replace thread-unsafe SimpleDateFormat with DateTimeFormatter in AbstractMockHttpServletRequestBuilder#36749
SebTardif wants to merge 1 commit intospring-projects:mainfrom
SebTardif:fix/thread-unsafe-simpledateformat-mock-request

Conversation

@SebTardif
Copy link
Copy Markdown

Summary

Replace the static SimpleDateFormat instance with a thread-safe DateTimeFormatter in AbstractMockHttpServletRequestBuilder.

Problem

A static final SimpleDateFormat is shared across all test request builder instances. SimpleDateFormat.format() mutates internal Calendar state and is not thread-safe. When tests run in parallel (e.g., with JUnit's @Execution(CONCURRENT)), concurrent calls to header(name, dateValue) can produce corrupt date strings or throw ArrayIndexOutOfBoundsException.

Fix

Replace with DateTimeFormatter.ofPattern(...) using the same pattern (EEE, dd MMM yyyy HH:mm:ss zzz), locale (US), and timezone (UTC). DateTimeFormatter is immutable and thread-safe by design.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 3, 2026
The static SimpleDateFormat instance in
AbstractMockHttpServletRequestBuilder is shared across all
instances. SimpleDateFormat.format() mutates internal Calendar
state and is not thread-safe, which can produce corrupt date
strings or ArrayIndexOutOfBoundsException when tests run in
parallel.

Replace with DateTimeFormatter which is immutable and thread-safe.

Signed-off-by: Sebastien Tardif <SebTardif@ncf.ca>
@SebTardif SebTardif force-pushed the fix/thread-unsafe-simpledateformat-mock-request branch from 63a14f6 to a8bf3c1 Compare May 3, 2026 15:41
@SebTardif SebTardif closed this May 9, 2026
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants