fix(sessions): normalize session_id before the duplicate check in InMemorySessionService - #6892
Open
MUHAMMEDHAFEEZ wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
…emorySessionService _create_session_impl() checked for an existing session using the raw session_id, then only stripped it afterward before using it as the storage key. A caller-supplied id that differs from an existing one only by surrounding whitespace passed the duplicate check and then silently overwrote the existing session's events and state, returning a normal Session with no error. sqlite_session_service already strips before checking; this brings InMemorySessionService in line with it. Fixes google#6887
MUHAMMEDHAFEEZ
force-pushed
the
fix/in-memory-session-strip-before-duplicate-check
branch
from
August 25, 2026 02:19
7665cd0 to
69ef8a3
Compare
MUHAMMEDHAFEEZ
marked this pull request as ready for review
August 25, 2026 02:25
Author
|
Hi @DeanChensj , just checking in on this PR. It’s ready for review and all required checks are currently passing. Whenever you have a chance, I’d appreciate your feedback. Happy to make any changes you think are needed. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
InMemorySessionService._create_session_impl()checks whether a session alreadyexists using the raw
session_id, and only strips whitespace from itafterward, right before using it as the storage key. A client-supplied id
that differs from an existing one only by surrounding whitespace (e.g. a
trailing
"\n"picked up from a file, CSV column, or env var) thereforemisses the duplicate check and silently overwrites the existing session,
wiping its events and state, with no exception and no log line.
sqlite_session_servicealready strips the id before running itsduplicate check, so this is an inconsistency between backends rather than
intended behavior.
Solution:
Move the
strip()to the top of_create_session_impl, before theduplicate-id check, and simplify the later normalization (which no longer
needs to re-strip) to just falling back to a generated id when blank. Net
change is -3 lines.
Testing Plan
Unit Tests:
Added two tests to
tests/unittests/sessions/test_session_service.py,instantiating
InMemorySessionServicedirectly (this is backend-specificbehavior, not something the shared cross-backend
session_servicefixtureshould assert, since
database/redisnormalize differently and that's aseparate, larger discussion):
test_create_session_with_padded_duplicate_id_raises_error— creates asession, then asserts that creating again with the same id padded in
whitespace raises
AlreadyExistsErrorinstead of clobbering the originalsession's state.
test_create_session_with_blank_id_generates_one— asserts awhitespace-only id is treated as no id (still generates one), guarding
the simplified fallback logic.
I confirmed the first test fails on the unfixed code (
DID NOT RAISE AlreadyExistsError) and passes after the fix, so it actually exercises thebug rather than passing vacuously.
The 2 xfailed are pre-existing, documented Redis divergences unrelated to
this change.
Also ran
pyink --check,ruff check,isort --check,codespell, andmypy --stricton both changed files — all clean.Manual End-to-End (E2E) Tests:
Ran the reproduction from the issue directly against
InMemorySessionService:Before the fix:
(the second
create_sessioncall silently destroyed the first session's2 events and its
cartstate)After the fix:
Checklist
Additional context
Scope note: the issue also flagged that
databaseandredisbackendsnormalize
session_iddifferently (or not at all), which is a separate,larger behavioral question across
BaseSessionServiceimplementations.This PR intentionally only fixes the
in_memoryinconsistency againstsqlite's existing (correct) behavior, and leaves the cross-backendnormalization contract as a follow-up.