Repository navigation
Conversation
…re override Signed-off-by: Inf1mum <vadim.lazuk@mail.ru>
|
@Inf1mum Yeah, this looks the same as mine — since mine got stale-closed, let's go with this one if you can get a maintainer's attention :) |
|
Thanks @glasser — acknowledged, both PRs implement the same fix for the displaced factory's stale cached state on |
| // drop the displaced factory's cached state, as it can still be | ||
| // referenced from module mappings and survive Koin restart | ||
| if (displaced !== factory) { | ||
| displaced.dropAll() |
There was a problem hiding this comment.
One edge case with secondary bindings (binds): a SingleInstanceFactory is stored in _instances under both its primary type and every bound secondary type. If a caller overrides just one of those mappings (e.g. declare<Foo>(fakeFoo) while FooImpl or Bar still points to displaced), calling displaced.dropAll() here drops the live singleton and fires onClose for the remaining active bindings too. Checking that displaced has no remaining references in _instances after overwriting _instances[mapping] = factory (e.g. _instances.values.none { it === displaced }) would avoid breaking partial overrides while still clearing fully displaced factories.
There was a problem hiding this comment.
Good catch - fixed in ce5e660: saveMapping now decides on dropping only after the mapping is overwritten, and drops the displaced factory only when no other entry in _instances still references it (your none { it === displaced } check). A partially overridden factory stays live under its remaining bindings with its cached instance and onClose untouched, while a fully displaced factory is still dropped, exactly once. Two regression tests in DeclareInstanceTest cover both cases; :core:koin-core:jvmTest (297 tests, 0 failures) and :core:koin-test:jvmTest (25 tests, 0 failures) pass.
…eferences it A SingleInstanceFactory is stored under its primary type and every bound secondary type. Overriding just one of those mappings used to drop the displaced factory even though it was still live under its other bindings, closing its cached instance and firing onClose for active bindings. Decide on dropping only after the mapping is overwritten: drop the displaced factory only when no mapping in _instances still references it; fully displaced factories are still dropped, exactly once. Signed-off-by: Inf1mum <vadim.lazuk@mail.ru>
Fixes #2412
The leak
When a binding is overridden via
koin.declare(...)(or anyInstanceRegistry.saveMappingcall withallowOverride = true), the displacedInstanceFactoryis replaced inInstanceRegistry._instances, but its cached value is never dropped. BecauseModule.mappingsstill holds the original factory by reference, that factory survivesstopKoin()with its old singleton intact:Koin.close()→instanceRegistry.close()only iterates_instances(i.e. the declare-owned factory), so the orphaned original keeps itsSingleInstanceFactory.value.The next
startKoin { modules(sameModuleInstance) }re-registers the still-cached factory fromModule.mappings, and the nextget<T>()returns a stale instance — including any state mutations from the previous Koin lifecycle. This matters most in test setups that reuse aModuleacrossstartKoin/stopKoincycles (e.g. kotest 6'sKoinExtension, which now captures the module once per spec — see the issue for details).The fix
In
InstanceRegistry.saveMapping, when an override displaces a different factory, drop the displaced factory's cached state before replacing it:The
displaced !== factoryguard keeps the no-op behavior when the same factory is re-registered (e.g. loading the sameModuleagain in the same lifecycle, or re-saving secondary-type mappings of the factory itself).Note on
onClosetimingAs a consequence, the displaced factory's
onClosehook is now invoked at the moment of displacement (override), with the cached instance — instead of never, or atclose()only for factories still present in_instances. This matches the semantics ofInstanceRegistry.close(), which already callsdropAll()(and thereforeonClose) on every registered factory. CallingonClosewith anullvalue for a never-resolved factory is also consistent with existing behavior (FactoryInstanceFactory.drop()already does exactly that).Test
Added a regression test in
DeclareInstanceTestfollowing the issue scenario:startKoinwith a module holding a stateful singleton →getand mutate →declarean override of the same type →stopKoin→startKoinwith the sameModuleinstance →get. The test assertsassertNotSamewith the previous lifecycle's instance and fresh state; it fails onmainwithout the fix.This supersedes the stale #2413 (auto-closed on 2026-10-14) — happy to coordinate with @glasser if they want to continue on it.
Verification
./gradlew :core:koin-core:jvmTest: 295 tests, 0 failures (2 pre-existing skips), including the new regression test../gradlew :core:koin-test:jvmTest: 25 tests, 0 failures.