Skip to content

Commit

Permalink
KTOR-7963 Handle ClosedWatchServiceException
Browse files Browse the repository at this point in the history
  • Loading branch information
bjhham committed Dec 19, 2024
1 parent 23eee5a commit 9cef176
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ package io.ktor.tests.server.cio
import io.ktor.server.cio.*
import io.ktor.server.engine.*
import io.ktor.server.testing.suites.*
import kotlin.system.*
import kotlin.test.*
import kotlin.system.measureTimeMillis
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertTrue

class CIOCompressionTest : CompressionTestSuite<CIOApplicationEngine, CIOApplicationEngine.Configuration>(CIO) {
init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,10 @@ actual constructor(
return@read currentApplication
}

val changes = packageWatchKeys.flatMap { it.pollEvents() }
if (changes.isEmpty()) {
if (getFileChanges().isNullOrEmpty()) {
return@read currentApplication
}

environment.log.info("Changes in application detected.")

var count = changes.size
while (true) {
Thread.sleep(200)
val moreChanges = packageWatchKeys.flatMap { it.pollEvents() }
if (moreChanges.isEmpty()) {
break
}

environment.log.debug("Waiting for more changes.")
count += moreChanges.size
}

environment.log.debug("Changes to $count files caused application restart.")
changes.take(5).forEach { environment.log.debug("... ${it.context()}") }

applicationInstanceLock.write {
destroyApplication()
val (application, classLoader) = createApplication()
Expand All @@ -132,6 +114,39 @@ actual constructor(
return@read applicationInstance ?: error("EmbeddedServer was stopped")
}

private fun getFileChanges(): List<WatchEvent<*>>? {
try {
val changes = packageWatchKeys.flatMap { it.pollEvents() }
if (changes.isEmpty()) {
return changes
}

environment.log.info("Changes in application detected.")

var count = changes.size
while (true) {
Thread.sleep(200)
val moreChanges = packageWatchKeys.flatMap { it.pollEvents() }
if (moreChanges.isEmpty()) {
break
}

environment.log.debug("Waiting for more changes.")
count += moreChanges.size
}

environment.log.debug("Changes to $count files caused application restart.")
changes.take(5).forEach { environment.log.debug("... {}", it.context()) }
return changes
} catch (e: InterruptedException) {
environment.log.debug("Watch service was interrupted", e)
return null
} catch (e: ClosedWatchServiceException) {
environment.log.debug("Watch service was closed", e)
return null
}
}

private fun createApplication(): Pair<Application, ClassLoader> {
val classLoader = createClassLoader()
val currentThread = Thread.currentThread()
Expand Down Expand Up @@ -383,10 +398,7 @@ actual constructor(
}

private fun cleanupWatcher() {
try {
watcher?.close()
} catch (_: NoClassDefFoundError) {
}
runCatching { watcher?.close() }
}
}

Expand Down

0 comments on commit 9cef176

Please sign in to comment.