Spring Boot 2.4.0-RC1 application with 1. applicationStartup(new FlightRecorderApplicationStartup()) 2. web mvc controller

starts ok, and I can see messages in JDK mission control. I can execute few requests manually in browser, and they work ok... But, when I do a load test with JMeter - i get exceptions:

java.util.NoSuchElementException: null
    at java.base/java.util.ArrayDeque.getFirst(ArrayDeque.java:402) ~[na:na]
    at org.springframework.core.metrics.jfr.FlightRecorderApplicationStartup.start(FlightRecorderApplicationStartup.java:55) ~[spring-core-5.3.0.jar:5.3.0]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:160) ~[spring-context-5.3.0.jar:5.3.0]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:426) ~[spring-context-5.3.0.jar:5.3.0]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383) ~[spring-context-5.3.0.jar:5.3.0]
    at org.springframework.web.servlet.FrameworkServlet.publishRequestHandledEvent(FrameworkServlet.java:1141) ~[spring-webmvc-5.3.0.jar:5.3.0]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1023) ~[spring-webmvc-5.3.0.jar:5.3.0]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.0.jar:5.3.0]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.39.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.0.jar:5.3.0]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.39.jar:4.0.FR]

Please find test application at: https://github.com/lvivJavaClub/jfr-demo/tree/issue_branch and very simple jmeter scenario https://github.com/lvivJavaClub/jfr-demo/blob/issue_branch/jfr_test_plan.jmx

Comment From: wilkinsona

Thanks for the sample. This looks like a Spring Framework issue to me. We'll transfer the issue to the Framework repository so that they can take a look.

Comment From: OrangeDog

It happens for me on (I think) every authenticated request at some point after starting. Application was not run with JFR attached.

java.util.NoSuchElementException: null
    at java.util.ArrayDeque.getFirst(ArrayDeque.java:403)
    at org.springframework.core.metrics.jfr.FlightRecorderApplicationStartup.start(FlightRecorderApplicationStartup.java:55)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:160)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:426)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.publishEvent(AbstractSecurityInterceptor.java:452)
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.attemptAuthorization(AbstractSecurityInterceptor.java:248)
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:208)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:113)
    ...

Comment From: bclozel

Thanks for this report.

After considering several approaches, I decided to remove the instrumentation around listeners invocations. One of the main design points behind the ApplicationStartup is about the single-threaded nature of the application context refresh phase. Listener invocation can be concurrent, so we can't guarantee proper behavior here.

We'll consider other instrumentation points and metrics with ApplicationStartup in the future (feel free to suggest some!).