Skip to content

Commit b25f9cd

Browse files
aarchibamaxnoe
authored andcommitted
Fix bug in locking mechanism
Should fix astropy#9642
1 parent f119c84 commit b25f9cd

2 files changed

Lines changed: 90 additions & 24 deletions

File tree

‎astropy/utils/data.py‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,12 +1414,27 @@ def clear_download_cache(hashorurl=None, pkgname='astropy'):
14141414
The package name to use to locate the download cache. i.e. for
14151415
``pkgname='astropy'`` the default cache location is
14161416
``~/.astropy/cache``.
1417-
14181417
"""
1419-
14201418
zapped_cache = False
14211419
try:
1422-
with _cache(pkgname, write=True) as (dldir, url2hash):
1420+
with contextlib.ExitStack() as stack:
1421+
try:
1422+
dldir, url2hash = stack.enter_context(
1423+
_cache(pkgname, write=True))
1424+
except OSError as e:
1425+
# Problem arose when trying to open the cache
1426+
msg = 'Not clearing data cache - cache inaccessible due to '
1427+
estr = '' if len(e.args) < 1 else (': ' + str(e))
1428+
warn(CacheMissingWarning(msg + e.__class__.__name__ + estr))
1429+
return
1430+
except RuntimeError: # Couldn't get lock
1431+
if hashorurl is None:
1432+
# Release lock by blowing away cache
1433+
# Need to get locations
1434+
dldir, _ = _get_download_cache_locs(pkgname)
1435+
else:
1436+
# Can't do specific deletion without the lock
1437+
raise
14231438
if hashorurl is None:
14241439
if os.path.exists(dldir):
14251440
shutil.rmtree(dldir)
@@ -1460,7 +1475,7 @@ def clear_download_cache(hashorurl=None, pkgname='astropy'):
14601475
# It's fine for this to fail.
14611476
return
14621477
else:
1463-
msg = 'Not clearing data cache - cache inaccessible due to '
1478+
msg = 'Not clearing data from cache - problem arose '
14641479
estr = '' if len(e.args) < 1 else (': ' + str(e))
14651480
warn(CacheMissingWarning(msg + e.__class__.__name__ + estr))
14661481
return
@@ -1533,6 +1548,7 @@ def _cache_lock(pkgname, need_write=False):
15331548
lockdir = os.path.join(_get_download_cache_locs(pkgname)[0], 'lock')
15341549
pidfn = os.path.join(lockdir, 'pid')
15351550
assume_cache_readonly = False
1551+
got_lock = False
15361552
try:
15371553
msg = f"Config file requests {conf.download_cache_lock_attempts} tries"
15381554
for waited in _keep_trying(conf.download_cache_lock_attempts):
@@ -1554,7 +1570,7 @@ def _cache_lock(pkgname, need_write=False):
15541570
else:
15551571
break
15561572
else:
1557-
# Got the lock!
1573+
got_lock = True
15581574
# write the pid of this process for informational purposes
15591575
with open(pidfn, 'w') as f:
15601576
f.write(str(os.getpid()))
@@ -1575,7 +1591,8 @@ def _cache_lock(pkgname, need_write=False):
15751591
yield
15761592

15771593
finally:
1578-
if not assume_cache_readonly and os.path.exists(lockdir):
1594+
# clear_download_cache might have deleted the lockdir
1595+
if got_lock and os.path.exists(lockdir):
15791596
if os.path.isdir(lockdir):
15801597
# if the pid file is present, be sure to remove it
15811598
if os.path.exists(pidfn):

‎astropy/utils/tests/test_data.py‎

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import py.path
2727
import pytest
28+
from _pytest.monkeypatch import MonkeyPatch
2829

2930
from astropy.utils import data
3031
from astropy.config import paths
@@ -34,6 +35,7 @@
3435
CacheDamaged,
3536
conf,
3637
_cache,
38+
_cache_lock,
3739
compute_hash,
3840
download_file,
3941
cache_contents,
@@ -191,23 +193,30 @@ def no_mkdir(p):
191193

192194

193195
_shelve_possible_backends = ["dbm.dumb", "dbm.ndbm", "dbm.gnu"]
196+
for n in _shelve_possible_backends:
197+
if n not in dbm._modules:
198+
try:
199+
dbm._modules[n] = __import__(n, fromlist=['open'])
200+
except ImportError:
201+
pass
194202

195203

196204
@contextlib.contextmanager
197205
def shelve_backend(name):
198206
"""Ensure that shelve has access only to one backend."""
199-
defaultmod = dbm._defaultmod
200-
modules = dbm._modules
201-
try:
207+
# WARNING: there is something weird and fragile about this.
208+
# Using it can cause weird order-dependent test failures in
209+
# the second shelve_backend test if the two meet certain
210+
# mysterious criteria.
211+
with MonkeyPatch().context() as mp:
202212
try:
203-
dbm._defaultmod = __import__(name, fromlist=['open'])
204-
except ImportError:
205-
pytest.skip(f"module {name} not available")
206-
dbm._modules = {name: dbm._defaultmod}
213+
m = dbm._modules[name]
214+
except KeyError:
215+
pytest.skip(f"Backend {name} not available")
216+
217+
mp.setattr(dbm, "_defaultmod", m)
218+
mp.setattr(dbm, "_modules", {name: m})
207219
yield
208-
finally:
209-
dbm._defaultmod = defaultmod
210-
dbm._modules = modules
211220

212221

213222
@pytest.mark.remote_data(source="astropy")
@@ -287,15 +296,21 @@ def test_download_file_threaded_many(b, temp_cache, valid_urls):
287296
also does this but coverage tools lose track of which paths are explored.
288297
289298
"""
299+
u, c = next(valid_urls)
300+
# Ensure download cache exists in appropriate format
290301
with shelve_backend(b):
291-
urls = list(islice(valid_urls, N_THREAD_HAMMER))
292-
with ThreadPoolExecutor(max_workers=len(urls)) as P:
293-
r = list(P.map(lambda u: download_file(u, cache=True),
294-
[u for (u, c) in urls]))
295-
check_download_cache()
296-
assert len(r) == len(urls)
297-
for r, (u, c) in zip(r, urls):
298-
assert get_file_contents(r) == c
302+
download_file(u, cache=True)
303+
clear_download_cache(u)
304+
305+
# Run test outside of non-thread-safe context manager
306+
urls = list(islice(valid_urls, N_THREAD_HAMMER))
307+
with ThreadPoolExecutor(max_workers=len(urls)) as P:
308+
r = list(P.map(lambda u: download_file(u, cache=True),
309+
[u for (u, c) in urls]))
310+
check_download_cache()
311+
assert len(r) == len(urls)
312+
for r, (u, c) in zip(r, urls):
313+
assert get_file_contents(r) == c
299314

