Skip to content

Commit 147e2bb

Browse files
committed
Use more locks to avoid race conditions
1 parent 647a90a commit 147e2bb

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

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

0 commit comments

Comments
 (0)