nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.37k stars 437 forks source link

Schema cache leaks memory with Laravel Octane #2436

Open adrianszczotkowski opened 1 year ago

adrianszczotkowski commented 1 year ago

I do not know where the problem comes from, nuwave/lighthouse or laravel/octane, so I put that in both project issues.

When cache is disabled Laravel Octane runs garbage collector, but on production is enabled (see difference in response times)

When cache is enabled Laravel Octane does not run garbage collector leading to memory exhaustion.

Steps to reproduce

  1. set php memory limit low, to have quicker effect
  2. use laravel/octane
  3. set env LIGHTHOUSE_CACHE_ENABLE=true
  4. run graphql queries

Output/Logs Cache off 204 OPTIONS /graphql .................................... 61.19 mb 4.94 ms 2023-08-15T07:59:58.195150293Z 200 POST /graphql ..................................... 61.22 mb 212.50 ms 2023-08-15T08:00:28.216507420Z 204 OPTIONS /graphql .................................... 61.19 mb 0.83 ms 2023-08-15T08:00:28.216723441Z 200 POST /graphql ..................................... 64.42 mb 203.90 ms 2023-08-15T08:00:38.223543408Z 204 OPTIONS /graphql .................................... 64.40 mb 3.72 ms 2023-08-15T08:00:38.223710925Z 200 POST /graphql ..................................... 61.00 mb 242.40 ms 2023-08-15T08:00:58.239050868Z 204 OPTIONS /graphql .................................... 60.97 mb 3.60 ms 2023-08-15T08:00:58.239365373Z 200 POST /graphql ..................................... 61.22 mb 226.69 ms 2023-08-15T08:01:28.263939214Z 204 OPTIONS /graphql .................................... 61.19 mb 3.63 ms 2023-08-15T08:01:28.263992024Z 200 POST /graphql ..................................... 61.22 mb 233.63 ms

Cahche On PHP memory set to 512MB

2023-08-15T07:52:38.117475683Z 200 POST /graphql ..................................... 105.41 mb 30.35 ms 2023-08-15T07:52:38.117477066Z 200 POST /graphql ..................................... 483.94 mb 36.73 ms 2023-08-15T07:53:38.159570346Z 204 OPTIONS /graphql ................................... 483.93 mb 3.71 ms 2023-08-15T07:53:38.159644884Z 204 OPTIONS /graphql ................................... 105.40 mb 3.64 ms 2023-08-15T07:53:38.159775573Z 200 POST /graphql ..................................... 115.33 mb 29.72 ms 2023-08-15T07:53:38.160064231Z 200 POST /graphql ..................................... 493.87 mb 35.12 ms 2023-08-15T07:54:38.199231222Z 204 OPTIONS /graphql ................................... 493.86 mb 3.61 ms 2023-08-15T07:54:38.199262697Z 204 OPTIONS /graphql ................................... 115.33 mb 3.46 ms 2023-08-15T07:54:38.199265135Z 200 POST /graphql ..................................... 125.26 mb 32.50 ms 2023-08-15T07:54:38.199304818Z PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/bootstrap/cache/lighthouse-schema.php on line 6 2023-08-15T07:54:38.199316710Z PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 65536 bytes) in /var/www/html/vendor/symfony/error-handler/Error/FatalError.php on line 14 2023-08-15T07:54:38.199349102Z [mutex.cc : 2374] RAW: Check w->waitp->cond == nullptr failed: Mutex::Fer while waiting on Condition

Lighthouse Version latest

Laravel Version latest

spawnia commented 1 year ago

There might be insufficient cleanup related to caching. Consider https://github.com/nuwave/lighthouse/blob/c28822acdb675ce3be99339c8bebf243bf0666e3/src/GraphQL.php#L350-L355 - perhaps something similar is also required for caching? The schema cache is dealt with in https://github.com/nuwave/lighthouse/blob/master/src/Schema/AST/ASTBuilder.php.