← Back to team overview

yahoo-eng-team team mailing list archive

[Bug 1501686] [NEW] Incorrect exception handling in DB code involving rollbacked transactions.

 

Public bug reported:

I found out that some methods like _create_ha_interfaces
https://github.com/openstack/neutron/blob/master/neutron/db/l3_hamode_db.py#L329-L345
contain the following logic:

def create():
      create_something()
      try:
            _do_other_thing()
      except Exception:
             with excutils.save_and_reraise_exception():
                     delete_something()

def  _do_other_thing():
     with context.session.begin(subtransactions=True):
         ....


The problem is that when exception is raised in _do_other_thing it is caught in except block, but the object cannot be deleted in except section because internal transaction has been rolled back. We have tests on these methods, but they also are not correct (for example https://github.com/openstack/neutron/blob/master/neutron/tests/unit/db/test_l3_hamode_db.py#L360-L377) as methods  _do_other_thing() are mocked so inside transaction is never created and aborted.

The possible solution is to use nested transaction in such places like
this:

def create()
       with context.session.begin(subtransactions=True):
               create_something()
               try:
                       _do_other_thing()
               except Exception:
                       with excutils.save_and_reraise_exception():
                           delete_something()

def _do_other_thing():
     with context.session.begin(nested=True):
         ....

** Affects: neutron
     Importance: Undecided
     Assignee: Ann Kamyshnikova (akamyshnikova)
         Status: New


** Tags: db

-- 
You received this bug notification because you are a member of Yahoo!
Engineering Team, which is subscribed to neutron.
https://bugs.launchpad.net/bugs/1501686

Title:
  Incorrect exception handling in DB code involving rollbacked
  transactions.

Status in neutron:
  New

Bug description:
  I found out that some methods like _create_ha_interfaces
  https://github.com/openstack/neutron/blob/master/neutron/db/l3_hamode_db.py#L329-L345
  contain the following logic:

  def create():
        create_something()
        try:
              _do_other_thing()
        except Exception:
               with excutils.save_and_reraise_exception():
                       delete_something()

  def  _do_other_thing():
       with context.session.begin(subtransactions=True):
           ....

  
  The problem is that when exception is raised in _do_other_thing it is caught in except block, but the object cannot be deleted in except section because internal transaction has been rolled back. We have tests on these methods, but they also are not correct (for example https://github.com/openstack/neutron/blob/master/neutron/tests/unit/db/test_l3_hamode_db.py#L360-L377) as methods  _do_other_thing() are mocked so inside transaction is never created and aborted.

  The possible solution is to use nested transaction in such places like
  this:

  def create()
         with context.session.begin(subtransactions=True):
                 create_something()
                 try:
                         _do_other_thing()
                 except Exception:
                         with excutils.save_and_reraise_exception():
                             delete_something()

  def _do_other_thing():
       with context.session.begin(nested=True):
           ....

To manage notifications about this bug go to:
https://bugs.launchpad.net/neutron/+bug/1501686/+subscriptions


Follow ups