question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.IllegalStateException: Could not self-attach to current VM using external process

See original GitHub issue

When trying to start Elastic APM agent the following issue is encountered :

2022-09-09 07:37:15,776 [Attach Listener] INFO  co.elastic.apm.agent.util.JmxUtils - Found JVM-specific OperatingSystemMXBean interface: com.sun.management.OperatingSystemMXBean
2022-09-09 07:37:15,850 [Attach Listener] INFO  co.elastic.apm.agent.configuration.StartupInfo - Starting Elastic APM 1.28.4 as DocSignManagerAPI on Java 11.0.16.1 Runtime version: 11.0.16.1+1-LTS VM version: 11.0.16.1+1-LTS (Red Hat, Inc.) Linux 3.10.0-1160.53.1.el7.x86_64
2022-09-09 07:37:15,855 [Attach Listener] INFO  co.elastic.apm.agent.configuration.StartupInfo - enable_log_correlation: 'true' (source: Attachment configuration)
2022-09-09 07:37:30.516  WARN 1 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'APMConfig': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Could not self-attach to current VM using external process


2022-09-09 07:37:39.540 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'APMConfig': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Could not self-attach to current VM using external process
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:440) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.21.jar!/:5.3.21]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.21.jar!/:5.3.21]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.1.jar!/:2.7.1]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.1.jar!/:2.7.1]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.1.jar!/:2.7.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.1.jar!/:2.7.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.1.jar!/:2.7.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.1.jar!/:2.7.1]
	at com.ing.document.signing.DocSignManagerAPIApplication.main(DocSignManagerAPIApplication.java:20) ~[classes!/:1.1.3-SNAPSHOT]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[app.jar:1.1.3-SNAPSHOT]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[app.jar:1.1.3-SNAPSHOT]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[app.jar:1.1.3-SNAPSHOT]
	at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467) ~[app.jar:1.1.3-SNAPSHOT]
**Caused by: java.lang.IllegalStateException: Could not self-attach to current VM using external process
	at co.elastic.apm.attach.bytebuddy.agent.ByteBuddyAgent.installExternal(ByteBuddyAgent.java:701) ~[apm-agent-attach-1.28.4.jar!/:na]**
	at co.elastic.apm.attach.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:632) ~[apm-agent-attach-1.28.4.jar!/:na]
	at co.elastic.apm.attach.bytebuddy.agent.ByteBuddyAgent.attach(ByteBuddyAgent.java:299) ~[apm-agent-attach-1.28.4.jar!/:na]
	at co.elastic.apm.attach.ElasticApmAttacher.attach(ElasticApmAttacher.java:154) ~[apm-agent-attach-1.28.4.jar!/:na]
	at co.elastic.apm.attach.ElasticApmAttacher.attach(ElasticApmAttacher.java:140) ~[apm-agent-attach-1.28.4.jar!/:na]
	at co.elastic.apm.attach.ElasticApmAttacher.attach(ElasticApmAttacher.java:105) ~[apm-agent-attach-1.28.4.jar!/:na]
	at co.elastic.apm.attach.ElasticApmAttacher.attach(ElasticApmAttacher.java:58) ~[apm-agent-attach-1.28.4.jar!/:na]
	at com.ing.document.signing.config.APMConfig.initAPM(APMConfig.java:17) ~[classes!/:1.1.3-SNAPSHOT]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.3.21.jar!/:5.3.21]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.3.21.jar!/:5.3.21]
	... 26 common frames omitted

Elastic APM version : 1.28.4 Java version : 11.0.16 VM OS : Linux 3.10.0-1160.53.1.el7.x86_64

Do you have any clues of what can cause

java.lang.IllegalStateException: Could not self-attach to current VM using external process

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
felixbarnycommented, Sep 14, 2022

I think we should catch the exception and avoid the application from crashing if the agent can’t be attached.

Compared to 1.28.4, we’re now always attaching in a try/catch block so that exceptions on attachment don’t impact the application:

https://github.com/elastic/apm-agent-java/blob/00b682310082cbc40d3216c8cb1a48a8af6aeee4/apm-agent-attach/src/main/java/co/elastic/apm/attach/ElasticApmAttacher.java#L163-L179

0reactions
felixbarnycommented, Sep 14, 2022

I agree that logging std err is not the best way to deal with it (maybe we should even use JUL for that?). However, I do strongly believe that we should not let exceptions escape our API to avoid risking to crash the application on startup. At most, we may want to return a boolean indicating whether the attachment was successful. But it seems like an edge case to me that users would want to act on the success of attachment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug: IllegalStateException: Could not self-attach to current VM ...
We have main code in Java and tests in Kotlin. ... Could not self-attach to current VM using external process java.lang.
Read more >
java - Mockito shipped within Spring Boot fails to attach to JVM ...
Review JAVA_HOME and the different Java installations on my machine in case the process was trying to attach to the wrong Java as...
Read more >
" Self-attach to current VM error " using Lucee 5.3 (Docker ...
Error Type: java.lang.IllegalStateException. Could not self-attach to current VM using external process. The file being accessed is on windows network share ...
Read more >
"Can not attach to current VM" error message on trying to ...
java.lang.RuntimeException: java.io.IOException: Can not attach to current VM. at mockit.internal.startup.AgentLoader.attachToRunningVM(AgentLoader.java:152).
Read more >
Investigate impact of self-attach flag requirement on plugins
The workaround for this exception is to add "-Djdk.attach.allowAttachSelf=true" in the java command line. Except that the file leak detector plugin does not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found