Title: Spring Boot 2: GetMapping not thread-safe
Tags: spring;spring-boot;thread-safety
Question: I have a RestController with the following function:
@GetMapping(&quot;/filter&quot;)
fun getProjects(
@RequestParam q: String,
): Page<ProjectSummaryView&gt; {
return projectSummaryViewRepository
.findAll(
ProjectSummaryViewSpecification(projectFilter),
PageRequest.of(projectFilter.page ?: 0, projectFilter.size ?: 0)
).map { v -&gt; mapChatRoomDto(v, invocationContext) }
The problem is that when I am sending concurrent requests with different query parameter, it looks like that the resolver assigning requestparam is not thread-safe. The parameter &quot;query&quot; get mixed up. If I inject WebRequest into the function, we can clearly see that query doesn't match webRequest.getParameter()
More information: If I do this, and hammer the the request, I will at some point get that q!=q2, that indicates that it is not threadsafe.
@JsonView(Publi75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0class)
@PreAuthorize(&quot;@sec.hasProjectPermission(#projectFilter)&quot;)
@GetMapping(&quot;/summary&quot;)
fun getProjectsSummaries(
@RequestParam q: String,
webRequest: WebRequest,
invocationContext: InvocationContext
): Page<ProjectSummaryView&gt; {
val cId = getCustomerId(query)
val param = webRequest.parameterMap
val q2 = webRequest.parameterMap.get(&quot;q&quot;)?.get(0)
if (q != q2) {
System.out.println(q)
System.out.println(q2)
}
return projectSummaryViewRepository
.findAll(
ProjectSummaryViewSpecification(q),
PageRequest.of(0, 20)
).map { v -&gt; mapChatRoomDto(v, invocationContext) }
Comment: The method call chain itself is threadsafe bacause it has an own stack. Please tell us what does it mean "inject WebRequest into the function". Do you use class level variables? etc. Enchance your question with the sourcecode.

Comment: No class level variables. I get a mismatch between the native webrequest query parameter and the binded @RequestParam.

Comment: Actually, we don't have any common state. I made a quick testcase, and forgot to change projectFilter.
I'm testing it by using the browser, sending multiple requests. I find it weird that if I send a lot of concurrent requests, q!= q2.

Comment: Hi, never mind. I solved it. Turned out that Spring is threadsafe, but we had a bean that weren't.  By deleting this code, it became threadsafe.
public WebBindingInitializer webBindingInitializer() {
ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
bindingInitializer.setPropertyEditorRegistrar(this);
return bindingInitializer;

Comment: How are you calling this, so by which test method? Each method call has its own stack and if it is through a http request its own handling thread. So unless you are keeping state in your controller (at least the `projectFilter` looks like state?) it is perfectly thread-safe and I suspect the error being on the test side. Finally it would be good to know which server you are running this on and how things are being loadtested.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:06/23/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<spring><spring-boot><thread-safety>', 'Title': 'Spring Boot 2: GetMapping not thread-safe', 'ViewCount': 90, 'id': 62543306, 'user_AboutMe': None, 'user_AccountId': 1660592.0, 'user_DisplayName': 'Jing Kjeldsen', 'user_Reputation': 13.0, 'user_Views': 9.0, 'user_id': 4312407.0}}