]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed potential memory leak whereby previously pickled objects
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 10 Jul 2009 20:01:56 +0000 (20:01 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 10 Jul 2009 20:01:56 +0000 (20:01 +0000)
placed back in a session would not be fully garbage collected
unless the Session were explicitly closed out.

CHANGES
lib/sqlalchemy/orm/state.py
test/orm/test_session.py

diff --git a/CHANGES b/CHANGES
index 16b21f634ad2b77204fefe781e9ae26ce6f0332b..0d16bcf8483748a962c4811b11e2529387817990 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -33,6 +33,10 @@ CHANGES
 
     - Fixed bug whereby session.is_modified() would raise an exception
       if any synonyms were in use.
+    
+    - Fixed potential memory leak whereby previously pickled objects
+      placed back in a session would not be fully garbage collected
+      unless the Session were explicitly closed out.
       
     - Fixed Query being able to join() from individual columns of
       a joined-table subclass entity, i.e.
index 1b7b3fbd54d23e307be2ac8305400cb395afc480..10a0f43eebaff6e56c65913697e39b6ca898d676 100644 (file)
@@ -16,6 +16,7 @@ class InstanceState(object):
     load_path = ()
     insert_order = None
     mutable_dict = None
+    _strong_obj = None
     
     def __init__(self, obj, manager):
         self.class_ = obj.__class__
@@ -139,7 +140,7 @@ class InstanceState(object):
         return d
         
     def __setstate__(self, state):
-        self.obj = weakref.ref(state['instance'])
+        self.obj = weakref.ref(state['instance'], self._cleanup)
         self.class_ = state['instance'].__class__
         self.manager = manager_of_class(self.class_)
 
@@ -150,6 +151,9 @@ class InstanceState(object):
         self.expired = state.get('expired', False)
         self.callables = state.get('callables', {})
         
+        if self.modified:
+            self._strong_obj = state['instance']
+            
         self.__dict__.update(
             (k, state[k]) for k in (
                 'key', 'load_options', 'expired_attributes', 'mutable_dict'
@@ -272,7 +276,8 @@ class InstanceState(object):
                 instance_dict._modified.add(self)
 
         self.modified = True
-        self._strong_obj = self.obj()
+        if not self._strong_obj:
+            self._strong_obj = self.obj()
 
     def commit(self, dict_, keys):
         """Commit attributes.
index a94b4e7df727ab99d4c339583a83e6649877beea..328cbee8ee991dd685ddaeb9b91d1cc226a281ef 100644 (file)
@@ -789,6 +789,36 @@ class SessionTest(_fixtures.FixtureTest):
         user = s.query(User).one()
         assert user.name == 'fred'
         assert s.identity_map
+
+    @testing.resolve_artifact_names
+    def test_weak_ref_pickled(self):
+        s = create_session()
+        mapper(User, users)
+
+        s.add(User(name='ed'))
+        s.flush()
+        assert not s.dirty
+
+        user = s.query(User).one()
+        user.name = 'fred'
+        s.expunge(user)
+
+        u2 = pickle.loads(pickle.dumps(user))
+
+        del user
+        s.add(u2)
+        
+        del u2
+        gc.collect()
+        
+        assert len(s.identity_map) == 1
+        assert len(s.dirty) == 1
+        assert None not in s.dirty
+        s.flush()
+        gc.collect()
+        assert not s.dirty
+        
+        assert not s.identity_map
     
     @testing.resolve_artifact_names
     def test_weakref_with_cycles_o2m(self):