Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lock to prevent race conditions on self._multipart_replies_flows #28

Merged
merged 3 commits into from
Nov 10, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""NApp responsible for the main OpenFlow basic operations."""

from threading import Lock

from pyof.foundation.exceptions import UnpackException
from pyof.foundation.network_types import Ethernet, EtherType
from pyof.utils import PYOF_VERSION_LIBS, unpack
Expand Down Expand Up @@ -39,6 +41,7 @@ def setup(self):
self.of_core_version_utils = {0x01: of_core_v0x01_utils,
0x04: of_core_v0x04_utils}
self.execute_as_loop(settings.STATS_INTERVAL)
self._multipart_flows_lock = {}

def execute(self):
"""Run once on app 'start' or in a loop.
Expand Down Expand Up @@ -163,9 +166,12 @@ def _handle_multipart_flow_stats(self, reply, switch):
# Get all flows from the reply
flows = [Flow04.from_of_flow_stats(of_flow_stats, switch)
for of_flow_stats in reply.body]
self._multipart_flows_lock.setdefault(switch.id, Lock())
# Get existent flows from the same xid (or create an empty list)
all_flows = self._multipart_replies_flows.setdefault(switch.id, [])
all_flows.extend(flows)
with self._multipart_flows_lock[switch.id]:
all_flows = self._multipart_replies_flows.setdefault(switch.id,
[])
all_flows.extend(flows)
if reply.flags.value % 2 == 0: # Last bit means more replies
self._update_switch_flows(switch)
event_raw = KytosEvent(
Expand Down