Skip to content

Ktor 3.5.x auto-reload closes newly initialized Koin context #2461

Description

@jg-hot

Ktor 3.5.x auto-reload closes newly initialized Koin context

Describe the bug

koin-ktor appears to be incompatible with the create-then-swap auto-reload pattern introduced in Ktor v3.5.x via PR #5557. When the Ktor application is reloaded, the newly created Koin context is subsequently closed when the previous application is stopped.

To Reproduce

  • Create a new project with ktor new and add the Koin plugin
  • Set the Ktor version to 3.5.2 in settings.gradle.kts
  • Set development: true in application.yaml
  • Add the sample code below to Routing.kt
  • Run ./gradlew run
  • Make a code change (e.g. add a new line in Routing.kt)
  • In a separate terminal, run ./gradlew build
  • Access localhost:8080 to trigger the Ktor auto-reload

Snippet or Sample project to help reproduce

class Resource : AutoCloseable {
    init {
        println("INIT resource -> ${System.identityHashCode(this)}")
    }

    override fun close() {
        println("CLOSE resource -> ${System.identityHashCode(this)}")
    }
}

fun Application.configureRouting() {
    log.info("Init application ${System.identityHashCode(this)}")

    install(Koin) {
        slf4jLogger()

        val module = module {
            single { Resource() } onClose { it?.close() }
        }

        modules(module)
    }

    val resource = koin().get<Resource>()

    monitor.subscribe(ApplicationStopping) { appForEvent ->
        log.info(
            "Application stopping: " +
                "${System.identityHashCode(this)} -> " +
                "${System.identityHashCode(appForEvent)}"
        )
    }

    routing {
        get("/") {
            call.respondText("koin / ktor example")
        }
    }
}

Actual behavior

The logs below show that the newly created resource is closed after the auto-reload has completed:

2026-08-11 11:41:36.746 [main] INFO  Application - Init application 1130160902
2026-08-11 11:41:36.813 [main] INFO  [Koin] - Started 1 definitions in 1.728 ms
INIT resource -> 173440626
2026-08-11 11:41:36.831 [main] INFO  Application - Application started in 0.357 seconds.
2026-08-11 11:41:36.951 [main] INFO  Application - Responding at http://0.0.0.0:8080

2026-08-11 11:42:33.488 [eventLoopGroupProxy-3-1] INFO  Application - Changes in application detected.
2026-08-11 11:42:33.710 [eventLoopGroupProxy-3-1] INFO  Application - Init application 1065226841
2026-08-11 11:42:33.712 [eventLoopGroupProxy-3-1] INFO  [Koin] - Started 1 definitions in 0.054 ms
CLOSE resource -> 173440626
INIT resource -> 770784708
2026-08-11 11:42:33.714 [eventLoopGroupProxy-3-1] INFO  Application - Application auto-reloaded in 0.008 seconds.
2026-08-11 11:42:33.716 [eventLoopGroupProxy-3-1] INFO  Application - Application stopping: 1130160902 -> 1130160902
CLOSE resource -> 770784708
2026-08-11 11:42:33.716 [eventLoopGroupProxy-3-1] INFO  Application - Application stopping: 1065226841 -> 1130160902

In particular, the newly created application has identity 1065226841, while the ApplicationStopping event is for the previous application with identity 1130160902:

Application stopping: 1065226841 -> 1130160902

The Resource belonging to the newly created application (770784708) is then closed.

Expected behavior

The Koin context associated with the newly created application should remain active after auto-reload. Only the Koin context associated with the previous application should be closed.

The issue does not reproduce with Ktor 3.4.3. With the same sample application and koin-ktor:4.2.2, the previous resource is closed before the new application is initialized:

2026-08-11 11:36:06.464 [main] INFO  Application - Init application 1186657657
2026-08-11 11:36:06.590 [main] INFO  [Koin] - Started 1 definitions in 2.994 ms
INIT resource -> 271422148
2026-08-11 11:36:06.624 [main] INFO  Application - Application started in 0.703 seconds.
2026-08-11 11:36:06.855 [main] INFO  Application - Responding at http://0.0.0.0:8080

2026-08-11 11:36:34.309 [eventLoopGroupProxy-3-1] INFO  Application - Changes in application detected.
CLOSE resource -> 271422148
2026-08-11 11:36:34.512 [eventLoopGroupProxy-3-1] INFO  Application - Application stopping: 1186657657 -> 1186657657
2026-08-11 11:36:34.545 [eventLoopGroupProxy-3-1] INFO  Application - Init application 1370625743
2026-08-11 11:36:34.547 [eventLoopGroupProxy-3-1] INFO  [Koin] - Started 1 definitions in 0.115 ms
INIT resource -> 1534899786
2026-08-11 11:36:34.549 [eventLoopGroupProxy-3-1] INFO  Application - Application auto-reloaded in 0.016 seconds.

Versions

  • koin-ktor: 4.2.2
  • ktor: 3.5.2

The issue appears to be related to the Ktor 3.5.x auto-reload lifecycle change introduced by PR #5557.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions