mjdi docsv0.10.0latest
Re-entrancy guard (P1-20)
Compile-time graph validation follows Inject fields — it cannot see
Source: mjdi/docs/reentrancy.md
Compile-time graph validation follows Inject fields — it cannot see
cycles created by construction code. The re-entrancy guard is the
runtime protection: when a resolution of T re-enters a resolution of
the same service that is still in progress, mjdi raises a precise,
actionable diagnostic instead of recursing to stack exhaustion.
What it detects
A cycle invisible to the compiler arises when custom construction code resolves services that resolve the constructor's own service again:
struct A(Injectable):
@staticmethod
def create[R: DependencyResolver](mut r: R) raises -> Self:
_ = r.resolve[B]() # B.create resolves A again
var a = A()
return a^
struct B(Injectable):
@staticmethod
def create[R: DependencyResolver](mut r: R) raises -> Self:
_ = r.resolve[A]() # the cycle closes here
var b = B()
return b^
Resolving A on one container raises at the second entry of A, with
the repeated service, the full resolution path, and the diagnostic
ID:
mjdi: re-entrant resolution of 'A' detected while it is already being
resolved. Resolution path: A -> B -> A. This is a dependency cycle the
compile-time graph validation cannot see: it is created by custom
construction code (a custom create override or a linear constructor)
resolving services that resolve this service again. [diagnostic
MJDI-R003] Break the cycle by restructuring the custom construction
path, or remove the re-entrant resolve call.
Direct and indirect cycles are both detected
(tests/test_reentrancy.mojo pins A -> B -> A and X -> Y -> Z -> X),
scopes guard their own resolutions, and the guard state is cleaned
up after the error — a later resolution detects freshly rather than
reading a stale flag.
How it works
Every resolution (container and scope) tracks a per-service active flag plus a small index stack (the diagnostic path). Entering a resolution whose service is already active raises; both exit paths clear the state. The check is one flag test on the happy path; the index stack is a fixed 32-entry array with a depth counter (resolution depth is bounded by the graph).
What it deliberately cannot see (documented restriction)
Thin factory functions cannot capture the resolving container (the
factory registration is a capture-free function by contract), so a
factory cannot participate in a same-container cycle. What a factory
can do is construct its own container for a registry whose
resolution re-enters that factory — recursion across containers, where
every level has fresh guard state. Detecting that would require
mutable global state, which mjdi forbids by design (AGENTS.md rule 2).
The discipline instead: factories must not resolve their own service
type through any container — the graph that would create is a
factory-mediated cycle with no same-container detection possible.
docs/known-limitations.md records the boundary.
Cost and opt-out
The guard is on by default (safety first: it converts a stack overflow into a diagnostic with a path). Measured cost: ~7 ns per resolution on hit paths and one reserved flag-list allocation per guarded container/scope construction (see Performance for the full table and the accepted architectural justification). For proven-acyclic hot paths:
var app = Container[type_of(reg), traced=False, collect_stats=False, reentrancy_guard=False]()
The disabled specialization compiles the tracking away entirely.
Related
- Performance — the guard's measured overhead.
- Compile-time validation — what the compiler does reject (Inject-field cycles: MJDI-E003-class checks).
- Diagnostics — the MJDI-R003 catalog entry with its quick fix.