OTP Supervision Audit
This document identifies all instances of unsupervised process creation (spawn
, Task.start
) within the core application logic (lib
directory). Unsupervised processes are a major architectural risk as they are not part of a supervision tree, leading to a lack of fault tolerance and potential for silent failures.
1. Audit Findings
The following files contain calls to spawn
, spawn_link
, spawn_monitor
, or Task.start
that are not linked to a supervisor. These represent critical areas for refactoring to ensure proper OTP compliance and system stability.
lib/mabeam/load_balancer.ex
: 293lib/mabeam/coordination.ex
: 912lib/mabeam/comms.ex
: 311lib/mabeam/agent.ex
: 643, 773lib/foundation/process_registry.ex
: 757lib/foundation/application.ex.backup
: 504, 509, 890, 895lib/foundation/application.ex
: 505, 510, 891, 896lib/foundation/coordination/primitives.ex
: 650, 678, 687, 737, 743, 788, 794lib/foundation/beam/processes.ex
: 229
2. Analysis and Recommendations
The widespread use of unsupervised spawn
and Task.start
indicates a systemic departure from OTP best practices. These calls should be replaced with supervised alternatives.
- For long-running processes: These should be structured as
GenServer
s or other OTP behaviours and started under a supervisor. - For concurrent, short-lived tasks: These should be managed by a
Task.Supervisor
. This ensures that even temporary tasks are properly monitored and cleaned up.
Next Steps:
- Prioritize Refactoring: The identified files should be prioritized for refactoring. The
foundation
andmabeam
modules are the most critical. - Create a
Task.Supervisor
: A general-purposeTask.Supervisor
should be added to the main application supervision tree to manage short-lived, concurrent tasks. - Update Developer Guidelines: The project’s developer guidelines should be updated to explicitly forbid the use of unsupervised
spawn
andTask.start
in application code.