mjdi docsv0.10.0latest
Lifecycle failure policy (P0-05)
What happens when lifecycle cleanup fails? mjdi's answer is precise,
Source: mjdi/docs/lifecycle-failure-policy.md
What happens when lifecycle cleanup fails? mjdi's answer is precise,
verified, and pinned by tests/test_lifecycle_policy.mojo.
The contract
-
pre_destructcannot fail. The trait signature is non-raising (def pre_destruct(mut self)— noraises). This is deliberate: the hook runs insideclose()/close_scope(), which returnNoneand have no error path to report through. A service whose cleanup can fail (a flush that might hit a full disk, a network close that might time out) must handle that failure inside the hook — record a status field, log it — and keep the hook non-raising. -
Every cached instance is released. The release walk clears every cache slot in one pass. Because hooks cannot raise, one service's misbehaving cleanup cannot abort the walk or skip its siblings: a "failing" (self-handled) hook and a healthy sibling are both released in the same close, each exactly once.
-
At most one cleanup attempt per cached lifetime. A cached canonical instance receives exactly one
pre_destructwhen mjdi releases it. A repeatedclose()finds empty slots and attempts nothing — cleanup is never retried on an instance that was already released. -
post_constructmay raise. Construction happens in a raising context (resolvepropagates errors). Whenpost_constructraises: the error propagates out ofresolve, the half-built instance is discarded — nothing is cached (no insertion), andpre_destructis not called for it (mjdi only releases what it cached). The next resolution attempts a fresh construction against clean container state. All of this is pinned by tests, including the "no insertion" counter. -
No error aggregation across hooks. Because cleanup hooks are non-raising by design, there is nothing to aggregate: the policy is "handle your own failure inside the hook" rather than "mjdi collects cleanup errors". This matches the target compiler's capabilities (there is no error-cause chaining to aggregate into) and keeps the release paths non-raising.
-
Factory failures are runtime errors, not lifecycle failures. A factory that raises propagates as an
Errorwith its cause preserved — seedocs/runtime-error-reference.md(MJDI-R004).
Why non-raising rather than fallible hooks
The alternative — raises cleanup with error aggregation — would require
either a raising close() (a breaking change to an existing API) or a
drop-error channel that Mojo's destructor semantics cannot provide
(destructors cannot raise; verified by
experiments/exp10_deinit_diagnostics.mojo and the destructor-diagnostic
work in P0-03). Non-raising hooks plus documented self-handling is the
safest contract the compiler supports, and it preserves mjdi's existing
close() -> None API.
What this means in practice
struct Connection(Injectable, InjectableLifecycle):
var status: String
def pre_destruct(mut self):
# Fallible work: handle it here. Never raise (you cannot).
if not self.try_flush():
self.status = "flush-failed" # observable, non-fatal
Applications that need hard failure signals on cleanup should pair the
status field with their own observability; mjdi's tracing (traced=True)
records the release sequence, and the debug tracking
(debug_tracking=True) reports cached instances that were dropped without
any cleanup attempt at all.