Skip to content

Commit

Permalink
Fix a bug involving thread local memory initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonwillard committed Jan 22, 2021
1 parent 39af8a8 commit ebaad55
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
33 changes: 33 additions & 0 deletions tests/compile/test_compilelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import sys
import tempfile
import threading
import time

import filelock
import pytest
Expand Down Expand Up @@ -78,6 +80,37 @@ def run_locking_test(ctx):
assert get_subprocess_lock_state(ctx, dir_name) == "unlocked"


def test_locking_thread():

with tempfile.TemporaryDirectory() as dir_name:

def test_fn_1():
with lock_ctx(dir_name):
# Sleep "indefinitely"
time.sleep(100)

def test_fn_2(arg):
try:
with lock_ctx(dir_name, timeout=0.1):
# If this can get the lock, then our file lock has failed
raise AssertionError()
except filelock.Timeout:
# It timed out, which means that the lock was still held by the
# first thread
arg.append(True)

thread_1 = threading.Thread(target=test_fn_1)
res = []
thread_2 = threading.Thread(target=test_fn_2, args=(res,))

thread_1.start()
thread_2.start()

# The second thread should raise `filelock.Timeout`
thread_2.join()
assert True in res


@pytest.mark.skipif(sys.platform != "linux", reason="Fork is only available on linux")
def test_locking_multiprocess_fork():
ctx = multiprocessing.get_context("fork")
Expand Down
8 changes: 6 additions & 2 deletions theano/compile/compilelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
]


local_mem = threading.local()
local_mem._locks: typing.Dict[str, bool] = {}
class ThreadFileLocks(threading.local):
def __init__(self):
self._locks = {}


local_mem = ThreadFileLocks()


def force_unlock(lock_dir: os.PathLike):
Expand Down

0 comments on commit ebaad55

Please sign in to comment.