You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by "David Jencks (JIRA)" <ji...@apache.org> on 2017/04/25 01:34:04 UTC

[jira] [Updated] (FELIX-5621) [DS] event methods setting service properties not thread safe

     [ https://issues.apache.org/jira/browse/FELIX-5621?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

David Jencks updated FELIX-5621:
--------------------------------
    Description: 
Thanks to Brent Daniels for pointing this out.

Setting service properties from returning properties from event methods is not thread safe and does not appear to be possible to make thread safe, as synchronizing will almost certainly produce deadlocks. 

I think use of ExtComponentContext could be thread safe using code similar to....

final AtomicReference<Dictionary<String, Object>> propRef;

...
  Dictionary<String, Object> oldProps;
  Dictionary<String, Object> newProps;
  do {
    synchronized(propRef) {
      oldProps =  propRef.get();
      newProps = new Hashtable<>(oldProps);
    }
    //update newProps with new information
    extComponentContext.setServiceProperties(newProps);
  } while ( propsRef.compareAndSet( oldProps, newProps)}


    

  was:
Thanks to Brent Daniels for pointing this out.

Setting service properties from event methods is not thread safe and does not appear to be possible to make thread safe, as synchronizing will almost certainly produce deadlocks.  This applies to both setting the properties on ExtComponentContext or returning a properties map.

If ExtComponentContext reliably provided a change counter and a compareAndSet method then it could be used in e.g.

{code}
int cc;
Dictionary<String, Object> newProps;
do {
  cc = etc.getChangeCount();
 newProps = new Hashtable<>(ecc.getProperties());
 //update properties
} while (!ecc.compareAndSet(cc, newProps)}
{code}

I don't think this can be implemented unless ServiceRegistration provides the same API.  Certainly the felix ServiceRegistration implementation is not thread safe.


> [DS] event methods setting service properties not thread safe
> -------------------------------------------------------------
>
>                 Key: FELIX-5621
>                 URL: https://issues.apache.org/jira/browse/FELIX-5621
>             Project: Felix
>          Issue Type: Bug
>          Components: Declarative Services (SCR)
>    Affects Versions: scr-2.0.8
>            Reporter: David Jencks
>
> Thanks to Brent Daniels for pointing this out.
> Setting service properties from returning properties from event methods is not thread safe and does not appear to be possible to make thread safe, as synchronizing will almost certainly produce deadlocks. 
> I think use of ExtComponentContext could be thread safe using code similar to....
> final AtomicReference<Dictionary<String, Object>> propRef;
> ...
>   Dictionary<String, Object> oldProps;
>   Dictionary<String, Object> newProps;
>   do {
>     synchronized(propRef) {
>       oldProps =  propRef.get();
>       newProps = new Hashtable<>(oldProps);
>     }
>     //update newProps with new information
>     extComponentContext.setServiceProperties(newProps);
>   } while ( propsRef.compareAndSet( oldProps, newProps)}
>     



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)