Skip to content

Commit

Permalink
Fix thread race condition (#2248)
Browse files Browse the repository at this point in the history
Discovered internally at Google. The culprit was a thread race condition
around the usage of usedAt, which could race between setting the field
and retrieving it.
  • Loading branch information
TimvdLippe committed Apr 5, 2021
1 parent 3b96d1f commit 08a2e76
Showing 1 changed file with 7 additions and 2 deletions.
Expand Up @@ -22,6 +22,7 @@ public class StubbedInvocationMatcher extends InvocationMatcher implements Seria
private static final long serialVersionUID = 4919105134123672727L;
private final Queue<Answer> answers = new ConcurrentLinkedQueue<Answer>();
private final Strictness strictness;
private final Object usedAtLock = new Object[0];
private DescribedInvocation usedAt;

public StubbedInvocationMatcher(
Expand All @@ -45,11 +46,15 @@ public void addAnswer(Answer answer) {
}

public void markStubUsed(DescribedInvocation usedAt) {
this.usedAt = usedAt;
synchronized (usedAtLock) {
this.usedAt = usedAt;
}
}

public boolean wasUsed() {
return usedAt != null;
synchronized (usedAtLock) {
return usedAt != null;
}
}

@Override
Expand Down

0 comments on commit 08a2e76

Please sign in to comment.