[concurrency-interest] Spring DispatcherServlet safe publication question

Jason T. Greene jason.greene at redhat.com
Wed Mar 5 22:43:34 EST 2008


Hi Dhanji,

This is OK, since the servlet spec guarantees that init() is called only 
once, and before any requests are serviced.

Dhanji R. Prasanna wrote:
> Hello again,
> 
> This question has been bugging me for a *long* time. Nobody (and no 
> docs) seem to adequately address this problem for me.
> 
> Spring's 
> DispatcherServlet: http://fisheye1.cenqua.com/browse/springframework/spring/src/org/springframework/web/servlet/DispatcherServlet.java?r=1.100
> ...has several non-volatile, non-final fields for various pluggable 
> services (ThemeResolver, ViewResolver, etc.). These are set by equally 
> various private unsynchronized methods which cascade down from 
> HttpServlet.init(), itself also unsynchronized.
> 
> The problem I have is that this seems like it could lead to incoherent 
> values between the startup thread's view of DispatcherServlet and any 
> subsequent request threads. Since there is no lock or synchronization, I 
> cannot find a guarantee that request threads will see the value set in 
> the init thread (even though they are chronologically sequenced ok). All 
> these fields are also non-volatile. It looks like a classic unsafe 
> publication problem to me.
> 
> Worse, there are no default values, so request threads could end up 
> biting NPEs.
> 
> This seems like a gross flaw in code that's deployed virtually 
> everywhere. Why isn't there more clamor about this? Or am I just totally 
> nuts (I *hope* so)?
> I know Tim has talked for a long time about (lack of) safe publication 
> guarantees in the Spring injector itself... but I could not find any 
> concrete examples.
> 
> @Jason: Yes I have JCiP, though it was hell finding it in Australian 
> book stores--thanks for the reck! =)
> 
> Dhanji.
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


-- 
Jason T. Greene
JBoss, a division of Red Hat


More information about the Concurrency-interest mailing list