300315

301316
def test_download_file_threaded_many_partial_success(
@@ -1791,9 +1806,16 @@ def test_cache_with_different_shelve_backends(b, temp_cache, valid_urls):
17911806
uc = list(islice(valid_urls, FEW))
17921807
for u, c in uc:
17931808
download_file(u, cache=True)
1809+
assert is_url_in_cache(u)
1810+
1811+
for u, _ in uc:
1812+
assert is_url_in_cache(u)
17941813

17951814
check_download_cache()
17961815

1816+
for u, _ in uc:
1817+
assert is_url_in_cache(u)
1818+
17971819
for u, c in uc:
17981820
assert get_file_contents(
17991821
download_file(u, cache=True, sources=[])) == c
@@ -1841,3 +1863,30 @@ def test_wrong_backend_reports_useful_error(b1b2, temp_cache, valid_urls):
18411863
with pytest.raises(CacheDamaged):
18421864
with pytest.warns(WrongDBMModuleWarning):
18431865
check_download_cache()
1866+
clear_download_cache()
1867+
clear_download_cache()
1868+
1869+
1870+
# What happens when the lock can't be obtained in a timely manner?
1871+
def test_lock_unavailable_raises_runtimeerror_and_clear_frees_lock(
1872+
temp_cache, valid_urls):
1873+
u, c = next(valid_urls)
1874+
download_file(u, cache=True)
1875+
with _cache_lock("astropy", need_write=True):
1876+
with conf.set_temp("download_cache_lock_attempts", 0):
1877+
with pytest.raises(RuntimeError):
1878+
with _cache_lock("astropy", need_write=True):
1879+
pass
1880+
# Ensure that the cache doesn't accidentally get unlocked!
1881+
with pytest.raises(RuntimeError):
1882+
with _cache_lock("astropy", need_write=True):
1883+
pass
1884+
# Download
1885+
with pytest.raises(RuntimeError):
1886+
is_url_in_cache(u), "if locked, act like cache empty"
1887+
with pytest.raises(RuntimeError):
1888+
download_file(u, cache=True, sources=[])
1889+
with pytest.raises(RuntimeError):
1890+
download_file(u, cache=True)
1891+
clear_download_cache() # breaks lock
1892+
is_url_in_cache(u)

0 commit comments

Comments
 (0)