Skip to content

Ktor: application stop releases what the plugin registered (global context, stop subscription) - #2494

Open
lidonis wants to merge 1 commit into
InsertKoinIO:4.3.0from
lidonis:ktor-stop-lifecycle-4.3
Open

lidonis wants to merge 1 commit into
InsertKoinIO:4.3.0from
lidonis:ktor-stop-lifecycle-4.3

Conversation

@lidonis

@lidonis lidonis commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

What & why

No GitHub issue. Targets 4.3.0.

setupMonitoring (shared by Koin and KoinIsolated) subscribes to ApplicationStopping on application.monitor. On an embedded server that monitor is the server's, shared by every Application across auto-reloads. Two problems follow:

  1. The global context is never freed on stop. The handler calls koin.close() only, so GlobalContext keeps the closed Koin and a later startKoin throws KoinApplicationAlreadyStartedException. This bites tests and any process that stops a server and starts another.
  2. The stop subscription is never released. It stays on the server monitor for the server's lifetime, keeps the stopped Application and its Koin reachable, and runs again on every later ApplicationStopping, so KoinApplicationStopPreparing and KoinApplicationStopped are raised once per reload for an application that stopped long ago.

Measured with a throwaway probe (not in this PR) on 4.3.0 against Ktor 3.4.0, 3.5.2 and 3.6.1: after 5 reloads and a GC, 5 of 5 previous Application instances were still reachable with the plugin installed, 0 of 5 in the control without it. With the handler change the count is 0 of 5 on the three versions.

The fix, in KoinPlugin.kt: the handler ignores a stop raised for another application, disposes its own subscription, then calls stopKoin() when the global context still holds this Koin and koin.close() otherwise. The === check keeps two cases correct: KoinIsolated never owns the global context, and a reload that starts the next application first has already replaced it there.

Public API

  • ./gradlew apiCheck passes (:ktor:koin-ktor:apiCheck)
  • No public API change
  • Removals / signature changes: not applicable

Behavior change

Nothing under core/resolution/. In koin-ktor:

  • Old: ApplicationStopping for any application on the monitor closed the Koin; the global context kept the closed instance; the handler stayed subscribed.
  • New: only a stop of the plugin's own application is handled, once; the global context is freed when it holds this Koin, so startKoin works again after the application stops.

Tests

  • Bug fix: regression tests that fail without the fix. stopping the application frees the global context and stop subscription is released once the application has stopped in KoinFeatureTest both failed before the change.
  • New behavior: two guard tests for the === check, stopping the previous application keeps the Koin of the one that replaced it and stopping an isolated application keeps the global Koin. Both fail if the handler always calls stopKoin().
  • commonMain touched, tests in jvmTest: koin-ktor has no commonTest source set and its test host dependency is declared for jvmTest only, so the tests sit with the existing plugin tests.

Verified beyond the JVM (for commonMain changes)

  • Native target + wasmJs compile: ./gradlew :ktor:koin-ktor:compileKotlinMacosArm64 :ktor:koin-ktor:compileKotlinWasmJs :ktor:koin-ktor:compileKotlinJs pass. allTests not run. :ktor:koin-ktor:jvmTest: 36 tests, 2 ignored, 0 failures, on the catalog's Ktor 3.4.0.

Docs

  • docs/ updated in this PR: docs/reference/koin-ktor/ktor.md states what Shutdown now does, and adds a note on a case this PR does not fix. On Ktor 3.5.0 to 3.5.2, auto-reload builds the new application before stopping the previous one; if that reload fails after install(Koin), the new application has already stopped the Koin the still-serving application uses (KoinPlugin.kt, the stopKoin() "for ktor auto-reload"). Ktor 3.6 stops the previous application first, and KoinIsolated is not affected. A code fix would need a way to detach a Koin from the global context without closing it, which KoinContext does not have, so it is documented only.

🤖 Generated with Claude Code

…plication stop

The Koin plugin subscribed to ApplicationStopping on the server's monitor and never disposed
the subscription. The monitor outlives every application of a reloading server, so each reload
kept the previous Application and Koin reachable. The handler now disposes its own subscription
when its application stops.

The plugin closed Koin on stop but left it registered in the global context, so a startKoin
after a server shutdown threw KoinApplicationAlreadyStartedException. The handler now calls
stopKoin() when the global context still holds its Koin, and only closes it otherwise
(KoinIsolated, or a Koin that a Ktor 3.5 reload has already replaced).

Behavior, old vs new: once the application has stopped, the global context is empty instead of
holding a closed Koin. No public API change.

Docs: the shutdown line of the Ktor page, plus a note on Ktor 3.5.0 to 3.5.2, where a failed
auto-reload leaves the previous application serving with a Koin the new one already stopped.
Not changed in code: Ktor 3.6 stops the previous application first.

Tests: four in KoinFeatureTest (jvmTest, they need the Ktor test host). Two fail without the
change (global context still registered, stop handler running twice). Two guard the global
context check and fail if the handler always calls stopKoin() (isolated application, previous
application stopping after its replacement started).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant