Circular dependencies
Two services that inject each other cannot both be constructed first. The container detects this and fails with a clear error instead of hanging or handing out a half-built object. This page shows the error and the ways to resolve it.
The error
The error is a DIError with code CircularDependency. error.context.cycle holds the path as an array of names, starting and ending with the same service.
How detection works
While a service is being constructed it is marked as in progress. An inject() call that reaches a service already in progress on the current construction stack has found a cycle, and throws on the spot. This costs nothing when there is no cycle, so it is always on, in production too.
Cycles can also form across asynchronous work: one service waits for another's onServiceInit, which in turn waits for the first through a factory's ctx.inject(), a factory token or an awaited asyncInject() promise. The container tracks who is waiting for whom and fails the resolution that would close the loop, with the same error code and the path of services that wait on each other.
A cycle of any length is detected, not just two services.
The container cannot see who awaits a promise that its own get() returned. These cycles are not detected and hang:
await container.get(B)in theonServiceInitofA, whenBneedsA. InjectBinstead.await ctx.container.get(B)in a factory. Usectx.inject(B), which is tracked.- A promise chained in the constructor (
this.ready = asyncInject(B).then(...)) and awaited later inonServiceInit.
Fix the design first
A cycle usually means two classes share a responsibility that belongs in a third. These fixes remove the cycle instead of working around it:
Breaking a cycle with asyncInject
When the cycle is real, as with two services that call each other at runtime but not during construction, asyncInject() breaks it on one side.
asyncInject() returns a promise and does not start resolving until the current constructor has returned. By then OrderService exists, so BillingService can inject it normally. Both end up holding the same two singletons.
Rules for using it:
- One side is enough. Keep
inject()on the other side so only one service pays for theawait. - Await the promise in methods, never in
onServiceInit. IfOrderService.onServiceInitawaitsthis.billing,OrderServiceis not ready untilBillingServiceis, andBillingServiceis waiting forOrderService. The cycle is back, and both resolutions fail withCircularDependency. - It needs a stored service on the way. A cycle made only of transients has no end, since each of them is a new instance: awaiting the promise fails with
CircularDependency. - Do not use it for anything else.
inject()already handles transients and services with asynchronous initialisers.