Unexpected DependencyLoopException
See original GitHub issueKodein 5.2.0 (5.0.0 failed too)
Following code for some reason throws a DependencyLoopException:
val kodein = Kodein.lazy {
bind<Kodein>() with singleton { kodein }
bind<Client>() with singleton { Client(instance()) }
bind<A>() with singleton { A(instance()) }
}
class Client(override val kodein: Kodein) : KodeinAware {
fun doSomething() { ... }
}
class A(override val kodein: Kodein) : KodeinAware {
val client by instance<Client>()
fun doSomething() {
client.doSomething()
}
}
fun main(args: Array<String>) {
// Works
val client by kodein.instance<Client>()
client.doSomething()
// Throws an exception
val a by kodein.instance<A>()
a.doSomething()
}
Replacing the Client binding with singleton { Client(kodein) }
works.
I feel like I’m missing something here, but it just doesn’t look right to me.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How to fix nasty circular dependency issues once and for all in ...
Once we move each class to it's own file, it turns out that the very same application suddenly, utterly dies, seemingly beyond repair...
Read more >Unexpected connection state when multiple requests at the ...
This is something I completly understand : the code is trying to access to the database with the same connection in parallel, but...
Read more >ansible-galaxy collection install timeout · Issue #2302 - GitHub
We've seen ERROR! Unexpected Exception, this is probably a bug: ('The read operation timed out',) (10 minute time out) quite a few times....
Read more >Handling Resolution Errors - Prism Library
Some common errors developers run into is a Service that was not registered or invalid XAML that generates an Exception when the View...
Read more >Indexing is stuck forever for a project
After some moment my indexing is stuck forever. In the log of Webstorm I can see this error over and over again (every...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ll investigate 😉
Ooooh, this is suuuuuch a weird thing.
So, to understand why this is showing this exception, you need to understand how Kodein detects dependency loops.
You see, in a binding, the
kodein
variable is NOT the Kodein instance you constructed and used for retrieval. It is a Kodein that knows its parent nodes. Let me show you an example. Consider the following:When asking for first A, the singleton provided lambda is called with a this Kodein that knows “All my requests are going to be A dependencies”. The lambda asks for a B, so it creates a new Kodein instance that knows “I come from A, and all my requests are going to be B dependencies”. By the same logic, a new Kodein instance is later constructed that knows “I come from B before A, and all my requests are going to be C dependencies”. Then, when a A instance is requested from this last Kodein instance, it knows that there is a dependency loop that had arisen from asking an instance of a type that it is a dependency.
Now, remember that
singleton
bindings are initialized on first access. This means that the Kodein instance that is cached is the one that is created by the first need, which in your code is the lineval client by kodein.instance<Client>()
. So, what is cached is an intermediate instance whose internal state is “I come fromClient
, and all my requests are going to beKodein
dependencies”. Then, later you reuse that Kodein instance, giving it to yourA
constructor, which uses it to request aClient
dependency. The loop is made, this Kodein instance thinks it already comes from theClient
constructor. So it does what it’s supposed to: it throws an error.I have no idea of how to prevent this other than saying DO NOT FREAKING DO THIS!: everything is working as it should!