Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope instances not getting cleanedup properly #1896

Closed
lexa-diky opened this issue Jun 19, 2024 · 2 comments
Closed

Scope instances not getting cleanedup properly #1896

lexa-diky opened this issue Jun 19, 2024 · 2 comments

Comments

@lexa-diky
Copy link

lexa-diky commented Jun 19, 2024

Describe the bug
Very similar to: #1176
Scopes created by KoinScopeComponent are sharing instance cache.

Good real-life example is RequestScope form koin-ktor. If somewhere mid pipeline we fail to register one of instances via scope.declare and fail, instance from previously successful requests will still be returned by Koin when requested further down the pipeline.

To Reproduce
Isolated case:

class MyScopeComponent(private val _koin: Koin) : KoinScopeComponent {
    override fun getKoin(): Koin = _koin
    override val scope: Scope = createScope()
}

fun main() {
    val koin = koinApplication { printLogger(Level.DEBUG) }.koin

    val scopeComponent = MyScopeComponent(koin)
    val scope = scopeComponent.scope
    scope.declare("hello")
    require("hello" == scope.get<String>())

    scope.close()

    val scopeComponent2 = MyScopeComponent(koin)
    val scope2 = scopeComponent2.scope

    require(scope2.getOrNull<String>() == null)
}

Expected behavior
Both require expressions in "To reproduce" should pass

Koin module and version:
koin-core:3.5.5

Snippet or Sample project to help reproduce
See "To reproduce"

@agabeyalioglu
Copy link

i am facing the same issue on 4.0.0-RC2

@arnaudgiuliani arnaudgiuliani added this to the 4.0.1 milestone Nov 18, 2024
@arnaudgiuliani arnaudgiuliani added status:checking currently in analysis - discussion or need more detailed specs core and removed status:checking currently in analysis - discussion or need more detailed specs labels Dec 17, 2024
@arnaudgiuliani
Copy link
Member

The declare function takes your instance as a new definition for this kind of scope. It will return the value as the instance of the factory. This is why in 2 different scope's instances, you get the result again.

I changed the implementation of declared instances in scope: #2082

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants