Skip to content

Commit

Permalink
KTOR-8045 Fix for NPE race condition in dispose (#4597)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjhham authored Jan 14, 2025
1 parent 18c18fe commit 14e92ae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal class MemoryCache(
override fun dispose() {
GlobalScope.launch {
reader.discard()
fullBody?.discard()
}
fullBody?.discard()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
package io.ktor.server.http

import io.ktor.client.request.*
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.*
import io.ktor.server.plugins.doublereceive.*
import io.ktor.server.plugins.statuspages.StatusPages
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import kotlin.test.*

class ApplicationRequestContentTest {
Expand Down Expand Up @@ -41,4 +48,35 @@ class ApplicationRequestContentTest {
setBody("bodyContent")
}
}

@Test
fun testDoubleReceiveRaceCondition() = runTest {
(1..100).map {
launch {
testApplication {
application {
install(DoubleReceive) {}

install(StatusPages) {
status(HttpStatusCode.BadRequest) { call, status ->
call.respondText(text = "400: Bad Request", status = status)
}
}
routing {
post("/") {
val request = call.receiveText()
call.respond(HttpStatusCode.BadRequest, request)
}
}
}

client.post("/") {
setBody("Hello World")
}.also {
assertEquals(HttpStatusCode.BadRequest, it.status)
}
}
}
}.joinAll()
}
}

0 comments on commit 14e92ae

Please sign in to comment.