Repository navigation
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
No GitHub issue. Targets
4.3.0.setupMonitoring(shared byKoinandKoinIsolated) subscribes toApplicationStoppingonapplication.monitor. On an embedded server that monitor is the server's, shared by everyApplicationacross auto-reloads. Two problems follow:koin.close()only, soGlobalContextkeeps the closed Koin and a laterstartKointhrowsKoinApplicationAlreadyStartedException. This bites tests and any process that stops a server and starts another.Applicationand its Koin reachable, and runs again on every laterApplicationStopping, soKoinApplicationStopPreparingandKoinApplicationStoppedare raised once per reload for an application that stopped long ago.Measured with a throwaway probe (not in this PR) on
4.3.0against Ktor 3.4.0, 3.5.2 and 3.6.1: after 5 reloads and a GC, 5 of 5 previousApplicationinstances 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 callsstopKoin()when the global context still holds this Koin andkoin.close()otherwise. The===check keeps two cases correct:KoinIsolatednever owns the global context, and a reload that starts the next application first has already replaced it there.Public API
./gradlew apiCheckpasses (:ktor:koin-ktor:apiCheck)Behavior change
Nothing under
core/resolution/. Inkoin-ktor:ApplicationStoppingfor any application on the monitor closed the Koin; the global context kept the closed instance; the handler stayed subscribed.startKoinworks again after the application stops.Tests
stopping the application frees the global contextandstop subscription is released once the application has stoppedinKoinFeatureTestboth failed before the change.===check,stopping the previous application keeps the Koin of the one that replaced itandstopping an isolated application keeps the global Koin. Both fail if the handler always callsstopKoin().commonMaintouched, tests injvmTest:koin-ktorhas nocommonTestsource set and its test host dependency is declared forjvmTestonly, so the tests sit with the existing plugin tests.Verified beyond the JVM (for
commonMainchanges)wasmJscompile:./gradlew :ktor:koin-ktor:compileKotlinMacosArm64 :ktor:koin-ktor:compileKotlinWasmJs :ktor:koin-ktor:compileKotlinJspass.allTestsnot 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.mdstates 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 afterinstall(Koin), the new application has already stopped the Koin the still-serving application uses (KoinPlugin.kt, thestopKoin()"for ktor auto-reload"). Ktor 3.6 stops the previous application first, andKoinIsolatedis not affected. A code fix would need a way to detach a Koin from the global context without closing it, whichKoinContextdoes not have, so it is documented only.🤖 Generated with Claude Code