statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.02k stars 528 forks source link

Memory leak while using NoCache #10973

Open webcodecs opened 1 week ago

webcodecs commented 1 week ago

Bug description

We are currently encountering an issue on one of our sites that ultimately results in a php memory leak error. After some investigation we found that the cause for this problem is a cache file under storage/statamic/static-urls-cache. The file in question seems to be responsible for storing the String regions for the Nocache tag, which we use inside our nav section to handle some authentication-based renderings.

It seems that loading the page is triggering the write() and restore() methods of src/StaticCaching/NoCache/Session.php. The restore() method is performing a merge of the String region collection items, which results in the file mentioned above doubling in size after each request. After ˜20 reloads of our site we run into the memory error mentioned above. We then need to remove the file in question or perform a full cache flush via command line or the CP.

As far es we know this issue is only present when using STATAMIC_STATIC_CACHING_STRATEGY=null. As I understand it, the files should not even be written if the caching strategy is null. The problem can be solved by appending ->unique()->values() to the merge call in src/StaticCaching/NoCache/Session.php:126 , but we don't know if this is a viable solution to the problem, as we have limited knowledge about the ins and outs. I can imagine that the merge was based on the assumption that existing regions would be overwritten. However, this only works with an associative array. We have a list at this point.

How to reproduce

  1. Checkout bug repository https://github.com/Digital-Masters/statamic-nocache-bugreport
  2. Refresh homepage

Fore each refresh one file within storage/statamic/static-urls-cache will get bigger and bigger

Logs

No response

Environment

Application Name: Statamic
Laravel Version: 11.28.1
PHP Version: 8.3.12
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: bug-report.test
Maintenance Mode: OFF
Timezone: UTC
Locale: en

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: NOT CACHED

Drivers
Broadcasting: log
Cache: file
Database: sqlite
Logs: stack / single
Mail: log
Queue: sync
Session: file

Statamic
Addons: 0
Sites: 1
Stache Watcher: Enabled (auto)
Static Caching: Disabled
Version: 5.31.0 Solo

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

duncanmcclean commented 1 week ago

Can you try moving the storage of the nocache tag into a database to see if it helps? https://statamic.dev/tags/nocache#database

webcodecs commented 1 week ago

@duncanmcclean after changing the nocache driver to database, their is only one entry as expected in the database table.

If its possible we want to avoid using a database

jasonvarga commented 1 week ago

I see the issue, thanks. I'm curious why you are using nocache tags without static caching though.

webcodecs commented 1 week ago

It is noticed in the local development environment where I have disabled the static cache

jasonvarga commented 1 week ago

Okay so it's not something that affects you in production. 👌

webcodecs commented 1 week ago

Because we are in early stage of a relaunch we deactivated the static caching on the staging server too. But yes when we are live, the caching strategy will be set