Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/538#discussion_r25637180
  
    --- Diff: 
core/src/main/java/brooklyn/location/access/PortForwardManagerImpl.java ---
    @@ -160,11 +162,22 @@ protected int getNextPort() {
         @Override
         public void associate(String publicIpId, HostAndPort publicEndpoint, 
Location l, int privatePort) {
             associateImpl(publicIpId, publicEndpoint, l, privatePort);
    +        emitAssociationEvent(publicIpId, publicEndpoint, l, privatePort);
         }
     
         @Override
         public void associate(String publicIpId, HostAndPort publicEndpoint, 
int privatePort) {
             associateImpl(publicIpId, publicEndpoint, null, privatePort);
    +        emitAssociationEvent(publicIpId, publicEndpoint, null, 
privatePort);
    +    }
    +
    +    private void emitAssociationEvent(String publicIpId, HostAndPort 
publicEndpoint, Location location, int privatePort) {
    +        AssociationMetadata metadata = new AssociationMetadata(publicIpId, 
publicEndpoint, location, privatePort);
    +        for (AssociationListener listener : associationListeners.keySet()) 
{
    +            if (associationListeners.get(listener).apply(metadata)) {
    --- End diff --
    
    Looks dangerous in a multi-threaded world. You've separated the call of 
`keySet()` and `get(listener)`, so you've introduced the chance for another 
thread to remove the listener in between those two calls.
    
    Better to do the following (thus allowing `ConcurrentHashMap` to give its 
guarantees):
    
        for (Map.Entry<AssociationListener, Predicate<? super 
AssociationMetadata> entry : associationListeners.entrySet()) {
            if (entry.getValue().apply(metadata)) {
                entry.getKey().onEvent(metadata);
            }
        }



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to