On Linux, how does jcmd attach to a running HotSpot JVM, and why does that handshake break when the tool and the JVM sit in different containers?
answer
- a file, a signal, a socket
- trigger file, then SIGQUIT
- socket named after the JVM's own PID
- NSpid and /proc/<pid>/root/tmp
- owner-only socket, matching user
basics
~20 sjcmd drops an .attach_pid trigger file, sends SIGQUIT, and waits for the JVM to open a .java_pid UNIX socket in /tmp. Across containers the PID, the /tmp directory and the user can all differ, and each mismatch breaks it.
solid answer
~50 sThe JDK's Linux attach client first looks for a `.java_pid<pid>` UNIX socket in the target's `/tmp`. If it is not there, it creates an `.attach_pid<pid>` trigger file in the target's working directory (falling back to the temp directory), sends `SIGQUIT`, and polls for the socket, giving up after about 10 seconds by default. The JVM's signal handler sees the trigger, starts its Attach Listener and creates the socket. Before connecting, the client checks that the socket belongs to its own effective user and group (root is exempt) and is readable only by its owner. Between containers all three inputs can differ: the PID in another PID namespace, `/tmp` in another mount namespace, and the user ID. The client handles the first two itself, by reading `NSpid` from `/proc/<pid>/status` and using `/proc/<pid>/root/tmp` when it can write there; the user you must match yourself.
code
console · 4 lines$ grep NSpid /proc/48213/status
NSpid: 48213 1
$ ls -l /proc/48213/root/tmp/.java_pid1
srw------- 1 1000 1000 0 Oct 18 09:12 /proc/48213/root/tmp/.java_pid1go deeper
Know that jcmd attaches locally through a file, a signal and a UNIX socket in /tmp, not over the network, and that it must run as the JVM's user.
Walk the handshake in order: NSpid lookup, the .java_pid socket, the .attach_pid trigger, SIGQUIT, the timeout, then the owner and permission checks on the socket.
Map each container boundary to the step it breaks, and read a timeout plus a thread dump in the app's log as signal crossed, filesystem did not.
Treat attachability as a property the image and pod spec must keep: a writable /tmp, a known UID and GID, and a deliberate decision on DisableAttachMechanism.
## The attach handshake, step by step **jcmd** does not talk to a JVM over the network. On Linux it uses the JDK's **attach mechanism**, a local handshake built from a file, a signal and a UNIX domain socket. The client side lives in the JDK's `sun.tools.attach.VirtualMachineImpl`, and its steps are: 1. **Resolve the PID.** The client reads `/proc/<pid>/status` and takes the last number on the `NSpid` line, which is the PID the JVM sees for itself inside its innermost PID namespace. 2. **Look for the socket.** The socket is named `.java_pid<nspid>` and lives in the target's temporary directory: `/proc/<pid>/root/tmp` when the client can write there, otherwise the client's own `/tmp`. 3. **Pull the trigger.** If the socket does not exist yet, the client creates `.attach_pid<nspid>` in the target's working directory (`/proc/<pid>/cwd`), or in that temporary directory if the first attempt fails. 4. **Send `SIGQUIT`.** Only if `/proc/<pid>/status` shows the process catching and not ignoring `SIGQUIT`; otherwise it holds back and, past the halfway point, gives up with a message that the process "is not ready to participate in attach handshake". 5. **Wait.** It polls with growing delays, re-sends the signal on each poll after the halfway point, and gives up after the `sun.tools.attach.attachTimeout` system property's value, 10,000 ms by default, with `Unable to open socket file ...: target process ... doesn't respond within ...ms or HotSpot VM not loaded`. On the JVM side, the **Signal Dispatcher** thread handles `SIGQUIT`. If it finds a trigger file it starts the **Attach Listener**, which binds the socket. The directory is a fixed `/tmp`: the client's source says the location must be the same for every process so that tools can find every HotSpot JVM. `-Djava.io.tmpdir` does not move it. ## What the client checks before it talks Before sending a command, the client stats the socket file and refuses it unless: - it is owned by the client's **effective user**, with the message `file should be owned by the current user (which is X) but is owned by Y`; - its group is the client's **effective group**, with `file's group should be the current group ...`; - no group or other access is granted, with `file should only be readable and writable by the owner ...`. A root client is exempt from the user and group checks, but not from the permission-bits check. ## Why containers break each step | Input | Same host, same user | Tool in another container | |---|---|---| | PID | one number everywhere | the JVM may be PID 1 inside, another number outside | | `/tmp` | one directory | each mount namespace has its own `/tmp` | | User | usually the same | images often run as different UIDs | - **PID namespace.** A tool in a separate PID namespace cannot see the JVM at all, so there is no PID to resolve and no process to signal. - **Mount namespace.** A tool with its own root filesystem has its own `/tmp`. The client can reach the target's files only through `/proc/<pid>/root` and `/proc/<pid>/cwd`, and the kernel usually refuses those paths for a process owned by another user unless the caller holds extra privileges. - **User.** Even when everything is reachable, a socket owned by UID 1000 is rejected by a client running as UID 2000. ## How the JDK compensates, and where it cannot The `NSpid` lookup and the `/proc/<pid>/root/tmp` path exist so that a tool on the host, or in a container sharing only the PID namespace, can still attach. Both depend on reaching `/proc/<pid>/root`. When that fails, the client silently falls back to its **own** `/tmp`, writes the trigger where the JVM never looks, and waits for a socket that will never appear there. ## Flags that change the handshake - `-XX:+StartAttachListener` (default `false`) starts the listener and its socket at JVM startup, so a client that can already see the socket connects without the trigger and signal. - `-XX:+DisableAttachMechanism` (default `false`) turns attach off: jcmd and every other attach-based tool fail. - `-Xrs` sets `ReduceSignalUsage`, and the JVM then starts no Signal Dispatcher thread, so the lazy, signal-driven start has nothing to answer it. ## What a failed handshake leaves behind A `SIGQUIT` that arrives **without** a trigger file the JVM can find is not wasted: HotSpot treats it as a thread-dump request and prints every thread's stack, plus a deadlock check, to its **stdout**. In a pod that is the container log. A thread dump appearing in the application's log at the moment someone's jcmd timed out is a strong clue: the signal crossed the PID boundary, the file did not cross the filesystem boundary.
- Why does setting -Djava.io.tmpdir on the JVM not move the attach socket?The Linux attach client hard-codes `/tmp` as the well-known directory for `.java_pid` and `.attach_pid` files, and its source notes that HotSpot must use the same location so every tool finds every JVM. `java.io.tmpdir` changes where Java code puts its temporary files, not where the Attach Listener binds. A pod with a read-only root filesystem therefore needs a writable volume mounted at `/tmp` itself if anyone is to attach.
- What do -XX:+StartAttachListener and -XX:+DisableAttachMechanism each change?`StartAttachListener`, `false` by default, creates the listener and its socket at startup, so a client that can see the socket skips the trigger file and `SIGQUIT`. It does not help a client that cannot reach the JVM's `/tmp`. `DisableAttachMechanism`, also `false` by default, switches attach off entirely: jcmd fails, and `SIGQUIT` still prints a thread dump to the JVM's stdout.
It is a doorbell with a note on the doormat: the bell alone only makes the house shout its status out of the window, and it opens the door only when the note is lying on the right doormat.
- jcmd talks to the JVM over a TCP port, the way JMX does.
- Passing -Djava.io.tmpdir moves the attach socket to another directory.
- The socket is named after the PID that ps shows on the node.
- A JVM must be started with a special flag before jcmd can attach.
- A jcmd attempt that times out leaves the target JVM untouched.