mjdi docsv0.10.0latest
Container and Scope close semantics (P0-04)
Close behavior in mjdi is precisely specified and test-pinned by
Source: mjdi/docs/close-semantics.md
Close behavior in mjdi is precisely specified and test-pinned by
tests/test_close_semantics.mojo.
This page is the contract; every statement below is verified by an executed
test on the target compiler.
What close does
Container.close() and Scope.close_scope() release every instance cached
in that owner:
- cached singleton and scoped canonical instances (class providers),
- cached factory products (
singleton/scopedfactory scopes), - cached alias instances (the alias's own cache slot).
Each cached instance whose type conforms to InjectableLifecycle receives
pre_destruct immediately before the release, while it is still fully
usable. The slots are then emptied; the next resolution re-constructs.
Release order is reverse registration order (newest registered first):
a consumer is released before the dependency types it was wired with. The
traced specializations record the exact sequence (pre-destruct: <type>
per instance, then container-close/scope-close), which the tests pin.
The exact rules
| Rule | Behavior | Pinned by |
|---|---|---|
| Repeated close | Idempotent: a repeated close finds only empty slots, releases nothing, runs no pre_destruct — no double destruction. (The observability event container-close/scope-close is still recorded, with a released count of zero.) | repeated-close assertions |
| Resolution after close | Valid and non-terminal: the next resolve re-constructs with the current value configuration. Close is a release, not a shutdown; there is deliberately no use-after-close error state (the diagnostics catalog documents why). | post-close resolution assertions |
| Value slots | Survive close: they hold application configuration, not lifecycle state. has_value[T]() stays true; the re-constructed services see the same values (or the updated ones if re-set). | value-survive assertions |
was_closed() | False until the first close, True afterwards, stays True through repeated closes and re-constructions. Informational only. | was-closed assertions |
| Close with nothing cached | A no-op for the caches (the close event is still recorded) and still marks was_closed(). | empty-close assertions |
| Consumer copies held across close | Unaffected: cached providers hand out copies (Mojo value semantics). A copy is an independent value; closing while a copy is "active" never disturbs it. | active-resource assertions |
| Container vs scopes | Independent release owners. close() does not close scopes created from the container; close_scope() does not release container caches; either may be closed in either order, and a scope created after close() still works (value slots survived). | independence assertions |
| Nested scopes | Not a construct: Scope has no create_scope of its own. Scopes are one level deep, created from a container. | documented design |
Where the lifetime actually ends
Mojo ends a value's lifetime at its last use. A container or scope that is
simply dropped (no close()) releases its cached instances through Mojo's
ordinary deinitialization without lifecycle notification — that is the
exact gap the lifecycle safety diagnostics
(P0-03) make observable: with debug_tracking=True, dropping an unclosed
owner with cached instances prints one actionable diagnostic at drop time.
The ownership guards
The lexical guards (with_scope, with_container, ScopeGuard,
ContainerGuard) make close exactly-once at the guard level on both the
normal path and error unwinding, so forgetting the release call is a
compile-time-structured impossibility. See
docs/scope-guard.md and
docs/container-guard.md.
Compatibility notes
All of the above is the behavior 0.9.0 inherits, now pinned instead of
merely implied. Changes in 0.9.0 are additive: the was_closed() accessors,
the debug_tracking specialization, and the debug log. No close-path
behavior changed.