DataPlanePort: fix race between thread start and kill
A very short test could kill the port before the port's thread started running.
The thread would overwrite self.running with True and thus never exit.
diff --git a/src/python/oftest/dataplane.py b/src/python/oftest/dataplane.py
index c8c59a4..ee3d7b1 100644
--- a/src/python/oftest/dataplane.py
+++ b/src/python/oftest/dataplane.py
@@ -94,6 +94,7 @@
raise
self.logger.info("Opened port monitor (class %s)", type(self).__name__)
self.parent = parent
+ self.killed = False
# Used to wake up the event loop in kill()
self.waker = EventDescriptor()
@@ -115,10 +116,9 @@
"""
Activity function for class
"""
- self.running = True
self.socs = [self.socket, self.waker]
error_warned = False # Have we warned about error?
- while self.running:
+ while not self.killed:
try:
sel_in, sel_out, sel_err = \
select.select(self.socs, [], [], 1)
@@ -127,9 +127,6 @@
self.logger.error("Select error, exiting")
break
- if not self.running:
- break
-
if (sel_in is None) or (len(sel_in) == 0):
continue
@@ -173,7 +170,7 @@
Terminate the running thread
"""
self.logger.debug("Port monitor kill")
- self.running = False
+ self.killed = True
self.waker.notify()
try:
self.socket.close()
@@ -278,8 +275,7 @@
"""
Activity function for class
"""
- self.running = True
- while self.running:
+ while not self.killed:
try:
sel_in, sel_out, sel_err = select.select([self.socket, self.waker], [], [], 1)
except:
@@ -287,9 +283,6 @@
self.logger.error("Select error, exiting")
break
- if not self.running:
- break
-
if (sel_in is None) or (len(sel_in) == 0):
continue
@@ -320,7 +313,7 @@
Terminate the running thread
"""
self.logger.debug("Port monitor kill")
- self.running = False
+ self.killed = True
self.waker.notify()
# pcap object is closed on GC.