Z-Hub / Z-Push

Home of Z-Push
https://z-push.org
GNU Affero General Public License v3.0
97 stars 47 forks source link

Memory leaks on syncs? #22

Closed DoobleD closed 1 year ago

DoobleD commented 1 year ago

Z-Push is using a lot of memory, and despite PHP's generous default of memory_limit=128M, we often get OOM errors on syncs:

Nginx logs:

/var/log/nginx/error.log:2023/07/19 16:02:28 [error] 686#686: *40262 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 4096 bytes) in /opt/z-push/backend/imap/imap.php on line 1047" while reading response header from upstream, client: 212.129.83.XXX, server: obfuscated$, request: "POST /Microsoft-Server-ActiveSync?User=XXX@XXX.XX&DeviceId=XXXX&DeviceType=iPhone&Cmd=Sync HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm-zpush.sock:", host: "obfuscated"
/var/log/nginx/error.log:2023/07/19 16:18:01 [error] 685#685: *82295 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 9337000 bytes) in /opt/z-push/include/mimeDecode.php on line 773" while reading response header from upstream, client: 212.129.87.XXX, server: obfuscated, request: "POST /Microsoft-Server-ActiveSync?User=XXX@XXX.XX&DeviceId=XXXX&DeviceType=iPhone&Cmd=Sync HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm-zpush.sock:", host: "obfuscated"

Z-Push logs:

19/07/2023 16:02:16 [ 9371] [FATAL] [XXX@XXX.XXX] Fatal error: /opt/z-push/backend/imap/imap.php:1047 - Allowed memory size of 134217728 bytes exhausted (tried to allocate 4096 bytes) (1)
19/07/2023 17:06:18 [67007] [FATAL] [XXX@XXX.XXX] Fatal error: /opt/z-push/include/mimeDecode.php:488 - Allowed memory size of 134217728 bytes exhausted (tried to allocate 6857552 bytes) (1)

This issue has been reported already in mulitple places:

The workaround usually given is to increase memory_limit (to 256M for instance), but that's not always an option and is not a good solution anyway. Above 128M seems abnormally large.

Any ideas on that?

matidau commented 1 year ago

Increasing the memory_limit is my first recommendation, it is pretty common for a lot of PHP apps to be set at 256M or 512M.

If this isn't an option try some PHP-FPM optimisation such as reloading workers. I think this article should still be relevant https://geekflare.com/php-fpm-optimization/

From your links @fmbiete also suggested to track down the memory leak. Would happily accept a PR for this.

DoobleD commented 1 year ago

Thanks for the feedback @matidau! I haven't tried the emergency_restart_* options mentioned in the guide, could be worth a test.

I lack the skills and motivation to track the leak. I'll close the issue for now and perhaps someone having investigated more can re-open later on.

matidau commented 1 year ago

I haven't tried the emergency_restart_* options mentioned in the guide, could be worth a test.

Would still be keen to hear if this helps, if you feel like dropping a comment back here.

DoobleD commented 1 year ago

Sure thing! I'll let you know if I try it.