Skip to content

Commit

Permalink
srm: Fix race in user persistence
Browse files Browse the repository at this point in the history
Motivation:

A lazily allocated lock may get garbage collected even while somebody
is holding the lock.

Modification:

Keep a hard reference to the lock while holding it.

Result:

Race is gone.

Target: trunk
Require-notes: no
Require-book: no
Request: 2.14
Acked-by: Paul Millar <paul.millar@desy.de>
Acked-by: Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
Patch: https://rb.dcache.org/r/8730/
  • Loading branch information
gbehrmann committed Nov 6, 2015
1 parent 8520709 commit 4f86664
Showing 1 changed file with 12 additions and 8 deletions.
Expand Up @@ -154,11 +154,12 @@ public CanonicalizingByteArrayStore(
@Nullable
public Token toToken(long id)
{
locks.get(id).lock();
Lock lock = locks.get(id);
lock.lock();
try {
return (load(id) != null) ? makeToken(id) : null;
} finally {
locks.get(id).unlock();
lock.unlock();
}
}

Expand All @@ -181,7 +182,8 @@ public Token toToken(byte[] bytes)
do {
HashCode hash = Hashing.sipHash24(K0, k1++).hashBytes(bytes);
long id = hash.asLong();
locks.get(id).lock();
Lock lock = locks.get(id);
lock.lock();
try {
byte[] canonical = load(id);
if (canonical == null) {
Expand All @@ -191,7 +193,7 @@ public Token toToken(byte[] bytes)
token = makeToken(id);
}
} finally {
locks.get(id).unlock();
lock.unlock();
}
} while (token == null);
return token;
Expand All @@ -207,15 +209,16 @@ public Token toToken(byte[] bytes)
public byte[] readBytes(Token token)
{
long id = token.getId();
locks.get(id).lock();
Lock lock = locks.get(id);
lock.lock();
try {
byte[] bytes = load(id);
if (bytes == null) {
throw new IncorrectResultSizeDataAccessException(1, 0);
}
return bytes;
} finally {
locks.get(id).unlock();
lock.unlock();
}
}

Expand Down Expand Up @@ -262,14 +265,15 @@ public void gc(List<Long> ids)
canonicalizationCache.cleanUp();

for (Long id : ids) {
locks.get(id).lock();
Lock lock = locks.get(id);
lock.lock();
try {
if (canonicalizationCache.getIfPresent(id) == null) {
cache.invalidate(id);
delete.accept(id);
}
} finally {
locks.get(id).unlock();
lock.unlock();
}
}
}
Expand Down

0 comments on commit 4f86664

Please sign in to comment.