Fix in-place session data mutations being silently dropped - #238
Open
albertski wants to merge 1 commit into
Open
Fix in-place session data mutations being silently dropped#238albertski wants to merge 1 commit into
albertski wants to merge 1 commit into
Conversation
When session data is mutated, the change detection in data= compared against the already-mutated @DaTa object, so changed? returned false and the save was skipped.
This was referenced Aug 2, 2026
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.
Fixes: #236
Since 2.3.0, in-place mutations to nested session data are silently lost:
The 2.3.0 optimization added change detection in
data=by comparing the incoming value againstself.data. The problem is thatget_sessionreturns the raw@dataobject reference. When user code mutates it in-place,@datais also mutated. By the timewrite_sessioncallsrecord.data = session_data, both sides of the comparison point to the same already-mutated object, so changed? returns false and the save is skipped.How I experienced the issue
We use Devise's
Timeoutablemodule, which updateslast_request_aton the session on every request. The bug meant this updated timestamp was never actually saved to the database and each write was silently skipped.Devise would read back the stale
last_request_atvalue from when the user first logged in. Since that timestamp was always older than the timeout window, users would get logged out regardless of whether they had been actively using the app.Instead of timing out after a period of inactivity, the session would expire at a fixed point after login.
Having said all that, the intention of the optimization is valid. I created heartcombo/devise#5857 to have apps using
:timeoutableto take advantage of the optimization.Fix
When session data is first lazily deserialized from the database, store a deep copy (
@data_snapshotviaMarshal.load(Marshal.dump(@data))). Thedata= setterthen compares against this snapshot instead of the live@datareference, correctly detecting in-place mutations.