diff --git a/neutron/policy.py b/neutron/policy.py index ed680566b7d..19db42364aa 100644 --- a/neutron/policy.py +++ b/neutron/policy.py @@ -17,6 +17,7 @@ import collections import re from oslo_config import cfg +from oslo_db import exception as db_exc from oslo_log import log as logging from oslo_policy import policy from oslo_utils import excutils @@ -257,6 +258,13 @@ class OwnerCheck(policy.Check): target[parent_foreign_key], fields=[parent_field]) target[self.target_field] = data[parent_field] + except exceptions.NotFound as e: + # NOTE(kevinbenton): a NotFound exception can occur if a + # list operation is happening at the same time as one of + # the parents and its children being deleted. So we issue + # a RetryRequest so the API will redo the lookup and the + # problem items will be gone. + raise db_exc.RetryRequest(e) except Exception: with excutils.save_and_reraise_exception(): LOG.exception(_LE('Policy check error while calling %s!'), diff --git a/neutron/tests/unit/test_policy.py b/neutron/tests/unit/test_policy.py index 23ad4d9e7bc..88bf6e2d66c 100644 --- a/neutron/tests/unit/test_policy.py +++ b/neutron/tests/unit/test_policy.py @@ -16,6 +16,7 @@ """Test of Policy Engine For Neutron""" import mock +from oslo_db import exception as db_exc from oslo_policy import fixture as op_fixture from oslo_policy import policy as oslo_policy from oslo_serialization import jsonutils @@ -537,6 +538,18 @@ class NeutronPolicyTestCase(base.BaseTestCase): action, target) + def test_retryrequest_on_notfound(self): + failure = exceptions.NetworkNotFound(net_id='whatever') + action = "create_port:mac" + with mock.patch.object(manager.NeutronManager.get_instance().plugin, + 'get_network', side_effect=failure): + target = {'network_id': 'whatever'} + try: + policy.enforce(self.context, action, target) + self.fail("Did not raise RetryRequest") + except db_exc.RetryRequest as e: + self.assertEqual(failure, e.inner_exc) + def test_enforce_tenant_id_check_parent_resource_bw_compatibility(self): def fakegetnetwork(*args, **kwargs):