Skip to content

Sessions

The session mapping and the server-side stores that back it.

Session

Bases: dict[str, Any]

The request session - a dict that knows when it has changed.

permanent property writable

permanent: bool

Whether the session cookie should use the longer lifetime.

Backed by the reserved _permanent key, so the flag persists in the cookie across requests and toggling it counts as a session mutation.

regenerate_id

regenerate_id() -> None

Request a fresh server-side session id on the next response.

Call this at a privilege boundary - login, role change - so an attacker-planted id cannot be replayed against the now-elevated session: the session-fixation defence. Harmless with cookie-only sessions.

clear

clear() -> None

Remove every key and mark the session modified.

pop

pop(key: Any, *default: Any) -> Any

Remove key and return its value, marking the session modified on removal.

popitem

popitem() -> Any

Remove and return the last (key, value) pair, marking the session modified.

setdefault

setdefault(key: Any, default: Any = None) -> Any

Insert default under key when absent, marking the session modified on insert.

update

update(*args: Any, **kwargs: Any) -> None

Merge keys into the session, marking it modified unless the input is empty.

SessionStore

Server-side session backend interface.

A concrete store persists session payloads keyed by an opaque session id; ServerSessionMiddleware drives it. The methods are async so a network-backed store can implement them without blocking the event loop.

read async

read(session_id: str) -> dict[str, Any] | None

Return the stored payload for session_id, or None.

None when the session is absent, expired, or has been revoked.

write async

write(session_id: str, data: dict[str, Any], max_age: int) -> None

Persist data under session_id, to expire after max_age seconds.

delete async

delete(session_id: str) -> None

Revoke session_id - a later read of it must return None.

replace async

replace(session_id: str, data: dict[str, Any], max_age: int) -> bool

Write data for session_id only if it still exists.

False when the id is absent - revoked or expired. This is the race-safe write, so a request in flight cannot resurrect a session a concurrent delete removed. The default is a non-atomic read-then-write; a store with an atomic conditional write should override it.

touch async

touch(session_id: str, max_age: int) -> bool

Extend the expiry of an existing entry without rewriting its payload.

False when the id was absent (revoked or expired). The default reads then rewrites the payload; a store with a native TTL-refresh primitive should override it.

InMemorySessionStore

Bases: SessionStore

A process-local SessionStore - a dict with per-entry expiry.

It does not share state across workers, so a multi-worker deployment needs a shared backend implementing the SessionStore interface.

Sized, iterable and containment-testable, so it is falsy when it holds no live session. Test store is not None to ask whether one is configured.

read async

read(session_id: str) -> dict[str, Any] | None

Return a copy of the stored payload, or None when absent or expired.

write async

write(session_id: str, data: dict[str, Any], max_age: int) -> None

Store a copy of data under session_id, expiring after max_age seconds.

delete async

delete(session_id: str) -> None

Drop session_id from the store. No-op if not present.

replace async

replace(session_id: str, data: dict[str, Any], max_age: int) -> bool

Write data only when session_id still exists and is unexpired.

touch async

touch(session_id: str, max_age: int) -> bool

Refresh the expiry of an existing, unexpired entry without copying its payload.

sweep_expired

sweep_expired() -> int

Drop every expired entry and return how many were removed.

For deterministic eviction, instead of the probabilistic sweep that fires from write / replace.

clear

clear() -> int

Drop every session and return how many were removed.

The sync counterpart to delete for the whole store. Expired entries no sweep has reached count as removed too.

expires_at

expires_at(session_id: str) -> float | None

Return when session_id expires as a Unix timestamp, or None.

None means absent or already past its expiry, matching what read would say. This is what sliding expiry is observable through.