Skip to content

Commit

Permalink
Fix race in ParametrizedUriEmitter.close() (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
leventov authored and drcrallen committed Aug 3, 2017
1 parent d7ffc3a commit 0cf83b3
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ private static UriExtractor makeUriExtractor(ParametrizedUriEmitterConfig config
private final ConcurrentHashMap<URI, HttpPostEmitter> emitters = new ConcurrentHashMap<>();
private final UriExtractor uriExtractor;
private final AtomicBoolean startFlag = new AtomicBoolean(false);
private final AtomicBoolean stopFlag = new AtomicBoolean(false);
private final Object closeLock = new Object();
private boolean closed = false;
private final Lifecycle innerLifecycle = new Lifecycle();
private final HttpClient client;
private final ObjectMapper jsonMapper;
Expand Down Expand Up @@ -121,7 +122,13 @@ public void emit(Event event)
@LifecycleStop
public void close() throws IOException
{
if (!stopFlag.getAndSet(true)) {
// Use full synchronized instead of atomic flag, because otherwise some thread may think that the emitter is already
// closed while it's in the process of closing by another thread.
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
innerLifecycle.stop();
}
}
Expand Down

0 comments on commit 0cf83b3

Please sign in to comment.