thecodingmachine / docker-images-php

A set of PHP Docker images
MIT License
768 stars 137 forks source link

Lots of `<defunct>` processes when using Cron #368

Open R0Wi opened 9 months ago

R0Wi commented 9 months ago

Hi, first of all: thanks for these great images, I really love them an use them a lot!

Problem definition

In one of my prod instances I'm using the image to run Shopware 5 in a Docker container. The system uses cron jobs to execute background jobs, so I'm using a docker-compose configuration like this:

version: '3.3'

services:
   app:
     image: thecodingmachine/php:8.1-v4-apache
     environment:
       # Shopware cron
       - CRON_USER=www-data
       - CRON_SCHEDULE=*/5 * * * *
       - CRON_COMMAND=php bin/console sw:cron:run
# ...

Which creates the following /tmp/generated_crontab in the container:

*/5 * * * * sudo -E -u www-data -- bash -c 'php bin/console sw:cron:run'

After some days, my monitoring system complained about the increasing number of threads my server is using. I figured out, that these threads are related to zombie processes, which are obviously created by the containers Cron system. So if I check the processes from inside the container via

ps -ef

I get a lot of these entries:

root       75536       1  0 15:34 ?        00:00:00 [sudo] <defunct>
root       75581       1  0 15:39 ?        00:00:00 [sudo] <defunct>
root       75597       1  0 15:44 ?        00:00:00 [sudo] <defunct>
root       75611       1  0 15:49 ?        00:00:00 [sudo] <defunct>
root       75625       1  0 15:54 ?        00:00:00 [sudo] <defunct>

(note the 5 minutes difference which is exactly the cron frequency).

So to me it looks like the process started by sudo -E -u www-data -- bash -c 'php bin/console sw:cron:run' is never really properly closed. Killing them manually is not possible because we'd need to kill the parent process, which is the containers root process with pid 1.

Any ideas? Thanks for your help!

Possible Solution

Did not find a workaround to get rid of all the defunct processes, yet. Think the only way would be to restart the container on a regular basis.

Your Environment

mistraloz commented 9 months ago

We had exactly the same issue with laravel tasks with background mode activated. Without the background mode, all work properly.

If i well understood, Shopware use a symfony stack and there is no background feature by default (or i don't know it). Maybe Shopware added it ?

BTW, to fix it I'm working on a feature to have a proper deamon supervisor who can help the daemon killer to work better for v5. Until that i recommand to look about a feature related to a background mode. If nothing work, let's me know (i'm curious about this defect) but for instance i don't have any better solution than restart the container.

R0Wi commented 9 months ago

Thanks for your feedback @mistraloz ! Well, I'm also not too deeply involved in the implementation details of Shopware 5 but I can definitely check if they're using the Symfony Background feature.

Since we're planning to upgrade to Shopware 6 within the next year, I'm not sure if it's worth spending too much time on this. It's rather a consideration if you expect more applications running in your container environment which are using that Symfony feature.

For the time being I just restart the container on a regular basis. Thank you for your help and I will keep you posted here if I have further information!