From 4d4de151e9a0edd86b8ce5b055e56a5784d5c08f Mon Sep 17 00:00:00 2001 From: Eugene Nikanorov Date: Mon, 23 Feb 2015 13:29:08 +0300 Subject: [PATCH] Avoid DetachedInstanceError after session rollback In some cases this exception is thrown while accessing Agent object from logging statement after a transaction was rolled back. There is a unit test that covers thsi code patch, but the issue is not reproducible with sqlite. Just avoid accessing db object after session had been closed. Change-Id: Iff6b72156b08f177bd0c71f6ba93d3bf46c82fa4 Closes-Bug: #1424578 --- neutron/scheduler/dhcp_agent_scheduler.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/neutron/scheduler/dhcp_agent_scheduler.py b/neutron/scheduler/dhcp_agent_scheduler.py index 67f0aca7136..09afabfa590 100644 --- a/neutron/scheduler/dhcp_agent_scheduler.py +++ b/neutron/scheduler/dhcp_agent_scheduler.py @@ -38,10 +38,13 @@ class ChanceScheduler(object): def _schedule_bind_network(self, context, agents, network_id): for agent in agents: context.session.begin(subtransactions=True) + # saving agent_id to use it after rollback to avoid + # DetachedInstanceError + agent_id = agent.id + binding = agentschedulers_db.NetworkDhcpAgentBinding() + binding.dhcp_agent_id = agent_id + binding.network_id = network_id try: - binding = agentschedulers_db.NetworkDhcpAgentBinding() - binding.dhcp_agent = agent - binding.network_id = network_id context.session.add(binding) # try to actually write the changes and catch integrity # DBDuplicateEntry @@ -49,11 +52,11 @@ class ChanceScheduler(object): except db_exc.DBDuplicateEntry: # it's totally ok, someone just did our job! context.session.rollback() - LOG.info(_LI('Agent %s already present'), agent) + LOG.info(_LI('Agent %s already present'), agent_id) LOG.debug('Network %(network_id)s is scheduled to be ' 'hosted by DHCP agent %(agent_id)s', {'network_id': network_id, - 'agent_id': agent}) + 'agent_id': agent_id}) def schedule(self, plugin, context, network): """Schedule the network to active DHCP agent(s).