Skip to content

Commit 0dd02ca

Browse files
authored
Results instances are not thread-safe, so reads take no lock (#241)
Results instances are not thread-safe, so reads take no lock. Reviewed at def9267 with no high finding open, and all 3 checks on that head passing.
1 parent 1952ff5 commit 0dd02ca

6 files changed

Lines changed: 120 additions & 232 deletions

File tree

‎device-detection/fiftyone_devicedetection_onpremise/src/fiftyone_devicedetection_onpremise/swig_data.py‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
# such notice(s) shall fulfill the requirements of that article.
2121
# *********************************************************************
2222

23-
import threading
24-
2523
from fiftyone_pipeline_engines.aspectdata import AspectData
2624
from fiftyone_pipeline_core.aspectproperty_value import AspectPropertyValue
2725

@@ -55,6 +53,17 @@ class SwigData(AspectData):
5553
"""!
5654
Extention of AspectData made for retrieving results
5755
created by the SWIG wrapper of the On Premise Device Detection engine
56+
57+
An instance reads its values from a results instance, the object the
58+
native code fills for one request, which references unmanaged memory.
59+
Multi-threaded access to a results instance is not supported, for
60+
performance reasons. Reading a value from it writes into it, so it is not
61+
safe to use from several threads at once, and neither is this instance.
62+
The engine, its results instances and this instance take no lock to make
63+
those reads safe for several threads. The caller is responsible for
64+
thread-safe access, and either gives each thread its own Flow Data, and
65+
so its own results instance, or coordinates its own access, for example
66+
with a lock of its own around every read and around close().
5867
"""
5968

6069
def __init__(self, flow_element, swig_results):
@@ -73,11 +82,6 @@ def __init__(self, flow_element, swig_results):
7382

7483
self.missing_property_service = OnPremiseMissingPropertyService()
7584

76-
# Reading a value writes into the native results, and the first read
77-
# of a component can walk its graph, so one thread at a time reads
78-
# this instance.
79-
self._read_lock = threading.RLock()
80-
8185
def close(self):
8286

8387
"""!
@@ -87,8 +91,7 @@ def close(self):
8791
exception. Closing the FlowData closes its results.
8892
"""
8993

90-
with self._read_lock:
91-
self.swig_results = None
94+
self.swig_results = None
9295

9396
def get(self, key):
9497

@@ -99,13 +102,12 @@ def get(self, key):
99102
@returns mixed
100103
"""
101104

102-
with self._read_lock:
103-
# Checked here, because AspectData.get turns an exception from
104-
# get_internal into a missing property message.
105-
if self.swig_results is None:
106-
raise Exception(RESULTS_CLOSED)
105+
# Checked here, because AspectData.get turns an exception from
106+
# get_internal into a missing property message.
107+
if self.swig_results is None:
108+
raise Exception(RESULTS_CLOSED)
107109

108-
return super(SwigData, self).get(key)
110+
return super(SwigData, self).get(key)
109111

110112
def get_internal(self, key):
111113

‎device-detection/fiftyone_devicedetection_onpremise/tests/test_swig_data_threads.py‎

Lines changed: 25 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,21 @@
2121
# *********************************************************************
2222

2323
"""
24-
One thread at a time reads a native results instance, because reading a value
25-
writes into the results.
24+
A results instance is not safe to use from several threads at once and the
25+
caller is responsible for thread-safe access, so SwigData takes no lock and
26+
its documentation says so.
2627
"""
2728

2829
import threading
29-
import time
3030
import unittest
3131

3232
from fiftyone_devicedetection_onpremise.swig_data import SwigData
3333

3434

35-
# How long a stand-in read takes, long enough that overlapping reads are seen.
36-
READ_SECONDS = 0.02
37-
# The longest a test waits for another thread.
38-
WAIT_SECONDS = 10
39-
# How long closing is given while a read holds the results. It must still be
40-
# waiting at the end.
41-
BLOCKED_SECONDS = 0.2
42-
43-
KEY = "deviceid"
44-
VALUE = "1-2-3"
45-
46-
47-
class OverlapCountingResults:
48-
"""Stands in for the native results and records how many reads overlap."""
49-
50-
def __init__(self):
51-
self.guard = threading.Lock()
52-
self.active = 0
53-
self.most_active = 0
54-
55-
def getDeviceId(self):
56-
with self.guard:
57-
self.active += 1
58-
self.most_active = max(self.most_active, self.active)
59-
time.sleep(READ_SECONDS)
60-
with self.guard:
61-
self.active -= 1
62-
return "1-2-3"
63-
64-
65-
class BlockingResults:
66-
"""Stands in for the native results with a read that waits to be let go,
67-
so a test can act while the read is in progress."""
68-
69-
def __init__(self):
70-
self.reading = threading.Event()
71-
self.release = threading.Event()
72-
73-
def getDeviceId(self):
74-
self.reading.set()
75-
self.release.wait(WAIT_SECONDS)
76-
return "1-2-3"
35+
# The types of the locks the threading module makes.
36+
LOCK_TYPES = (
37+
type(threading.Lock()), type(threading.RLock()),
38+
threading.Condition, threading.Semaphore)
7739

7840

7941
class FakeElement:
@@ -83,50 +45,24 @@ class FakeElement:
8345

8446
class SwigDataThreadTests(unittest.TestCase):
8547

86-
def test_reads_from_several_threads_do_not_overlap(self):
87-
results = OverlapCountingResults()
88-
data = SwigData(FakeElement(), results)
89-
errors = []
90-
91-
def read():
92-
try:
93-
for _ in range(3):
94-
data.get("deviceid")
95-
except Exception as e:
96-
errors.append(e)
97-
98-
threads = [threading.Thread(target=read) for _ in range(6)]
99-
for thread in threads:
100-
thread.start()
101-
for thread in threads:
102-
thread.join()
103-
104-
self.assertEqual([], errors)
105-
self.assertEqual(1, results.most_active)
106-
107-
def test_closing_waits_for_a_read_in_progress(self):
108-
results = BlockingResults()
109-
data = SwigData(FakeElement(), results)
110-
values = []
111-
reader = threading.Thread(
112-
target=lambda: values.append(data.get(KEY)))
113-
reader.start()
114-
self.assertTrue(results.reading.wait(WAIT_SECONDS))
115-
116-
closer = threading.Thread(target=data.close)
117-
closer.start()
118-
closer.join(BLOCKED_SECONDS)
119-
# The read holds the results, so closing waits for it.
120-
self.assertTrue(closer.is_alive())
121-
122-
results.release.set()
123-
reader.join(WAIT_SECONDS)
124-
closer.join(WAIT_SECONDS)
125-
self.assertFalse(closer.is_alive())
126-
self.assertEqual([VALUE], [value.value() for value in values])
127-
self.assertIsNone(data.swig_results)
128-
with self.assertRaises(Exception):
129-
data.get(KEY)
48+
def test_the_documentation_says_the_caller_coordinates_threads(self):
49+
text = " ".join(SwigData.__doc__.split())
50+
self.assertIn("unmanaged memory", text)
51+
self.assertIn(
52+
"Multi-threaded access to a results instance is not supported, "
53+
"for performance reasons", text)
54+
self.assertIn("not safe to use from several threads at once", text)
55+
self.assertIn("take no lock", text)
56+
self.assertIn("caller is responsible for thread-safe access", text)
57+
58+
def test_an_instance_holds_no_lock(self):
59+
"""A smoke test. It looks only at the instance's own attributes,
60+
so a lock at module level, on the engine or in AspectData would
61+
still pass."""
62+
data = SwigData(FakeElement(), object())
63+
locks = [name for name, value in vars(data).items()
64+
if isinstance(value, LOCK_TYPES)]
65+
self.assertEqual([], locks)
13066

13167

13268
if __name__ == "__main__":

‎ip-intelligence/fiftyone_ipintelligence_onpremise/src/fiftyone_ipintelligence_onpremise/swig_data.py‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# *********************************************************************
2222

2323
import math
24-
import threading
2524
from decimal import Decimal, ROUND_HALF_UP
2625

2726
from fiftyone_pipeline_engines.aspectdata import AspectData
@@ -65,6 +64,17 @@ class SwigData(AspectData):
6564
"""!
6665
Extension of AspectData made for retrieving results
6766
created by the SWIG wrapper of the On Premise IP Intelligence engine
67+
68+
An instance reads its values from a results instance, the object the
69+
native code fills for one request, which references unmanaged memory.
70+
Multi-threaded access to a results instance is not supported, for
71+
performance reasons. Reading a value from it writes into it, so it is not
72+
safe to use from several threads at once, and neither is this instance.
73+
The engine, its results instances and this instance take no lock to make
74+
those reads safe for several threads. The caller is responsible for
75+
thread-safe access, and either gives each thread its own Flow Data, and
76+
so its own results instance, or coordinates its own access, for example
77+
with a lock of its own around every read and around close().
6878
"""
6979

7080
def __init__(self, flow_element, swig_results, echo_ip=None, echo_ipv6=None):
@@ -89,11 +99,6 @@ def __init__(self, flow_element, swig_results, echo_ip=None, echo_ipv6=None):
8999

90100
self.missing_property_service = OnPremiseMissingPropertyService()
91101

92-
# Reading a value writes into the native results, and the first read
93-
# of a component can walk its graph, so one thread at a time reads
94-
# this instance.
95-
self._read_lock = threading.RLock()
96-
97102
def close(self):
98103

99104
"""!
@@ -103,8 +108,7 @@ def close(self):
103108
exception. Closing the FlowData closes its results.
104109
"""
105110

106-
with self._read_lock:
107-
self.swig_results = None
111+
self.swig_results = None
108112

109113
def get(self, key):
110114

@@ -115,13 +119,12 @@ def get(self, key):
115119
@returns mixed
116120
"""
117121

118-
with self._read_lock:
119-
# Checked here, because AspectData.get turns an exception from
120-
# get_internal into a missing property message.
121-
if self.swig_results is None:
122-
raise Exception(RESULTS_CLOSED)
122+
# Checked here, because AspectData.get turns an exception from
123+
# get_internal into a missing property message.
124+
if self.swig_results is None:
125+
raise Exception(RESULTS_CLOSED)
123126

124-
return super(SwigData, self).get(key)
127+
return super(SwigData, self).get(key)
125128

126129
@staticmethod
127130
def utf8_string_to_str(utf8_string):

0 commit comments

Comments
 (0)