Skip to content

feat(session): record session expiry in storage via ExpiringSessionStorage - #436

Merged
usernane merged 6 commits into
mainfrom
dev
Nov 17, 2026
Merged

usernane merged 6 commits into
mainfrom
dev

Conversation

@usernane

@usernane usernane commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Session garbage collection now removes each session by its own expiry time, instead of after 4 idle hours. Storages record the expiry through a new optional ExpiringSessionStorage interface, which all built-in drivers implement. The PR also fixes three smaller session lifetime bugs.

Motivation

GC used one global idle threshold (4 hours) and never read a session's lifetime. Long-lived sessions, such as a 7 day "remember me" session, were deleted after 4 idle hours (#432). Sessions that were only read looked idle, because resuming them didn't update their last-use time (#433). Three more bugs broke lifetimes: setDuration()/setIsRefresh() weren't saved (#431), SESSION_GC was read as a timestamp (#434), and a missing lifeTime fell back to 2 minutes (#435).

The design is recorded in ADR-0061.

User docs are updated in WebFiori/docs@7da9d3e. That commit changes Sessions Management (GC, custom storages, cache TTL, migration step) and the SESSION_GC row in env-vars.md.

Changes

How to Test / Verify

Unit and integration tests run against every built-in driver: default (file), in-memory, cache, SQLite, MySQL 8 and SQL Server 2022.

MYSQL_PORT=3307 SQL_SERVER_HOST='127.0.0.1,1435' vendor/bin/phpunit -c tests/phpunit.xml tests/WebFiori/Framework/Tests/Session

Result: 264 tests pass and 1 is skipped on purpose (the cache driver has no GC batch limit). Unreachable databases are skipped once per run instead of timing out on every test.

The full suite has 55 failures, all in tests/.../Cli. Those tests hardcode MySQL port 3306 and failed with "Access denied" against an unrelated local server. They don't touch session code.

Code style: php-cs-fixer (php_cs.php.dist) was applied to the 15 files this PR changes, in a separate style(session) commit with no behavior change. Most of that commit is the fixer's method ordering, so review the feat commit for the logic. It wasn't run on the whole repo, where 182 of 339 files already differ from the fixer's output.

Breaking Changes and Migration Steps

No public API is removed or changed. ExpiringSessionStorage is optional, and custom SessionStorage implementations keep the 3.1 behavior.

Behavior changes to be aware of:

  • SESSION_GC is now a duration in seconds. It used to be an absolute Unix timestamp. A static timestamp could never give a moving threshold, so we don't expect anyone to rely on the old meaning. If you set it, change it to the idle period you want, for example 604800 for one week.
  • Sessions live their full lifetime. Long sessions are no longer cut at 4 idle hours, so session storage holds more rows than before.

Migration steps for database session storage:

  1. Run SessionSchemaMigration::run('sessions-connection') once after deploying. It adds sessions.expires_at and its index.
  2. Until it has run, GC uses the idle threshold exactly as in 3.1. Existing sessions get an expiry the next time they are resumed.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed) Docs Repo
  • I ran lint/cs-fixer (if applicable) (composer fix-cs)
  • I considered backward compatibility
  • I considered security

Related issues

Closes #431
Closes #432
Closes #433
Closes #434
Closes #435

Related: #418 (already fixed in v3.1.1; this PR adds per-driver regression tests)

…#418)

Adds SessionDriversTrait, which builds every built-in storage driver (file, in-memory, cache, SQLite, MySQL, MSSQL) for driver-parametrized tests. Database drivers are skipped once per run when unreachable, and pooled connections are closed after each test.

SessionUserPersistenceTest checks that setUser() writes the reserved _user key and that the user is restored on resume, on every driver.
…orage

Session now computes each session's expiry (startedAt + duration, or now + duration with refresh) and hands it to storages that implement the new optional ExpiringSessionStorage interface. GC removes sessions whose own expiry has passed, and falls back to the idle threshold for sessions without one. All built-in drivers implement it:

- DatabaseSessionStorage: nullable sessions.expires_at column with an index. SessionSchemaMigration::run() adds both. Unmigrated databases keep the old behavior.
- DefaultSessionStorage: <id>.exp sidecar file.
- CacheSessionStorage: entry TTLs follow the remaining lifetime instead of a fixed 7200 seconds.
- InMemorySessionStorage: expiry map; gc() now removes expired sessions.

Also fixes:
- setDuration() and setIsRefresh() persist _meta on a running session (#431).
- Resuming a refresh session extends its stored expiry, at most once a minute (#433).
- SESSION_GC is read as an idle period in seconds, not a Unix timestamp (#434).
- A missing lifeTime in _meta falls back to 120 minutes, not 2 (#435).
- SessionSchemaMigration called a non-existent createIfNotExists() and used ADD COLUMN, which SQL Server rejects.

See ADR-0061 in webfiori/docs.

Fixes #431
Fixes #432
Fixes #433
Fixes #434
Fixes #435
No behavior change. Only the files touched by this work are formatted; most of the change is the fixer's method ordering.
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.50617% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.25%. Comparing base (00c9d94) to head (c6a11f9).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
WebFiori/Framework/Session/SessionDB.php 75.55% 11 Missing ⚠️
WebFiori/Framework/Session/Session.php 92.30% 3 Missing ⚠️
...bFiori/Framework/Session/DefaultSessionStorage.php 93.93% 2 Missing ⚠️
WebFiori/Framework/Session/SessionManager.php 83.33% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #436      +/-   ##
============================================
+ Coverage     81.99%   82.25%   +0.26%     
- Complexity     3301     3355      +54     
============================================
  Files           106      106              
  Lines          9752     9868     +116     
============================================
+ Hits           7996     8117     +121     
+ Misses         1756     1751       -5     
Flag Coverage Δ
php-8.1 81.02% <86.66%> (+0.17%) ⬆️
php-8.2 81.01% <86.66%> (+0.17%) ⬆️
php-8.3 89.39% <89.50%> (+0.21%) ⬆️
php-8.4 89.39% <89.50%> (+0.21%) ⬆️
php-8.5 89.27% <89.50%> (+0.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

PHPUnit 12 (used by the PHP 8.5 CI job) ignores @dataProvider annotations, so the driver-parametrized tests ran without arguments. Attributes work on PHPUnit 10 and 12.
CI runs PHPUnit 9.5 (PHP 8.1, 8.2), 10 (PHP 8.3, 8.4) and 12 (PHP 8.5). PHPUnit 9 only reads @dataProvider annotations and PHPUnit 12 only reads #[DataProvider] attributes, so declare both.
ubuntu-latest moved to Ubuntu 26.04, which no longer provides mysql-client-core-8.0, so every PHP job failed in 'Setup MySQL Client' before running tests.
@sonarqubecloud

Copy link
Copy Markdown

@usernane
usernane merged commit d4cebc3 into main Nov 17, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment