Hey, i have this really weird behaviour in a ktor ...
# ktor
m
Hey, i have this really weird behaviour in a ktor app where the routing/Application context does not seem to access the same static map as the main context. The printouts below points to two different maps. 🤔
Copy code
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.coroutines.delay

import java.util.concurrent.ConcurrentHashMap

private val testMap = ConcurrentHashMap<String, String>()
private var counter = 1
suspend fun main() {
    testMap["MS"] = "1"

    println("testMap from main setup: $testMap")
    embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::myTestApp).start(wait = false)

    while (true) {
        testMap["M${counter++}"] = "1"
        println("testMap from Main: $testMap")
        delay(3000)
    }
}

fun Application.myTestApp() {
    testMap["RS"] = "1"
    println("testMap from routing setup: $testMap")
    routing {
        get("/") {
            testMap["R" + counter++] = "1"
            println("testMap from routing: $testMap")
            call.respond(testMap)
        }
    }
}
Gives the output:
Copy code
testMap from main setup: {MS=1}
testMap from routing setup: {RS=1}
13:04:51.102 │             main INFO  ktor.application          │ Application started in 0.775 seconds.
13:04:51.284 │             main INFO  ktor.application          │ Responding at <http://0.0.0.0:8080>
testMap from Main: {M1=1, MS=1}
testMap from Main: {M1=1, M2=1, MS=1}
testMap from routing: {RS=1, R1=1}
testMap from routing: {R2=1, RS=1, R1=1}
testMap from routing: {R2=1, RS=1, R3=1, R1=1}
testMap from Main: {M1=1, M2=1, MS=1, M3=1}
When my colleague runs the same code it does not show the same issue. For him the code adds entries in the same map. Running Mac M2 Pro with java 21 Corretto and Kotlin 2.0.21, Ktor: 2.3.11 How can this be and how do i fix it?
changed to ktor 3.0.1 and java 17 and still have the same behaviour
a
Can you try disabling the development mode if it's enabled?
m
I dont think i have development mode turned on. Im running it in intellij. Is there any settings for that in intellij? Here is my gradle:
Copy code
val ktor_version: String by project
plugins {
    application
    kotlin("jvm") version "2.0.21"
    id("org.jetbrains.kotlin.plugin.serialization") version "1.9.23"
}

group = "se.svt"
version = "0.0.1"

application {
    mainClass.set("se.svt.videocollab.gissa.TestAppKt")
}

val javaVersion = 21
java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(javaVersion))
    }
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlin {
        jvmToolchain {
            (this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of(javaVersion))
        }
    }
}

tasks {
    compileKotlin {
        kotlinOptions.jvmTarget = "$javaVersion"
    }
}

repositories {
    mavenCentral()
}

val jackson = "2.13.2"
dependencies {
    implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
    implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
    implementation("io.ktor:ktor-client-core:$ktor_version")
    implementation("io.ktor:ktor-client-cio:$ktor_version")
    implementation("io.ktor:ktor-server-cors:$ktor_version")
    implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
    implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
    implementation("io.ktor:ktor-client:$ktor_version")
    implementation("io.ktor:ktor-client-cio-jvm:2.3.11")
    implementation("org.bytedeco:javacv-platform:1.5.10")
    implementation("org.bytedeco:javacpp-platform:1.5.10")
    implementation("org.bytedeco:opencv-platform:4.9.0-1.5.10")
    implementation("org.bytedeco:ffmpeg-platform-gpl:6.1.1-1.5.10")
    implementation("org.bytedeco:openblas-platform:0.3.26-1.5.10")
    implementation("io.github.crackthecodeabhi:kreds:0.9.1")
    testImplementation(kotlin("test"))

    implementation("org.slf4j:jul-to-slf4j:2.0.16")

    implementation("ch.qos.logback:logback-classic") {
        version {
            strictly("1.5.8")
        }
    }
    implementation("net.logstash.logback:logstash-logback-encoder:7.0.1")
    implementation("ch.qos.logback.contrib:logback-json-classic:0.1.5")
    implementation("ch.qos.logback.contrib:logback-jackson:0.1.5")

    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jackson")
    implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jackson")
    implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson")
    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-smile:$jackson")
    implementation("org.apache.kafka:kafka-clients:3.4.0")
    implementation("com.google.guava:guava:31.1-jre")
}

tasks.test {
    useJUnitPlatform()
}
kotlin {
    jvmToolchain(21)
}
a
If you use a Ktor run configuration, there is an option to toggle the development mode.
m
Ah nice. Turning that off solved it. Thanks!
a
This is a long-standing issue, unfortunately.
m
Ok 😞 Thanks alot for your help anyway. Ill put a vote on that issue as well. I would imagine that this makes the development mode completely broken for a lot of people
👌 1
Ktor development mode is turned on by default when running in intellij? If i delete the run config and press play again on the main function its enabled again 🤦
Yeah it seems they automatically enable the development mode in IntelliJ IDEA 2024.2. This makes this a even bigger issue. Made a comment about it in the issue 👍