[concurrency-interest] Spring DispatcherServlet safe publication question

Peter Veentjer alarmnummer at gmail.com
Mon Mar 10 04:51:15 EDT 2008


The code looks a lot like the double checked locking problem

There is one visibility problem at least:
There is no (visible) happens before relation between the write to the
instance variable and the read of the instance variable in the
outermost if(instance==null) check. So it could be that changes made
in the loadServlet are not visible in threads that access the instance
once it has been created.

And gobbling up an exception? My god.. that is asking for problems

On Mon, Mar 10, 2008 at 7:32 AM, Jed Wesley-Smith <jed at atlassian.com> wrote:
> David Holmes wrote:
>  > Jed Wesley-Smith wrote:
>  >
>  >> The key being "after ... properly initialized" - I would
>  >> posit that most servlet containers would implement a _happens before_
>  >> relationship here,  therefore all will be well.
>  >>
>  >
>  > I would posit that servlet containers would need to implement a
>  > happens-before relationship here, but whether the writers of such servlet
>  > containers are actually aware of that need, is a different matter entirely.
>  > ;-)
>  >
>
>  hehe. My initial posit was based on the assumption (dangerous I know)
>  that there is quite likely _some kind_ of synchronisation around the
>  initialisation of the Servlet instances that causes this happens-before
>  relationship _not necessarily directly related to the spec_. I.e.
>  Servlets generally work as much perhaps by accident as by design :-)
>
>  As support for this argument I was going to present Tomcat's
>  org.apache.catalina.core.StandardWrapper class that caches a Servlet
>  instance (if it doesn't implement SingleThreadedModel). When I looked I
>  saw the following:
>
>     public Servlet allocate() {
>         ...
>             // Load and initialize our instance if necessary
>
>             if (instance == null) {
>                 synchronized (this) {
>                     if (instance == null) {
>                         try {
>                             instance = loadServlet();
>                         } catch (Exceptions etc) {}
>                     }
>                 }
>             }
>         ...
>             return (instance);
>         ...
>     }
>
>  The instance variable is (of course) not volatile.
>
>  I am now backing away slowly and quietly.
>
>
>  > Safe-publication is the forgotten topic of "Threads 101" :(
>  >
>
>  Unfortunately and clearly true.
>
>
> _______________________________________________
>  Concurrency-interest mailing list
>  Concurrency-interest at altair.cs.oswego.edu
>  http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>


More information about the Concurrency-interest mailing list