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

@pctablet505
pctablet505 marked this pull request as ready for review July 17, 2026 12:25
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 force-pushed the fix-session-created-timestamp branch from a8defd9 to 04fade4 Compare August 21, 2026 15:13
@greptile-apps

greptile-apps Bot commented Aug 21, 2026

Copy link
Copy Markdown

Confidence Score: 4/5

The PostgreSQL storage compatibility issue should be fixed before merging because active sessions can expire according to creation time rather than last activity.

The new timeout behavior depends on persisting last_visit, while the bundled PostgreSQL storage example drops that field and reloads sessions with only the now-immutable creation timestamp.

Files Needing Attention: aiohttp_session/init.py and examples/postgres_storage.py

Reviews (1): Last reviewed commit: "Strengthen created-mutation test and add..." | Re-trigger Greptile

Comment on lines +260 to +263
return {
"created": session.created,
"last_visit": session._last_visit,
"session": session._mapping,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 PostgreSQL loses activity timestamps

When an application uses the bundled PostgreSQL storage example with max_age, this new field is discarded because that storage persists only session and the now-immutable created value. Reloading therefore falls back to the original creation time, causing actively mutated sessions to expire once max_age has elapsed since creation.

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