Skip to content

Commit 0120ec0

Browse files
committed
Use more locks to avoid race conditions
1 parent d52946d commit 0120ec0

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

main.py

+29-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
NApp to provision circuits from user request.
44
"""
5+
from threading import Lock
6+
57
from flask import jsonify, request
68
from werkzeug.exceptions import (BadRequest, Conflict, Forbidden,
79
MethodNotAllowed, NotFound,
@@ -48,6 +50,8 @@ def setup(self):
4850
# dictionary of EVCs by interface
4951
self._circuits_by_interface = {}
5052

53+
self._lock = Lock()
54+
5155
self.execute_as_loop(settings.DEPLOY_EVCS_INTERVAL)
5256

5357
def execute(self):
@@ -508,28 +512,31 @@ def add_to_dict_of_sets(self, intf, circuit_id):
508512
@listen_to('kytos/topology.port.created')
509513
def load_evcs(self, event):
510514
"""Try to load the unloaded EVCs from storehouse."""
511-
log.debug("Event load_evcs %s", event)
512-
circuits = self.storehouse.get_data()
513-
if not self._circuits_by_interface:
514-
self.load_circuits_by_interface(circuits)
515-
516-
interface_id = '{}:{}'.format(event.content['switch'],
517-
event.content['port'])
518-
519-
for circuit_id in self._circuits_by_interface.get(interface_id, []):
520-
if circuit_id in circuits and circuit_id not in self.circuits:
521-
try:
522-
evc = self._evc_from_dict(circuits[circuit_id])
523-
except ValueError as exception:
524-
log.info(
525-
f'Could not load EVC {circuit_id} because {exception}')
526-
continue
527-
528-
evc.deactivate()
529-
evc.current_path = Path([])
530-
evc.sync()
531-
self.circuits.setdefault(circuit_id, evc)
532-
self.sched.add(evc)
515+
with self._lock:
516+
log.debug("Event load_evcs %s", event)
517+
circuits = self.storehouse.get_data()
518+
if not self._circuits_by_interface:
519+
self.load_circuits_by_interface(circuits)
520+
521+
interface_id = '{}:{}'.format(event.content['switch'],
522+
event.content['port'])
523+
524+
for circuit_id in self._circuits_by_interface.get(interface_id,
525+
[]):
526+
if circuit_id in circuits and circuit_id not in self.circuits:
527+
try:
528+
evc = self._evc_from_dict(circuits[circuit_id])
529+
except ValueError as exception:
530+
log.info(
531+
f'Could not load EVC {circuit_id} '
532+
f'because {exception}')
533+
continue
534+
535+
evc.deactivate()
536+
evc.current_path = Path([])
537+
evc.sync()
538+
self.circuits.setdefault(circuit_id, evc)
539+
self.sched.add(evc)
533540

534541
@listen_to('kytos/flow_manager.flow.error')
535542
def handle_flow_mod_error(self, event):

0 commit comments

Comments
 (0)