3

When attempting to attach an agent jar file onto another process running in java, I have came across the exception:

com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded

I was running linux, with java Oracle JDK 8_101, however after answering this question I've realized the O.S does not matter for the cause of this problem.

Edit: Answer:

If you encounter this problem, the reason it occured for me is because I was launching a program from a different JVM, other than the default JVM specified for the system.

i.e)

Program A (The launcher), is running on JVM-1 (JDK_8_1 for example, or JDK_8_1/jdk/jre).

Program A (The launcher), creates a process with java -jar programB.jar

Program B (The target), is running on the system's default JVM, JVM-2 (JDK_8_2 for example, or JDK_8_2/jre).

Program A (The launcher) CANNOT attatch to Program B (The target), because the JVM Program A (The launcher) is running on, does not match the JVM of which Program B (The target) is running on, thus throwing the com.sun.tools.attach.AttachNotSupportedException:

Joe
  • 1,316
  • 9
  • 17
  • You might increase the attach timeout (when it's related to timeout and not to permissions) `-Dsun.tools.attach.attachTimeout=10000` (the default is 5000). Could you post some code to reproduce the issue? – SubOptimal Jan 22 '18 at 13:02
  • Duplicate of a great https://stackoverflow.com/questions/26140182/running-jmap-getting-unable-to-open-socket-file – gavenkoa Dec 21 '20 at 20:40

2 Answers2

10

Common reasons for this problem:

  • Attach socket /tmp/.java_pid1234 has been removed (e.g. by a scheduled job that periodically cleans up /tmp).
  • Target JVM is started with -XX:+DisableAttachMechanism option.
  • Garbage Collection or other long VM operation (e.g. Heap Dump) is in progress.
  • JVM cannot reach safepoint within attach timeout. This happens rarely, and the problem is typically intermittent.
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Thanks my man! Going to check out any schedulers for the tmp folder removal. Is there any way to specify the location which the file gets stored? – Joe Jan 30 '18 at 23:15
  • 1
    @JoeD Unfortunately, the location is hard-coded inside JVM. However, a process can be run in its own [mount namespace](http://man7.org/linux/man-pages/man1/unshare.1.html) so that `/tmp` of a JVM process will differ from `/tmp` of the rest system. – apangin Jan 30 '18 at 23:34
  • For me it happened due to the missing `/tmp/.java_pidXXX file` (happened on HotSpot version 1.8.0_281 and RHEL 8.3 OS). But another time it was due to using another user to launch the jcmd/jmap command as explained in https://stackoverflow.com/a/51507597/1996150 answer. – Mariano Paniga May 11 '23 at 08:10
4

Problem: different user executing jcmd

It might happen, that the user calling jcmd is different, than the user running the proccess.

Example:

  • user invoking jcmd as root
  • user running JVM as fancyUser

Solution:

On Linux try to run jcmd with the same user, as the process is running.

When you have such a scenario, you will get given error.

Problem: AppArmor

When AppArmor is enabled for running JVM instance, which restricts sys-calls, it might be the case, that opening a socket-connection is restricted.

Solution:

Change AppArmor-Profile for process

Sergej Isbrecht
  • 3,842
  • 18
  • 27