I'm using org.apache.commons.math3.distribution.NormalDistribution
in a large distributed Scala & Akka application. During debugging I found sample()
was occasionally returning NaN, which propagated silently and caused threads to hang in org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator
The NaN can be reproduced simply with parallel colelctions (doesn't happen in sequential code):
val normal = new NormalDistribution(0,0.1)
(1 to 1000000000).par.foreach{i =>
val r = normal.sample
if(r.isNaN()) throw new Exception("r = "+r)
}
Obviously moving the val normal
inside the foreach
solves the issue in this case.
I've looked at the docs but can't see anything warning me of such issues. Have I failed to grasp a more fundamental concept about thread safety? Needless to say I'm now checking for NaN.