The session that lasted 24 minutes, whatever I told it

Title card: The session that lasted 24 minutes, whatever I told it

Everyone was getting logged out. Not at a set time, not on a deploy, just somewhere around the half-hour mark, mid-click, back to the sign-in screen.

I’d set the idle limit myself. Advisors get eight hours. Admins got thirty minutes at that point, which I’d chosen on purpose, so the admins were the ones complaining and I assumed I’d simply got a number wrong. I bumped the admin limit to six hours, deployed, and was told it was still happening. To the advisors as well, who had never had a short limit at all.

That was the clue I should have taken first. The setting I’d changed couldn’t be what was biting, because the people it didn’t apply to were being thrown out too.

Two clocks

In a Symfony app there are really two things that decide when a session ends. One is yours: a listener that looks at the last-used time and ends the session if you’ve been idle too long. That’s application logic, and it was behaving exactly as written.

The other is the session file itself. PHP’s default handler writes one file per session into a folder, and something has to delete the old ones. I’d set gc_maxlifetime to eight hours in the framework config and considered the matter closed.

Here’s the part I hadn’t known. On Ubuntu, PHP’s own garbage collection is switched off (session.gc_probability is 0 in the packaged config). Instead a systemd timer, phpsessionclean, runs every thirty minutes and deletes session files in the default folder that are older than the session.gc_maxlifetime in php.ini. That’s 1440 seconds. Twenty-four minutes.

It reads php.ini. It doesn’t know a framework exists. The runtime value I’d set in the app’s config was invisible to it, because the cleaner is a separate shell script that never starts PHP with my settings.

So: file untouched for 24 minutes, next timer tick removes it, user is signed out. With a tick every 30 minutes, “about half an hour” is exactly what people were reporting. Their activity touches the file, so a busy user survives longer, which is why it felt random.

You can see it for yourself in a few seconds:

systemctl list-timers phpsessionclean.timer
php -r 'echo ini_get("session.gc_maxlifetime"), PHP_EOL;'
ls -l --time=atime /var/lib/php/sessions | head

Fixing it

I thought about editing the php.ini for FPM. That works until the next box is built from the launch script and nobody remembers to do it. The server wouldn’t stay fixed, and I’d have an invisible dependency between the app and the machine it happened to be installed on.

The version I went with keeps everything in the app’s repository config. Point the session files at a folder the cleaner doesn’t know about, and turn PHP’s own collection back on for that folder:

framework:
    session:
        gc_maxlifetime: 28800
        gc_probability: 1
        gc_divisor: 100
        save_path: '%kernel.project_dir%/var/sessions'

Now nothing deletes those files except PHP, using the eight-hour limit I actually meant. About one session start in a hundred does a sweep. The folder is owned by the web user like the rest of var/, and Symfony creates it on first use.

There’s one catch worth writing down, because it will bite a future me. That folder is the sessions. Wiping var/ on deploy, which plenty of deploy scripts do to clear a cache, signs everyone out. Clear var/cache instead.

What I’d check next time

If a session limit doesn’t behave, stop looking at the limit and find out who deletes the files. The answer on a stock Ubuntu box is probably not your application, and not PHP either.

I changed the admin idle time to six hours as well, since the thirty minutes had only ever been a guess. That part was optional. The cleaner was the actual bug, and it had been quietly doing it since the day the app first went onto that server.

Leave a Reply

Your email address will not be published. Required fields are marked *