Skip to content

Stop cookieless requests from growing server-side sessions - #1455

Merged
crimson-knight merged 3 commits into
v2-devfrom
fix/cookieless-session-growth
Nov 20, 2026
Merged

crimson-knight merged 3 commits into
v2-devfrom
fix/cookieless-session-growth

Conversation

@crimson-knight

@crimson-knight crimson-knight commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

What was wrong

A :web route served to clients that never send the session cookie back (load generators, crawlers, clients that ignore cookies) grew the process without limit. Measured on a release build of an Amber CLI 2.0.11 app at 32 connections, the beta.10 :web route shape reached 441 MiB after 30 s and 858 MiB after 90 s, with 302 and 692 MiB still in use after a forced collection: about 413 bytes per new visitor.

Three defects combined:

  1. Amber::Pipe::Flash wrote session["_flash"] = "{}" on every response, even when the flash was empty, so every cookieless request created a session.
  2. Session::Store#build ignored session.store and always built the memory adapter session. store: signed_cookie, the default, still kept every session on the server.
  3. MemorySessionAdapter had no size limit and kept entries for two weeks (the generated expires).

What changes

  • Pipe::Flash writes the session only when the flash has messages, or when it must clear a flash the session still holds. A request that never sets a flash leaves the session unchanged.
  • Session::Store#build honors session.store. signed_cookie and encrypted_cookie build a CookieStore and keep 0 bytes on the server. adapter (now a valid value, as the generated YAML already documents) and redis use the configured session.adapter.
  • MemorySessionAdapter keeps at most max_sessions sessions and drops the session used longest ago when it is full. Reading, writing or extending a session counts as using it. The limit is the typed setting session.max_memory_sessions (default 10,000, AMBER_SESSION_MAX_MEMORY_SESSIONS overrides it), read once when the memory adapter is first built; validate! rejects a value below 1.

Behavior change: apps on store: signed_cookie or encrypted_cookie now get cookie sessions, as configured. An app that depends on server-side sessions (for example regenerate_session!, which is a no-op for cookie sessions) sets store: adapter. CHANGELOG and the configuration guide say so.

Specs (each fails on v2-dev at f987f1b)

  • spec/amber/pipes/flash_spec.cr: for signed_cookie, encrypted_cookie and adapter, an empty flash sends no Set-Cookie and stores no session; a flash message reaches the next request and is cleared once read.
  • spec/amber/router/session_spec.cr: each store value builds the right store; a signed_cookie or encrypted_cookie app round-trips a session value through the cookie while the memory adapter's size stays unchanged. The old "creates an adapter session store by default" example asserted the defect and is replaced.
  • spec/amber/adapters/memory_session_adapter_spec.cr: the adapter never exceeds its limit, evicts the least recently used session first, and does not evict when an existing session is updated at the limit.
  • spec/amber/configuration/session_config_spec.cr, env_override_spec.cr: the new setting, its YAML key, its override and its validation.
  • Regression check spec/amber/pipes/cookieless_web_requests_spec.cr: serves 100,000 cookieless GET requests to a :web route through Session, Flash and CSRF with store: adapter and the two-week expiry, and asserts no Set-Cookie, no stored session, and retained heap growth (after two forced collections) below 8 MiB. Before the fix the same run stores 100,000 sessions, about 40 MiB.

Testing

CI is green on all jobs (3,074 examples, 0 failures on Crystal 1.22.1 ubuntu).

Load test with the diagnosis harness: an Amber CLI 2.0.11 app, release build (crystal-alpha 1.21.0), the beta.10 :web pipeline (Logger, Session, Flash, CSRF) around a full render of GET /, 32 connections, cookieless oha load, gc_initial_heap_mib: 0.

Amber Run Req/s Peak RSS Live after forced GC
v2.0.0-beta.11 (f987f1b) 30 s 25,904 448.6 MiB 308.7 MiB
v2.0.0-beta.11 (f987f1b) 90 s 21,528 875.1 MiB 709.0 MiB
this PR (3a8fd90), generated store: signed_cookie 30 s 32,782 13.8 MiB 4.1 MiB
this PR (3a8fd90), generated store: signed_cookie 90 s 32,550 14.9 MiB 4.2 MiB
this PR (3a8fd90), AMBER_SESSION_STORE=adapter 30 s 28,985 15.6 MiB 4.2 MiB

Memory no longer grows with the request count, and throughput rises about 27% because no session cookie is encrypted per request.

A companion amber_cli PR corrects the generated session comments: amberframework/amber_cli#59.

Every request through Amber::Pipe::Flash wrote session["_flash"] = "{}",
even with an empty flash, so every request without a session cookie
created a session. Session::Store#build ignored session.store and always
built the memory adapter session, and MemorySessionAdapter had no size
limit, so each such request kept about 413 bytes for two weeks. A :web
route served to clients that never send the cookie back grew from
441 MiB after 30 s to 858 MiB after 90 s.

- Pipe::Flash writes the session only when the flash has messages, or
  when it must clear a flash the session still holds.
- Session::Store#build honors session.store: signed_cookie and
  encrypted_cookie build a CookieStore and keep nothing on the server;
  adapter (newly valid) and redis use the configured session adapter.
- MemorySessionAdapter keeps at most max_sessions sessions and drops the
  session used longest ago. The limit is session.max_memory_sessions
  (default 10,000, AMBER_SESSION_MAX_MEMORY_SESSIONS overrides it), and
  validate! rejects a value below 1.

Specs cover each fix, and a regression spec serves 100,000 cookieless
GET requests to a :web route with store: adapter and asserts that no
session is stored, no Set-Cookie is sent, and retained heap grows by
less than 8 MiB.
A Cookies::Store subclass can override #encrypted, as the spec mocks do,
which widened the return type of Session::Store#cookie_store and failed
to compile. Narrow it with is_a? and raise a ConfigurationError naming
the class when it is not an EncryptedStore.
@crimson-knight
crimson-knight merged commit 814705e into v2-dev Nov 20, 2026
7 checks passed
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.

1 participant