Skip to content

Stop resetting Session.created on mutation - #1219

Open
pctablet505 wants to merge 2 commits into
aio-libs:masterfrom
pctablet505:fix-session-created-timestamp
Open

Stop resetting Session.created on mutation#1219
pctablet505 wants to merge 2 commits into
aio-libs:masterfrom
pctablet505:fix-session-created-timestamp

Conversation

@pctablet505

Copy link
Copy Markdown

What do these changes do?

Session.created is documented as the timestamp of the session's
first access, but __setitem__/__delitem__ were bumping it on
every change, so in practice it reflected the last write instead.

That behavior was added in #671 to fix an idle-timeout bug (max_age
is treated as an inactivity timeout, so an actively used session
shouldn't expire). Simply reverting #671 would bring that expiry bug
back, so instead this tracks last-activity time separately as
last_visit and uses that for the expiry check, leaving created
untouched after the session is first created. Sessions serialized
before this change won't have last_visit, so the expiry check falls
back to created for them, same as before.

Are there changes in behavior for the user?

Session.created now stays fixed for the lifetime of a session,
matching the documentation. The idle-timeout behavior for max_age
is unchanged.

Related issue number

Fixes #981

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

Session.created is documented as the timestamp of the session's first
access, but __setitem__ and __delitem__ were bumping it on every
change, so it actually reflected the last write instead.

That behavior was added to keep idle sessions from expiring early
(max_age is treated as an inactivity timeout), so simply reverting it
would bring back the old expiry bug. Instead, track the last-activity
time separately as last_visit and use that for the expiry check,
leaving created untouched after the session is first created. Older
serialized sessions without last_visit fall back to created for the
expiry calculation, so existing cookies keep working.

Fixes aio-libs#981
test_created_not_modified_on_mutation previously relied on real
wall-clock time, so it happened to pass against the pre-fix code
whenever construction and mutation landed within the same second.
Mock time.time and advance it between steps so the test
deterministically fails if created is ever reset on mutation.

Also add the towncrier changelog fragment for aio-libs#981.
@pctablet505
pctablet505 marked this pull request as ready for review July 17, 2026 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation for Session.created does not match actual behavior

1 participant