felix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sahoo <sanjeeb.sa...@oracle.com>
Subject Possible synchronization issue with BundleContext.getBundle()
Date Mon, 27 Dec 2010 04:24:09 GMT
Hi,

Looking at the code, it seems to me that BundleContext.getBundle() can 
return a bundle even though the BundleContext has been invalidated. I 
say this based on the following reasoning:

public BundleContext.getBundle()
{
         checkValidity();

         return m_bundle;
}

private void checkValidity() // non-synchronized method
{
         if (m_valid) // m_valid is a non-volatile field
         {
             switch (m_bundle.getState())
             {
                 case Bundle.ACTIVE:
                 case Bundle.STARTING:
                 case Bundle.STOPPING:
                     return;
             }
         }

         throw new IllegalStateException("Invalid BundleContext.");

}

protected void invalidate()    {        m_valid = false;    }

class Bundle  {
   public int getState() // non-synchronized method
     {
         return m_state;
     }
}

time t1: thread1 has access to bundleContext.
time t2: thread2 calls b.update()
time t3: bundle has been stopped and is now in STARTING state. thread1 
calls bundleContext.getBundle().
Since m_valid is not a volatile field, it can see m_valid as true, 
although thread2 would have set it to false. Since m_state is volatile, 
it sees its value as STARTING. So, it fails to detect invalidation of 
the bundle and is returned with the bundle object instead of 
IllegalStateException.

Am I missing anything here?

Thanks,
Sahoo

Mime
View raw message