Skip to content

Koin and KoinApplication implement AutoCloseable - #2491

Open
lidonis wants to merge 3 commits into
InsertKoinIO:4.3.0from
lidonis:koin-autocloseable-4.3
Open

lidonis wants to merge 3 commits into
InsertKoinIO:4.3.0from
lidonis:koin-autocloseable-4.3

Conversation

@lidonis

@lidonis lidonis commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Supersedes #2374 (closed as stale), rebased onto 4.3.0.

Summary

  • Koin and KoinApplication now implement AutoCloseable, enabling Kotlin's use { } pattern for safe resource cleanup
  • close() already existed with the right signature on both classes, so the change is the interface declaration plus override
  • KoinApplication.close() is declared final override, so it stays non-overridable as before (KoinApplication is an open class)
  • koin-core.api is updated: both classes now list java/lang/AutoCloseable as a supertype

Motivation

Koin's Koin and KoinApplication have close() methods but don't implement AutoCloseable, so developers can't use Kotlin's use { } and must write manual try/finally blocks.

With this change:

koinApplication {
    modules(myModule)
}.use { app ->
    val service: MyService = app.koin.get()
    // work with service
}
// app.close() called automatically, even on exceptions

Changes

  • Koin.kt: AutoCloseable interface and override on close()
  • KoinApplication.kt: AutoCloseable interface and final override on close()
  • koin-core.api: API dump updated
  • KoinAutoCloseableTest.kt: four tests covering use { } and close-on-exception for both classes
  • start-koin.md / starting-koin.md: use { } examples

Compatibility

  • Source compatible: existing code compiles without changes
  • Binary compatible: adding a supertype is ABI-safe, and KoinApplication.close() stays final
  • Behavioral: no change, close() keeps its existing semantics
  • Multiplatform: AutoCloseable is available in the Kotlin stdlib for all targets

Test plan

  • ./gradlew :core:koin-core:jvmTest --tests 'org.koin.core.KoinAutoCloseableTest' (4 tests pass)
  • ./gradlew :core:koin-core:apiCheck passes

🤖 Generated with Claude Code

lidonis and others added 3 commits October 2, 2026 23:37
KoinApplication.close stays final. Updates the koin-core API dump.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-4.3

# Conflicts:
#	docs/reference/koin-core/start-koin.md
#	docs/reference/koin-core/starting-koin.md
assertFailsWith's string argument is the assertion's failure message, so the
exception tests accepted any IllegalStateException. Assert the message instead,
and cover close() after use { } on Koin and KoinApplication.
@lidonis lidonis mentioned this pull request Nov 9, 2026
6 tasks done

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