Skip to content

Commit bf9dd0b

Browse files
Add thread lock to Synchronization class to prevent race conditions in plv_history access (fixes #42)
1 parent a990ad4 commit bf9dd0b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

pyeyesweb/sync.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
sys.path.append(os.getcwd())
3131

3232
from collections import deque
33+
import threading
3334
import numpy as np
3435

3536
from pyeyesweb.data_models.sliding_window import SlidingWindow
@@ -83,6 +84,7 @@ class Synchronization:
8384
----------
8485
plv_history : collections.deque
8586
Rolling buffer storing recent PLV values for temporal analysis.
87+
Thread safe access is ensured via internal locking.
8688
output_phase : bool
8789
Flag controlling phase status output.
8890
filter_params : tuple or None
@@ -158,6 +160,7 @@ def __init__(self, sensitivity=100, output_phase=False, filter_params=None, phas
158160
raise ValueError(f"highcut ({highcut}) must be less than Nyquist frequency ({fs/2})")
159161

160162
self.plv_history = deque(maxlen=sensitivity)
163+
self._history_lock = threading.Lock()
161164
self.output_phase = output_phase
162165
self.filter_params = filter_params
163166
self.phase_threshold = phase_threshold
@@ -216,7 +219,8 @@ def compute_synchronization(self, signals: SlidingWindow):
216219

217220
# Compute the Phase Locking Value (PLV)
218221
plv = compute_phase_locking_value(phase1, phase2)
219-
self.plv_history.append(plv)
222+
with self._history_lock:
223+
self.plv_history.append(plv)
220224

221225
phase_status = None
222226
if self.output_phase:

0 commit comments

Comments
 (0)