Skip to content

Commit

Permalink
Fix for #576 Possible Thread Race when Broadcaster gets destroyed and…
Browse files Browse the repository at this point in the history
… the re-use option is set to true
  • Loading branch information
jfarcand committed Aug 27, 2012
1 parent 424f447 commit d6aea89
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ public abstract class AsynchronousProcessor implements AsyncSupport<AtmosphereRe
aliveRequests = new ConcurrentHashMap<AtmosphereRequest, AtmosphereResource>();
private boolean trackActiveRequest = false;
private final ScheduledExecutorService closedDetector = Executors.newScheduledThreadPool(1);
private final BroadcasterFactory broadcasterFactory;

public AsynchronousProcessor(AtmosphereConfig config) {
this.config = config;
this.broadcasterFactory = config.getBroadcasterFactory();
}

@Override
Expand Down Expand Up @@ -207,9 +209,16 @@ Action action(AtmosphereRequest req, AtmosphereResponse res) throws IOException,
// Check Broadcaster state. If destroyed, replace it.
Broadcaster b = handlerWrapper.broadcaster;
if (b.isDestroyed()) {
synchronized (handlerWrapper) {
config.getBroadcasterFactory().remove(b, b.getID());
handlerWrapper.broadcaster = config.getBroadcasterFactory().get(b.getID());
synchronized (broadcasterFactory) {
broadcasterFactory.remove(b, b.getID());
try {
handlerWrapper.broadcaster = broadcasterFactory.get(b.getID());
} catch (IllegalStateException ex) {
// Something wrong occurred, let's not fail and loookup the value
logger.trace("", ex);
// fallback to lookup
handlerWrapper.broadcaster = broadcasterFactory.lookup(b.getID(), true);
}
}
}

Expand Down

0 comments on commit d6aea89

Please sign in to comment.