mjdi

Search documentation

Search the mjdi v0.10.0 documentation

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

  1. pre_destruct cannot fail. The trait signature is non-raising (def pre_destruct(mut self) — no raises). This is deliberate: the hook runs inside close()/close_scope(), which return None and 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.

  2. 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.

  3. At most one cleanup attempt per cached lifetime. A cached canonical instance receives exactly one pre_destruct when mjdi releases it. A repeated close() finds empty slots and attempts nothing — cleanup is never retried on an instance that was already released.

  4. post_construct may raise. Construction happens in a raising context (resolve propagates errors). When post_construct raises: the error propagates out of resolve, the half-built instance is discarded — nothing is cached (no insertion), and pre_destruct is 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.

  5. 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.

  6. Factory failures are runtime errors, not lifecycle failures. A factory that raises propagates as an Error with its cause preserved — see docs/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.