Tag: «debian»
A Note on PHP Sessions and the Garbage Collector
I ran into an unpleasant issue on one of the work servers here.
Historically, when the site's PHP engine starts up, this value gets set:
session_save_path( PATH_TMP );
ini_set( 'session.gc_maxlifetime', 1800 );
In other words, we have a custom directory for storing sessions, and its lifetime is set to half an hour for the garbage collector.
The server uses php-fpm. I noticed problems in the logs - as if the disk had run out of space. Even though there was still plenty of it. My first thought was that somewhere a huge number of tiny files had been created. As it turned out, session files in our custom directory were not being deleted. I started digging and found the session.gc_probability parameter on php.net, which in php.ini is supposed to be set to 1 - this is the probability that the garbage collector will also run when a script executes.
In the user notes there is a note that Debian sets this parameter to 0. Googling says this is related to the permissions on the default folder /var/lib/php5, which do not allow PHP's garbage collection to clean up old files there. So Debian disables PHP's garbage collector and apparently runs some cron job of its own as root for cleanup. It seems to look for files in the standard directory, and since that does not match our PATH_TMP directory, the sessions were not being removed.
That's how it goes. The options are either to set up your own cron job for cleanup, for example:
0,30 * * * * find /path/to/tmp -mmin +30 -exec rm {} \;
Or specify the default folder in php.ini. But, for example, if a site has 2 projects that use different temporary folders for storing sessions, then you will have to configure something for both of them.
Or add this line when the script starts: ini_set('session.gc_probability', 1);
Personally, I like having my own cron job more. Although maybe adding one more line to the script is better, since it could save you from this problem in the future. Still, cron somehow feels more reliable. Now I just have to figure out how to delete the files that have piled up over 6 months. Midnight Commander spent about 2 hours scanning the folder. When the counter went past 49 million, I gave up on the scan, started deletion, and went to sleep.
That's that.