Skip to content

Commit 903cc43

Browse files
committed
Worked on some thread safety issues with vcs caching.
1 parent 0f3f187 commit 903cc43

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

labscript/labscript.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717
import subprocess
1818
import keyword
19+
import threading
1920
from inspect import getcallargs
2021
from functools import wraps
2122

@@ -2187,8 +2188,10 @@ def generate_connection_table(hdf5_file):
21872188
# replace the outdated cache entry with a new list of updated vcs commands and
21882189
# outputs.
21892190
_vcs_cache = {}
2191+
_vcs_cache_rlock = threading.RLock()
21902192
def _file_watcher_callback(name, info, event):
2191-
_vcs_cache[name] = _run_vcs_commands(name)
2193+
with _vcs_cache_rlock:
2194+
_vcs_cache[name] = _run_vcs_commands(name)
21922195

21932196
_file_watcher = FileWatcher(_file_watcher_callback)
21942197

@@ -2283,17 +2286,15 @@ def save_labscripts(hdf5_file):
22832286
# Doesn't seem to want to double count files if you just import the contents of a file within a module
22842287
continue
22852288
hdf5_file.create_dataset(save_path, data=open(path).read())
2286-
if path not in _file_watcher.files:
2287-
# Add file to watch list and create its entry in the cache.
2288-
_file_watcher.add_file(path)
2289-
_file_watcher_callback(path, None, None)
2290-
# Get a reference to the current results list in case
2291-
# another thread updates _vcs_cache[path] to point at a new
2292-
# list during this loop.
2293-
results = _vcs_cache[path]
2294-
for command, info, err in results:
2295-
attribute_str = command[0] + ' ' + command[1]
2296-
hdf5_file[save_path].attrs[attribute_str] = (info + '\n' + err)
2289+
with _vcs_cache_rlock:
2290+
if path not in _vcs_cache:
2291+
# Add file to watch list and create its entry in the cache.
2292+
_file_watcher.add_file(path)
2293+
_file_watcher_callback(path, None, None)
2294+
# Save the cached vcs output to the file.
2295+
for command, info, err in _vcs_cache[path]:
2296+
attribute_str = command[0] + ' ' + command[1]
2297+
hdf5_file[save_path].attrs[attribute_str] = (info + '\n' + err)
22972298
except ImportError:
22982299
pass
22992300
except WindowsError if os.name == 'nt' else None:

0 commit comments

Comments
 (0)