I have a server running which crashed multiple times by getting stuck in Unsafe.defineAnonymousClass while trying to create a lambda instance. Here is an example from one of the stack traces:
"MessageIterationThread (false, 1059)" #163386 prio=5 os_prio=0 cpu=56.89ms elapsed=28566.80s tid=0x00007fef183bb000 nid=0x78f6 runnable [0x00007fece0693000]
java.lang.Thread.State: RUNNABLE
at jdk.internal.misc.Unsafe.defineAnonymousClass0(java.base@11.0.2/Native Method)
at jdk.internal.misc.Unsafe.defineAnonymousClass(java.base@11.0.2/Unsafe.java:1223)
at java.lang.invoke.InnerClassLambdaMetafactory.spinInnerClass(java.base@11.0.2/InnerClassLambdaMetafactory.java:320)
at java.lang.invoke.InnerClassLambdaMetafactory.buildCallSite(java.base@11.0.2/InnerClassLambdaMetafactory.java:188)
at java.lang.invoke.LambdaMetafactory.metafactory(java.base@11.0.2/LambdaMetafactory.java:329)
at java.lang.invoke.LambdaForm$DMH/0x00000008002e6840.invokeStatic(java.base@11.0.2/LambdaForm$DMH)
at java.lang.invoke.Invokers$Holder.invokeExact_MT(java.base@11.0.2/Invokers$Holder)
at java.lang.invoke.BootstrapMethodInvoker.invoke(java.base@11.0.2/BootstrapMethodInvoker.java:127)
at java.lang.invoke.CallSite.makeSite(java.base@11.0.2/CallSite.java:307)
at java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(java.base@11.0.2/MethodHandleNatives.java:258)
at java.lang.invoke.MethodHandleNatives.linkCallSite(java.base@11.0.2/MethodHandleNatives.java:248)
at some random point in my code using this::someMethod
Crashing meaning a thread got stuck at a point where he blocked others and caused a deadlock like scenario. The point where the lamda creation gets stuck seems to be completly random. There are even multiple threads getting stuck at completly different points in the code. At least one of them compromising the whole server.
I found this post which looked like a problem which could be related and ensured that this was not the problem. Java8 hangs up if getStackTrace() is called by one thread and lambda definition (via Unsafe.defineAnonymousClass) occurs in another thread
The server runs on an Ubuntu 18.04.4 LTS
and the JVM is OpenJDK 64-Bit Server VM (11.0.2+9 mixed mode)
. The code is compiled with the same OpenJDK. These are the starting parameters:
-Xmx4000m
-Xms4000m
-XX:NewSize=128m
-Xss400k
-XX:CMSInitiatingOccupancyFraction=70
-XX:+UseConcMarkSweepGC
-XX:+CMSParallelRemarkEnabled
-XX:-OmitStackTraceInFastThrow
-XX:MaxJavaStackTraceDepth=0
-XX:PermSize=200m
-XX:MaxPermSize=512m
-XX:ReservedCodeCacheSize=256m
FunFact: There is another instance of the server running on the same physical machine with a way higher usage which seems to not have this problem. The server with the problem is the testing environment with less traffic but much more frequent restarts.
I would be happy for suggestions what could possibly get this methode call stuck or explanations about what this method does in the native part.