livewire / volt

Volt is an elegantly crafted functional API for Livewire.
https://livewire.laravel.com/docs/volt
MIT License
307 stars 19 forks source link

`Volt::mount()` merges duplicate paths [Memory leak] #96

Closed inmanturbo closed 4 months ago

inmanturbo commented 4 months ago

Volt Version

1.6.2

Laravel Version

10

PHP Version

8.3

Database Driver & Version

No response

Description

Volt::mount() merges new mounted directory instances regardless of path. This can lead to memory leaks, especially on Octane.

https://github.com/livewire/volt/blob/8f4bbbd6f19a4a1e56df4430d4d7eeaf58f546fa/src/MountedDirectories.php#L30

for ($i = 1; $i <= 50; $i++) {
    Volt::mount([
        config('livewire.view_path', resource_path('views/livewire')),
        resource_path('views/pages'),
    ]);
}

dump(\Livewire\Volt\Volt::paths()) 

//0 => 
// Livewire\Volt..\MountedDirectory {#1715 ▶}
//  1 => 
// Livewire\Volt..\MountedDirectory {#1716 ▶}
//  2 =>  
// Livewire\Volt..\MountedDirectory {#1717 ▶}
//  3 => 
// Livewire\Volt..\MountedDirectory {#1718 ▶}
//  4 =>  
// Livewire\Volt..\MountedDirectory {#1719 ▶}
//..
//  25 => 
// Livewire\Volt..\MountedDirectory {#1979 ▶}

Steps To Reproduce

https://github.com/inmanturbo/volt-memory-leak

inmanturbo commented 4 months ago

You can see example of a test for this case in #88 here:

https://github.com/livewire/volt/pull/88/commits/e982a76bdf33955149f62c63875f7d6e97df36fb

inmanturbo commented 4 months ago

This is actually fixed by #95, because there will no longer be any need to call Volt::mount with the same path(s) more than once.