提交 ebaad559 authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Brandon T. Willard

Fix a bug involving thread local memory initialization

上级 39af8a86
...@@ -2,6 +2,8 @@ import multiprocessing ...@@ -2,6 +2,8 @@ import multiprocessing
import os import os
import sys import sys
import tempfile import tempfile
import threading
import time
import filelock import filelock
import pytest import pytest
...@@ -78,6 +80,37 @@ def run_locking_test(ctx): ...@@ -78,6 +80,37 @@ def run_locking_test(ctx):
assert get_subprocess_lock_state(ctx, dir_name) == "unlocked" 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") @pytest.mark.skipif(sys.platform != "linux", reason="Fork is only available on linux")
def test_locking_multiprocess_fork(): def test_locking_multiprocess_fork():
ctx = multiprocessing.get_context("fork") ctx = multiprocessing.get_context("fork")
......
...@@ -18,8 +18,12 @@ __all__ = [ ...@@ -18,8 +18,12 @@ __all__ = [
] ]
local_mem = threading.local() class ThreadFileLocks(threading.local):
local_mem._locks: typing.Dict[str, bool] = {} def __init__(self):
self._locks = {}
local_mem = ThreadFileLocks()
def force_unlock(lock_dir: os.PathLike): def force_unlock(lock_dir: os.PathLike):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论