Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #4795 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

ARs in the session have their associations persisted as well

Reported by: andylien@gmail.com Assigned to: bitsweat
Priority: normal Milestone: 1.2
Component: ActionPack Version: edge
Severity: normal Keywords: session assocations tiny patch
Cc:

Description

I just upgraded to Rails 1.1.2. I store an AR for a user in the session on disk-- @session[:user] which is of class User < ActiveRecord::Base. Before the upgrade all associated objects (@session[:user].friends, @session[:user].posts) were cleared between requests. Now the sessions are keeping these associations persistent, and the session files are getting quite big (100K). I wanted to post this ticket for now to see if others had this problem. I'll continue investigating and updating here.

BTW, my workaround is to add an after filter onto the application controller that calls @session[:user].clear_association_cache.

Attachments

4795.patch (0.6 kB) - added by sd@notso.net on 05/03/06 14:26:33.
test-4795.patch (3.0 kB) - added by sd on 11/08/06 19:23:04.
Test this bug
clear_persistent_in_all_stores.patch (1.8 kB) - added by sd on 11/08/06 19:24:04.
Additional patch needed to test the original fix. Also provides "extra" features
fix_clear_persistent_in_ar_stores.patch (0.6 kB) - added by sd on 11/15/06 19:41:07.

Change History

04/19/06 07:14:18 changed by skaes

This shouldn't happen. Which session store is used by your app?

04/19/06 13:55:50 changed by andylien@gmail.com

I am using PStore.

04/23/06 15:10:12 changed by tom@smallroomsoftware.com

I'm experiencing the same problem. I'm just using the default filesystem session storage.

I found this new comment in session_management.rb in the latest active record gem (1.12.1):

# Clear cached associations in session data so they don't overflow
# the database field.  Only applies to ActiveRecordStore since there
# is not a standard way to iterate over session data.
def clear_persistent_model_associations

If associations are really not being cleared for anything but AR stored sessions, I'm amazed more people aren't reporting problems (with stale data in their sessions).

I'm going to try switching to AR storage.

04/23/06 17:00:20 changed by anonymous

Switching to the ActiveRecordStore didn't help much. Actually, the method I was looking at in session_management.rb hasn't really changed and the clearing should work for all storage implementations. I added some logging and confirmed that it was working. At that point, I can look at the session data, follow the references, see the sql running in the logs and everything looks fine. By the time my controller is run and my templates rendered, I have the old state data so something wierd is happening. Your workaround, however, works for me and fixes the problem. It's all very strange. Are you using the login_engine? Maybe it has something to do with that.

04/24/06 16:19:56 changed by andylien@gmail.com

I am using a highly modified version of the salted-hash login generator, with localization completely stripped out. I think login_engine is the engine version of that? (If I had to do it again I would've used acts as verified). Thanks for the lead. I haven't had much time to work on this, but I should be able to look at it sometime this week.

05/03/06 14:26:05 changed by sd@notso.net

The problem seems to be in process_cleanup_with_session_management_support, where clear_persistent_model_associations is actually called.

For some reason, it's called after the session is saved, instead of before, where it would actually make a difference.

Patch is being uploaded.

05/03/06 14:26:33 changed by sd@notso.net

  • attachment 4795.patch added.

07/08/06 00:09:30 changed by bitsweat

  • status changed from new to closed.
  • resolution set to wontfix.

Please reopen with a patch and unit tests if this remains an issue.

11/08/06 19:22:43 changed by sd

  • status changed from closed to reopened.
  • resolution deleted.

Let's try this again. I'm attaching two files:

The first one adds a test that fails because of the discussed in this ticket. The patch also modifies TestSession in order to make it functional enough to mimic the behaviour we're looking for.

The second file one adds support for clear_persistent_model_associations in all session stores (by not looking for @data but rather using a 'data' method), including the TestSession (not really a 'session store', but you get the idea). This patch is needed for the original patch to pass the tests. Otherwise, only ARSessionStore sessions can pass the test.

So, apply test-4795.patch, run tests and see how they fail. Then apply 4795.patch and clear_persistent_in_all_stores.patch. Run tests. See how they run.

11/08/06 19:23:04 changed by sd

  • attachment test-4795.patch added.

Test this bug

11/08/06 19:24:04 changed by sd

  • attachment clear_persistent_in_all_stores.patch added.

Additional patch needed to test the original fix. Also provides "extra" features

11/08/06 19:25:48 changed by sd

  • keywords changed from session assocations to session assocations tiny patch.

11/13/06 18:46:03 changed by bitsweat

  • owner changed from David to bitsweat.
  • status changed from reopened to new.
  • milestone changed from 1.1 to 1.2.

Good work. I have one test failure:

  1) Failure:
test_process_cleanup_with_session_management_support(SessionManagementTest) [./test/controller/session_management_test.rb:143]:
<"does not have cached associations"> expected but was
<"has cached associations">.

11/13/06 18:55:47 changed by bitsweat

Just needed TestSession#data.

11/13/06 18:59:02 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [5512]) Always clear model associations from session. Closes #4795.

11/15/06 19:40:53 changed by sd

  • status changed from closed to reopened.
  • resolution deleted.

The extra features in my original patch, actually needed for the test itself, broke the original feature of clearing the caches on active_record based sessions.

The feature enabled the "clearing" on all other session stores, but it actually broke it on AR stores.

This additional patch (fix_clear_persistent_in_ar_stores.patch) fixes that problem.

11/15/06 19:41:07 changed by sd

  • attachment fix_clear_persistent_in_ar_stores.patch added.

11/15/06 20:00:56 changed by bitsweat

  • version set to edge.

Thanks!

11/15/06 20:08:44 changed by bitsweat

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [5531]) ARStore needs a data reader method. Closes #4795.