Skip to content

Commit

Permalink
Fix race condition if two messages are broadcast to the same resource…
Browse files Browse the repository at this point in the history
… in very close succession
  • Loading branch information
trask committed Apr 3, 2011
1 parent 03baa5c commit eb696da
Showing 1 changed file with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,34 +428,34 @@ private Object translate(Object msg) {

protected void executeAsyncWrite(final AtmosphereResource<?, ?> resource, final Object msg, final BroadcasterFuture future) {

synchronized (resource) {
if (resource.getAtmosphereResourceEvent().isCancelled()) {
return;
}

final AtmosphereResourceEvent event = resource.getAtmosphereResourceEvent();
event.setMessage(msg);

if (resource.getAtmosphereResourceEvent() != null && !resource.getAtmosphereResourceEvent().isCancelled()
&& HttpServletRequest.class.isAssignableFrom(resource.getRequest().getClass())) {
try {
HttpServletRequest.class.cast(resource.getRequest())
.setAttribute(CometSupport.MAX_INACTIVE, System.currentTimeMillis());
}
catch (Exception t) {
// Shield us from any corrupted Request
logger.warn("Preventing corruption of a recycled request: resource" + resource, event);
resources.remove(resource);
if (future != null) {
future.cancel(true);
bc.getAsyncWriteService().execute(new Runnable(){
@Override
public void run() {
synchronized (resource) {
if (resource.getAtmosphereResourceEvent().isCancelled()) {
return;
}

final AtmosphereResourceEvent event = resource.getAtmosphereResourceEvent();
event.setMessage(msg);

if (resource.getAtmosphereResourceEvent() != null && !resource.getAtmosphereResourceEvent().isCancelled()
&& HttpServletRequest.class.isAssignableFrom(resource.getRequest().getClass())) {
try {
HttpServletRequest.class.cast(resource.getRequest())
.setAttribute(CometSupport.MAX_INACTIVE, System.currentTimeMillis());
}
catch (Exception t) {
// Shield us from any corrupted Request
logger.warn("Preventing corruption of a recycled request: resource" + resource, event);
resources.remove(resource);
if (future != null) {
future.cancel(true);
}
return;
}
}
return;
}
}

bc.getAsyncWriteService().execute(new Runnable(){
@Override
public void run() {
broadcast(resource, event);
if (resource instanceof AtmosphereEventLifecycle) {
((AtmosphereEventLifecycle) resource).notifyListeners();
Expand All @@ -464,8 +464,8 @@ public void run() {
future.done();
}
}
});
}
}
});
}

protected void checkCachedAndPush(final AtmosphereResource<?, ?> r, final AtmosphereResourceEvent e) {
Expand Down

0 comments on commit eb696da

Please sign in to comment.