Skip to content

ext/session: Fix new empty sessions never being stored with lazy_write and strict mode - #25433

Open
jorgsowa wants to merge 1 commit into
php:PHP-8.6from
jorgsowa:fix-session-lazy-write-new-sid-8.6
Open

jorgsowa wants to merge 1 commit into
php:PHP-8.6from
jorgsowa:fix-session-lazy-write-new-sid-8.6

Conversation

@jorgsowa

Copy link
Copy Markdown
Contributor

Since 86b4921, lazy_write calls updateTimestamp() instead of write() for a session that is read as empty and stays empty. That includes a session whose ID was just created.

Many handlers, such as redis and memcached, only refresh existing records in updateTimestamp(). With those handlers, a new empty session is never stored. With session.use_strict_mode=1, which is now the default, the next request's ID fails validateId() and a new ID is issued. The client gets a new session ID on every request until something is written to the session.

Steps, with default settings:

  1. A request without a cookie gets a new ID A. The session is empty, so updateTimestamp(A) is called and A is never stored.
  2. The next request sends A. validateId(A) fails, so a new ID B is issued, and step 1 repeats.

The fix skips the lazy_write comparison when the ID was created in the current request, so a new session is written once, as in 8.5. Existing empty sessions still get updateTimestamp(), so the fix for bug 71162 is kept.

…mode

With lazy_write, an empty new session triggered updateTimestamp() instead
of write(). Handlers whose updateTimestamp() does not create records never
stored it, so strict mode rejected the ID and issued a new one on every
request.
@jorgsowa jorgsowa self-assigned this Nov 12, 2026
@jorgsowa
jorgsowa requested a review from Girgias as a code owner November 12, 2026 19:50
session.cache_limiter=
--FILE--
<?php
class Handler implements SessionHandlerInterface, SessionIdInterface, SessionUpdateTimestampHandlerInterface

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this handler is identical to the one in lazy_write_new_session_id_no_id.phpt. Could merge both cases into a single test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I specifically put them into separate files to have simpler to understand different paths.

@jorgsowa

Copy link
Copy Markdown
Contributor Author

@Girgias , do you also think it's sensible to merge?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants