Fix #2412 - drop displaced factory's cached singleton on override#2413
Open
glasser wants to merge 1 commit into
Open
Fix #2412 - drop displaced factory's cached singleton on override#2413glasser wants to merge 1 commit into
glasser wants to merge 1 commit into
Conversation
…override
When `InstanceRegistry.saveMapping` replaces an existing mapping (e.g.
via `koin.declare(...)`), the displaced `InstanceFactory` was left with
its cached value still set. Because `Module.mappings` shares factory
references with the registry, the orphan survives `stopKoin()` — which
iterates `_instances`, never the orphan — and is re-installed on the
next `startKoin { modules(sameModule) }`, returning a stale singleton
from the previous lifecycle.
Fix: in `saveMapping`'s override branch, call `dropAll()` on the
displaced factory before overwriting. The `displaced !== factory`
guard preserves the no-op behavior when the same factory is
re-registered (e.g. loading an already-loaded module).
Adds `DeclareOverrideDropsCachedSingleTest` as a regression: declare a
fresh override on top of a module-owned `single`, mutate the singleton,
`stopKoin`, then `startKoin` with the same module — the next `get<T>()`
must return a fresh singleton.
See InsertKoinIO#2412 for the full diagnosis, including the kotest-6 interaction
that newly exposes the bug.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.
Fixes #2412.
When
InstanceRegistry.saveMappingoverrides an existing mapping (e.g. viakoin.declare(...)), the displacedInstanceFactorywas left with itsvaluestill cached. BecauseModule.mappingsshares factory references with the registry, the orphan survivedstopKoin()(which only iterates_instances) and was re-installed on the nextstartKoin { modules(sameModule) }, leaking the previous lifecycle's singleton into the new one.This is a long-standing latent bug — present in every released koin version going back to 3.x. It only manifests when the same
Moduleinstance is loaded across multiplestopKoin/startKoincycles after adeclareoverride has populated the displaced factory's cache. That combination is rare in production but routine in test setups; in particular it became newly visible in late-2024 / 2025 to users ofkotest-extensions-koinafter kotest 6 turnedSpec.extensionsfrom a function into a property. See the issue for the full diagnosis, the kotest-6 connection, and a public reproduction repo.The fix is two lines in
saveMapping's override branch: when_instances[mapping]is being replaced by a different factory, calldropAll()on the displaced one before overwriting. Thedisplaced !== factoryguard preserves the no-op behavior when the same factory is re-registered (e.g., loading an already-loaded module).Test
Adds
DeclareOverrideDropsCachedSingleTestcovering the regression: declare a fresh override on top of a module-ownedsingle, mutate the singleton,stopKoin, thenstartKoinwith the same module instance —get<T>()must return a fresh singleton.assertNotSame(first, second)andassertEquals(0, second.value)assertions.:core:koin-core:jvmTestis green (284/284, including all ofDeclareInstanceTest— no behavior change for documenteddeclare/override semantics, only for the latent cache leak).