Skip to content

Replace thread-unsafe SimpleDateFormat with DateTimeFormatter in AbstractMockHttpServletRequestBuilder - #36772

Merged
bclozel merged 1 commit into
spring-projects:7.0.xfrom
SebTardif:fix/thread-unsafe-simpledateformat-mock-request
Sep 4, 2026
Merged

bclozel merged 1 commit into
spring-projects:7.0.xfrom
SebTardif:fix/thread-unsafe-simpledateformat-mock-request

Conversation

@SebTardif

Copy link
Copy Markdown
Contributor

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 10, 2026
@bclozel bclozel self-assigned this Sep 4, 2026
@bclozel bclozel added in: test Issues in the test module type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 4, 2026
@bclozel
bclozel changed the base branch from main to 7.0.x September 4, 2026 13:00
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>
@bclozel
bclozel force-pushed the fix/thread-unsafe-simpledateformat-mock-request branch from a8bf3c1 to e0144da Compare September 4, 2026 13:11
@bclozel
bclozel merged commit e0144da into spring-projects:7.0.x Sep 4, 2026
1 check passed
@bclozel

bclozel commented Sep 4, 2026

Copy link
Copy Markdown
Member

Thanks @SebTardif !

@bclozel bclozel added this to the 7.0.10 milestone Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: test Issues in the test module type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants