Maravel-Framework 10.61.0 prevents circular dependency
Source: Dev.to
Background
There have been multiple attempts to add this feature in Laravel.
Problem
In Maravel-Framework, macroable classes are resolved from the container.
Some classes—e.g. \Illuminate\Events\Dispatcher or Illuminate\Console\Scheduling\Schedule—create a circular dependency when resolved from the container. This leads to:
- Segmentation faults
- Memory exhaustion
Developers can spend a long time debugging these issues.
Solution
To detect such cases early, this PR introduces a hard memory limit of 10 MB (configurable via app.circular_dependency_memory_limit). When the limit is reached, a CircularDependencyException is thrown instead of allowing the process to run into a fatal error.
/**
* Limit of extra memory used to detect circular dependencies
* when resolving an abstract from container (bytes)
*/
'circular_dependency_memory_limit' => 10_485_760, // 10 MB
Error Example
Illuminate\Contracts\Container\CircularDependencyException
Circular dependency detected while resolving [Illuminate\Console\Scheduling\Schedule].
Memory limit reached or exceeded 10485760 bytes.
Dependencies initial memory (bytes): {"Illuminate\Console\Scheduling\Schedule":27262976}
at vendor/macropay-solutions/maravel-framework/illuminate/Container/Container.php:886
What the message means
- When
Illuminate\Console\Scheduling\Schedulebegins to be resolved, PHP memory usage (viamemory_get_usage(true)) is 27 262 976 bytes. - The detection loop continues until the memory increase exceeds 10 MB (10 485 760 bytes).
- If not stopped, the loop would keep allocating memory until a segmentation fault or a fatal “out of memory” error occurs.
Additional Notes
- A time‑based limit also results in segmentation faults or memory‑limit fatal errors.
- The first attempt to limit based on memory increments proved unreliable.
- This improvement follows a previous change to the DI helper, which avoided calling
makeon the container before the application finished booting.