[ 
https://issues.apache.org/jira/browse/FELIX-5887?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16774947#comment-16774947
 ] 

Todor Boev commented on FELIX-5887:
-----------------------------------

I tested this issue on SCR {{2.1.15-SNAPSHOT}} and it is fixed.

The setup I had to use to make it work was something like this:
{code:java}
@Component(service = Top.class)
class Top {
    @Reference(policy=DYNAMIC, cardinality=MULTIPLE, policyOption=GREEDY)
    void addBottom(Bottom bot)

    void removeBottom(Bottom bot)
}

@Component(service = Bottom.class)
class Bottom {
    @Reference
    void setTop(Top top)
}{code}
I could not get it to work with an injection of {{List<Bottom>}} in the {{Top}} 
component.

Also when it works the loop it closed by a different thread than the one that 
initially started the component creation. When SCR detects the loop it leaves 
it open, completes the component creation and returns it to the caller. It also 
submits a task on an internal work queue to complete the loop later. I.e. 
you'll get your {{Top}} object first and later {{Top.addBottom}} get's called 
on a different thread.

> Cycles in DS depending on bundle order
> --------------------------------------
>
>                 Key: FELIX-5887
>                 URL: https://issues.apache.org/jira/browse/FELIX-5887
>             Project: Felix
>          Issue Type: Bug
>          Components: Declarative Services (SCR)
>    Affects Versions: scr-2.0.14, scr-2.1.0
>         Environment: MacOS + Windows + Linux 
>            Reporter: Marko Herchet
>            Priority: Critical
>         Attachments: TestProjects.7z
>
>
> Following test case:
> top.jar: Top @Reference volatile List<Bottom>
>  bottom.jar: Bottom @Reference Top
> In SCR 2.0.2:
>  top.jar, bottom.jar -> OK, see trace
>  new Top()
>  Top.activate()
>  new Bottom()
>  Bottom.top=<top>
>  Bottom.activate()
>  Top.bottom +=<bottom>
> bottom.jar, top.jar -> FAILS but recovers
>  [osgi.enroute.examples.concurrency.cycle.bottom.Bottom(1)] 
>  Circular reference detected, getService returning null
>  new Top()
>  Top.activate()
>  new Bottom()
>  Bottom.top=<top>
>  Bottom.activate()
>  Top.bottom +=<bottom>
> In SCR 2.0.4 and later
> top.jar,bottom.jar -> OK
> new Top()
>  Top.activate()
>  new Bottom()
>  Bottom.top=<top>
>  Bottom.activate()
>  Top.bottom +=<bottom>
> bottom.jar, top.jar -> FAILS and DOES NOT RECOVER!
>  new Bottom()
>  new Top()
>  Top.activate()
>  new Bottom()
> The Bottom component never gets added to Top. This is a very serious problem 
> for reliable systems. I see this behaviour on 2.0.4, 2.0.6, and 2.0.8
> ----------
>  I've traced the cycle problem in 2.0.2. What happens is:
>  1) The Bottom configuration is started. There is no Top, so it is not 
> satisfied
>  2) The Top configuration is started. It is satisfied since it can live with 
> 0 Bottoms. 
>  3) The top configuration registers Top
>  4) The bottom configuration sees a Top and initiates Bottom
>  5) The bottom configuration gets Top (which is under construction)
>  6) The top configuration looks at its dependencies
>  7) The top configuration sees there is a Bottom now and tries to get it
>  8) The bottom factory is called again on the same thread and blows up
> I am not sure what happens in 2.0.4 and later but it does look scary that 
> Bottom is now never added to the Top.
> ---- Fix
>  I tried to do the activation in a background thread that but that failed. I 
> think the cycle should be detected and and de Top should not get its dynamic 
> dependencies after the activate method returned. But, just a guess, this is 
> complicated stuff.
> ---- Workaround
>  The workaround is to not let Top register as a service but manually register 
> it at the end of the activate method. Since the service is then already 
> initialized and registered, the Bottom will not get activated until the Top 
> is done.
> There is a bnd workspace that consistently shows this output.
>  [https://github.com/pkriens/felix.scr.cycle]
> I've added 4 log files. 2.0.4 and 2.0.8 seem to act very similar



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to