activerecord-session_store icon indicating copy to clipboard operation
activerecord-session_store copied to clipboard

Title: 2.3.0 drops in-place nested session writes (changed? misses mutations)

Open yaakov-sanders opened this issue 4 months ago • 9 comments

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

yaakov-sanders avatar Jul 16 '26 16:07 yaakov-sanders

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.

mikejamesthompson avatar Jul 26 '26 06:07 mikejamesthompson

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

oleksii-leonov avatar Jul 27 '26 09:07 oleksii-leonov

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've run into this problem as well. ((

Viktor-Ivliev avatar Jul 29 '26 12:07 Viktor-Ivliev

We are using Timeoutable and ran into this as well. Confirmed by pinning down to 2.2.0, the issue went away.

albertski avatar Jul 29 '26 20:07 albertski

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? 🙏

decasia avatar Aug 05 '26 20:08 decasia

I have this fixed in https://github.com/rails/activerecord-session_store/pull/238.

albertski avatar Aug 06 '26 20:08 albertski

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.

jlw avatar Aug 07 '26 14:08 jlw

Any news on this? It would be great to get this fixed.

etrejos avatar Sep 29 '26 01:09 etrejos

@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

Viktor-Ivliev avatar Sep 29 '26 17:09 Viktor-Ivliev