mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi docsv0.10.0latest

Lifecycle safety diagnostics (P0-03)

mjdi can tell you when a cached resource was dropped without its cleanup

Source: mjdi/docs/lifecycle-safety-diagnostics.md

mjdi can tell you when a cached resource was dropped without its cleanup opportunity: the opt-in debug_tracking=True container/scope specialization records cache insertions and releases, and prints one actionable diagnostic when a tracked owner dies unclosed with instances still cached. Tracking is disabled by default; the default specialization is unchanged (verified by the full suite running on the default spellings).

What is tracked

StateWhere it is visible
A canonical instance entered a cache slotdebug_report() records cache-insert: <service>
The instance was released by an explicit closedebug_report() records release: <service> and the container-close/scope-close entry
The owner was closedwas_closed() returns True
The owner was dropped without close while instances were cachedthe destructor prints MJDI-D001 (container) / MJDI-D002 (scope)
var app = Container[type_of(reg), False, False, True, True]()  # debug_tracking=True
_ = app.resolve[ConnectionPool, provider_scope.singleton]()
print(app.debug_report())   # cache-insert: ConnectionPool ...
app.close()
print(app.was_closed())     # True

The unclosed-resource diagnostic looks like:

mjdi debug [diagnostic MJDI-D001]: Container dropped without close() while 1 cached instance(s) exist:
    - ConnectionPool
    The cached instances were released by Mojo's ordinary deinitialization;
    their pre_destruct hooks did NOT run. Call close() (or use the
    with_container guard) when your services declare lifecycle hooks.

What the diagnostic means (and does not mean)

  • The cached instances were released — Mojo always deinitializes values; mjdi never leaks. What was lost is the pre_destruct notification: flushes, handle closes, and other cleanup the service declared did not run. That is the actionable fact.
  • The diagnostic prints to stdout at the owner's drop point (Mojo ends value lifetimes at last use, so this is deterministic for a deterministic program). It cannot raise, cannot be caught, and cannot be recorded into the owner's log — the log dies with its owner — which is why the destructor path is print-only and verified through the runner's golden-output check (tests/golden_debug_diagnostics.mojo, MJDI-D001 and MJDI-D002 both asserted) rather than by in-process assertions.
  • It reports the affected services by name (collect_cached_services), so you know exactly what missed its hooks.

What is deliberately not tracked

  • Value slots — application configuration, not lifecycle state; they survive close by design and are released by ordinary deinitialization like any other value.
  • Transient instances — owned by the caller from the moment resolve returns; mjdi never sees their release.
  • Linear services — never cached: the visitor overload of resolve destroys them explicitly on every path (success and error).
  • Consumer copies of cached instances — independent values.

Limitations

  • The destructor diagnostics are print-only (the target compiler's destructors cannot raise or write into a surviving structure — verified by experiments/exp10_deinit_diagnostics.mojo). There is no callback or push-model hook: the supported compiler cannot store capturing closures safely (the same limitation as lifecycle tracing).
  • The diagnostic reports unclosed owners at drop time only. It cannot retroactively list every historical close — use debug_report() / tracing for the recorded history.
  • Tracking costs a bounded DebugLog (64 entries, overflow-flagged) and a closed flag per owner; it is compiled into the specialization only when enabled.

Stable identifiers

MJDI-D001 (container) and MJDI-D002 (scope) are pinned diagnostic IDs in the catalog and in the emitted text, following the validation output contract.