Title: 2.3.0 drops in-place nested session writes (changed? misses mutations)
Summary
2.3.0's #166 optimization makes write_session skip the save when record.changed? is false — but Session#data= can't detect in-place mutation, so nested session writes are silently dropped. 2.2.0 always saved, so this worked.
Cause
#active_record_store.rb (2.3.0)
record.data = session_data
return sid unless record.changed? || record.new_record? # new
#session.rb
def data=(data)
attribute_will_change!(@@data_column_name) if data != self.data
@data = data
end
get_session hands Rack the record's own @data object. When an app mutates a nested value in place (session[:foo][k] << v), the
change bleeds into @data, so at write time session_data deep-equals self.data, changed? is false, and the save is skipped.
Repro
record = ActiveRecord::SessionStore::Session.find_by_session_id("abc")
data = record.data # {"key" => ["a"]}
data["key"] << "b" # in-place
record.data = data
record.changed? # => false on 2.3.0; save is skipped
Suggested fix
Snapshot the deserialized data (deep copy) at read time and diff against that at write, instead of comparing against the
already-mutated @data.
Env: 2.3.0 (regression vs 2.2.0), Rails 8.1, :active_record_store
This issue is affecting a Rails application I work on at NHS England.
We use Devise Timeoutable to expire inactive sessions. However, the timeoutable hook mutates the last_request_at field on the session to track session activity.
With this change in 2.3.0, this mechanism for tracking session activity with Devise is broken and we will most likely have to pin at 2.2.0 until this is fixed.
We did a monkeypatch in the initialiazer to fix it:
# config/initializers/active_record_session_store.rb
ActiveRecord::SessionStore::Session.class_eval do
def data=(data)
attribute_will_change!("data") if data != self.class.deserialize(read_attribute("data"))
@data = data
end
end
This issue is affecting a Rails application I work on at NHS England.
We use Devise Timeoutable to expire inactive sessions. However, the timeoutable hook mutates the
last_request_atfield on the session to track session activity.With this change in 2.3.0, this mechanism for tracking session activity with Devise is broken and we will most likely have to pin at 2.2.0 until this is fixed.
We've run into this problem as well. ((
We are using Timeoutable and ran into this as well. Confirmed by pinning down to 2.2.0, the issue went away.
Yeah, it would be really good to get a fix out for this, because in its current state, v2.3.0 can break some core session management flows (anything that updates nested session token data will break unexpectedly because of this issue).
@rafaelfranca any chance you can help get a bugfix merged and released? 🙏
I have this fixed in https://github.com/rails/activerecord-session_store/pull/238.
We ran into this bug with a pattern for storing a user's current filter/sort/page state in separate session hashes where the key of each hash is the controller name.
Any news on this? It would be great to get this fixed.
@rafaelfranca Sorry for tagging you here, but this affects a lot of people, and it seems like there isn't an answer here.
Even there is a pull https://github.com/rails/activerecord-session_store/pull/238