Skip to content

Fix Module.isLoaded: reflect actual loading instead of non-empty mappings - #2489

Open
Inf1mum wants to merge 2 commits into
InsertKoinIO:mainfrom
Inf1mum:fix/2373-is-loaded
Open

Inf1mum wants to merge 2 commits into
InsertKoinIO:mainfrom
Inf1mum:fix/2373-is-loaded

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 4, 2026

Copy link
Copy Markdown

Fixes #2373

Problem

Module.isLoaded was implemented as mappings.isNotEmpty(). Since mappings is populated as soon as definitions are declared through the DSL (before any registry involvement), isLoaded returned true for any non-empty module, even before startKoin / loadKoinModules:

val loadedModule = module {
    single { 123 }
}
println(loadedModule.isLoaded) // true — but nothing was ever loaded into a registry

Fix

isLoaded now reflects the actual module loading:

  • Module keeps an internal @KoinInternalApi internal var isLoadedFlag (default false), and the public getter returns that flag — the public ABI is unchanged (public final fun isLoaded ()Z).
  • InstanceRegistry.loadModule(...) sets module.isLoadedFlag = true once the module's mappings have been saved into the instance registry. This covers both startKoin { modules(...) } and loadKoinModules(...), as well as modules pulled in via includes(...) (loading goes through flatten, so included modules are marked too).

Semantic change (heads-up)

isLoaded used to mean "this module declares at least one definition". It now means "this module has actually been loaded into an instance registry". Concretely:

  • a non-empty module that was never passed to a Koin context now reports false (this was the bug);
  • an empty module that has been loaded now reports true (the flag tracks "was loaded", not "has definitions").

Design note / question for maintainers

The flag is currently not reset on unloadModules / close(): it stays true once the module has been loaded at least once ("was loaded at some point" semantics). If the preferred semantics are "currently loaded in the registry", the flag could be cleared in InstanceRegistry.unloadModule(...) instead — happy to adjust.

…g, not non-empty mappings

Signed-off-by: Inf1mum <vadim.lazuk@mail.ru>
@Inf1mum

Inf1mum commented Nov 16, 2026

Copy link
Copy Markdown
Author

Friendly reminder - open to review whenever convenient, and happy to make any needed changes.

saveMapping(override, mapping, factory)

}
module.isLoadedFlag = true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when a module is unloaded via unloadKoinModules(...) / InstanceRegistry.unloadModules(...)? unloadModule() doesn't reset module.isLoadedFlag = false, so module.isLoaded stays true even after its definitions have been removed from _instances.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - you are right that unloadModules left isLoadedFlag untouched, so isLoaded stayed true after unloading. The unload path now resets the flag to false (mirroring the true set on load), so isLoaded reflects actual loading end to end, and the definitions are dropped from _instances as before. A regression test covers the load -> unload -> isLoaded == false flow, including the included-module case, and fails on the previous head without the fix.

…ed reflects actual state

Signed-off-by: Inf1mum <vadim.lazuk@mail.ru>

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.

The Module#isLoaded value is always true for non-empty modules

2 participants