-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove call to requestCompleted in RequestMappingHandlerAdapter #29002
Comments
This seems to have been added as part of 698f923 but appears to be completely unrelated to the change, and other usages of this method are in more global places like In terms of async requests, Do you have a specific issue that you are experiencing? Aside from that we should probably remove this call from |
@rstoyanchev I want to use RequestScope bean in a coroutine. For this, I created a SpringContext that restores the value in ThreadLocal when the Thread is changed. But ScopeNotActiveException was occured.
package com.example.coroutinemvctest.controller
import kotlinx.coroutines.ThreadContextElement
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.context.annotation.RequestScope
import org.springframework.web.context.request.AbstractRequestAttributes
import org.springframework.web.context.request.RequestAttributes
import org.springframework.web.context.request.RequestContextHolder
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext
@RestController
class TestController(
private val testService: TestService,
) {
@GetMapping("/hello")
suspend fun hello(): String = withContext(SpringContext()){
delay(100)
return@withContext testService.id.toString()
}
}
private var counter = 0
@RequestScope
@Service
class TestService {
val id: Int = ++counter
}
class SpringContext(
private val requestAttributes: RequestAttributes? = RequestContextHolder.getRequestAttributes(),
) : ThreadContextElement<RequestAttributes?>, AbstractCoroutineContextElement(Key) {
companion object {
object Key : CoroutineContext.Key<SpringContext>
private val requestActiveField = AbstractRequestAttributes::class.java.getDeclaredField("requestActive").apply {
isAccessible = true
}
}
override fun updateThreadContext(context: CoroutineContext): RequestAttributes? {
val oldState = RequestContextHolder.getRequestAttributes()
requestAttributes?.let {
RequestContextHolder.setRequestAttributes(it)
// requestActiveField.set(it, true) // if uncomment this line, then ScopeNotActiveException not occurs
}
return oldState
}
override fun restoreThreadContext(context: CoroutineContext, oldState: RequestAttributes?) {
RequestContextHolder.setRequestAttributes(oldState)
}
} |
I'll go ahead and remove the call to That said, the same call will be still be made a little later in Generally, request scoped beans rely on |
I have slove this problem,details please see Cannot ask for request attribute - request is not active anymore! |
RequestMappingHandlerAdapter
callswebRequest.requestCompleted();
even though the async request has not completed.Is that intentional, or is it a bug?
spring-framework/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java
Line 906 in dd3e3b2
The text was updated successfully, but these errors were encountered: