From 728ce8cc480d0ada690e5a97067cff821b9a65f3 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 3 Oct 2016 12:25:42 -0400 Subject: Ensure strong ref to obj before calling persistent_to_deleted, others Add checks in spots where state.obj() might be late-GC'ed before we get a chance to call the event. There may be more cases of these which we should address as they come up. The Session should always be maintaining strong refs to objects that have pending operations left on them, so for these cases we need to ensure that ref remains long enough for the event to be called. Change-Id: I1a7c7bc57130acc11f54ad55924af2e36ac75101 Fixes: #3808 --- lib/sqlalchemy/orm/state.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'lib/sqlalchemy/orm/state.py') diff --git a/lib/sqlalchemy/orm/state.py b/lib/sqlalchemy/orm/state.py index 2704367f9..519220e34 100644 --- a/lib/sqlalchemy/orm/state.py +++ b/lib/sqlalchemy/orm/state.py @@ -328,13 +328,21 @@ class InstanceState(interfaces.InspectionAttr): if persistent: if to_transient: if persistent_to_transient is not None: - persistent_to_transient(session, state.obj()) + obj = state.obj() + if obj is not None: + persistent_to_transient(session, obj) elif persistent_to_detached is not None: - persistent_to_detached(session, state.obj()) + obj = state.obj() + if obj is not None: + persistent_to_detached(session, obj) elif deleted and deleted_to_detached is not None: - deleted_to_detached(session, state.obj()) + obj = state.obj() + if obj is not None: + deleted_to_detached(session, obj) elif pending and pending_to_transient is not None: - pending_to_transient(session, state.obj()) + obj = state.obj() + if obj is not None: + pending_to_transient(session, obj) state._strong_obj = None -- cgit v1.2.1