Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 309261 - System bundle becomes active before the other bundles are started
Summary: System bundle becomes active before the other bundles are started
Status: RESOLVED FIXED
Alias: None
Product: Equinox
Classification: Eclipse Project
Component: Framework (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows Vista
: P3 normal (vote)
Target Milestone: 3.6 M7   Edit
Assignee: Thomas Watson CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 309374
  Show dependency tree
 
Reported: 2010-04-15 02:21 EDT by Lazar Kirchev CLA
Modified: 2010-04-16 09:24 EDT (History)
4 users (show)

See Also:


Attachments
Proposition for a patch for the problem. (1.57 KB, patch)
2010-04-15 03:14 EDT, Lazar Kirchev CLA
tjwatson: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Lazar Kirchev CLA 2010-04-15 02:21:37 EDT
Build Identifier: 3.5.2

The system bundle is put in ACTIVE state just before the bundles are started, and a FrameworkEvent.STARTED is fired after starting these bundles. According to the OSGi spec, the system bundle should be put in ACTIVE state after the bundles are started - just before firing the event FrameworkEvent.STARTED. The exact code is in the method StartLevelManager.doSetStartLevel(…). There is a comment in the code, that putting the bundle in ACTIVE state “should be done just before firing the STARTED event for the system bundle” but is done earlier, because “some depend on the system bundle being in the ACTIVE state when they are starting”.

For us this is a problem, because we are implementing logic which depends on the system bundle being put in ACTIVE state after all bundles, which should be running, are started. In some cases listening for the FrameworkEvent.STARTED can be used, but only if we are sure that we listen for this event when it is fired. If the code, which listens for it, is executed after the framework is started, then it will never get the event. This happens in an Activator of a bundle – it may be executed during the initial start of the framework, or after the framework is started (for example on the first start of the bundle after installing it). Therefore we need to be able to check the framework state in one and the same way in both cases.

Reproducible: Always
Comment 1 Lazar Kirchev CLA 2010-04-15 03:14:14 EDT
Created attachment 164939 [details]
Proposition for a patch for the problem.

A proposed patch for the problem - it moves the code for putting the system bundle in ACTIVE state just before the code for firing FrameworkEvent.STARTED.
Comment 2 Thomas Watson CLA 2010-04-15 14:58:37 EDT
I found a case where some code was making the invalid assumption that the system bundle is in the ACTIVE state while setting the beginning start-level.  See bug 309374.

I worry that there may be others.  But that one at least needs to be fixed before this patch can be released.
Comment 3 Thomas Watson CLA 2010-04-15 15:13:22 EDT
(In reply to comment #0)

> For us this is a problem, because we are implementing logic which depends on
> the system bundle being put in ACTIVE state after all bundles, which should be
> running, are started. In some cases listening for the FrameworkEvent.STARTED
> can be used, but only if we are sure that we listen for this event when it is
> fired. If the code, which listens for it, is executed after the framework is
> started, then it will never get the event. This happens in an Activator of a
> bundle – it may be executed during the initial start of the framework, or after
> the framework is started (for example on the first start of the bundle after
> installing it). Therefore we need to be able to check the framework state in
> one and the same way in both cases.
> 

I am not arguing against the change to Equinox to set the system bundle state to ACTIVE after incrementing the start-level to the beginning start-level and just before firing the BundleEvent.STARTED and FrameworkEvent.STARTED for the system bundle.  But the assumption above still has some timing issues that need to be handled carefully.

In order to make this work correctly (after applying the attached framework patch) you must make sure you add your FrameworkListener (listening for FrameworkEvent.STARTED) before checking the state of the system bundle.  If you only add your FrameworkListener after checking (systemBundle.getState() == STARTING) then you will have a small window where the system bundle state could change to ACTIVE and the event FrameworkEvent.STARTED could get fired before you add your listener and you will miss the event.  Something like the following is needed:

  myContext.addFrameworkListener(myListener);
  if (systemBundle.getState() != Bundle.STARTING)
    myContext.removeFrameworkListener(myListener);
Comment 4 Lazar Kirchev CLA 2010-04-16 05:55:28 EDT
(In reply to comment #3)

> I am not arguing against the change to Equinox to set the system bundle state
> to ACTIVE after incrementing the start-level to the beginning start-level and
> just before firing the BundleEvent.STARTED and FrameworkEvent.STARTED for the
> system bundle.  But the assumption above still has some timing issues that need
> to be handled carefully.
> 
> In order to make this work correctly (after applying the attached framework
> patch) you must make sure you add your FrameworkListener (listening for
> FrameworkEvent.STARTED) before checking the state of the system bundle.  If you
> only add your FrameworkListener after checking (systemBundle.getState() ==
> STARTING) then you will have a small window where the system bundle state could
> change to ACTIVE and the event FrameworkEvent.STARTED could get fired before
> you add your listener and you will miss the event.  Something like the
> following is needed:
> 
>   myContext.addFrameworkListener(myListener);
>   if (systemBundle.getState() != Bundle.STARTING)
>     myContext.removeFrameworkListener(myListener);

Thanks for the patch for bug 309374. 

As for the timing issue, you are absolutely right in general. But in our particular use case if we can count on a correct state of the system bundle, it will be enough to check the state when starting and we will not have to register a listener for the FrameworkEvent.STARTED at all.
Comment 5 Thomas Watson CLA 2010-04-16 09:24:31 EDT
Patch released.  Thanks Lazar!
Comment 6 Thomas Watson CLA 2010-04-16 09:24:53 EDT
Comment on attachment 164939 [details]
Proposition for a patch for the problem.

Marking for iplog.