skip to content

A JVM service that calls a native image-processing library through JNI dies several times a week with exit code 134, leaving hs_err_pid<pid>.log files. How do you establish whose bug it is?

level: seniorimportance: should knowfreq 30%

answer

  1. collect every crash log, compare
  2. same frame or a moving frame
  3. thread state _thread_in_native
  4. Current CompileTask means the JIT
  5. -Xcheck:jni in a test environment

basics

~20 s

Compare the problematic frames and crashing threads across several hs_err files. A C frame in the library with the thread in _thread_in_native points at the library or the JNI glue; a compiler thread points at the JIT; V frames in GC threads need native corruption ruled out.

solid answer

~50 s

Exit code 134 is `SIGABRT` (128 + 6): HotSpot's error handler aborts the process after writing the log, so 134 says only that it crashed, not why. I gather every `hs_err` file and compare them. If the `Problematic frame:` is the same `C [libimgproc.so+…]` function each time, and `Current thread` is a Java worker in `_thread_in_native` whose native stack runs from the `Java_…` JNI entry into the library, the bug is in the library or in how our JNI code calls it (a bad length, a released array, a buffer used from two threads). If the crashing thread is a compiler thread with a `Current CompileTask:` block, it is a JIT problem. If the frames move around and include `V` frames in collector threads, I suspect the native code is corrupting the Java heap. Then I reproduce in a test environment with `-Xcheck:jni` and the same inputs before changing anything.

go deeper

for a junior

Recall that exit code 134 is SIGABRT, and that each crash leaves an hs_err file naming the crashing frame and thread.

for a middle

Read the thread state, the native stack from the Java_ entry into the library, and the Current CompileTask block, and know what each implies.

for a senior

Compare several logs, separate glue bugs from library bugs with -Xcheck:jni in a test environment, and refuse to blame the collector until native corruption is ruled out.

for a principal

Weigh isolating crash-prone native code in a separate process against the latency of crossing a process boundary, and set how a native library upgrade is proven safe.

## Start from the exit code, then leave it behind A Linux process killed by a signal is reported by shells and container runtimes as **128 plus the signal number**. HotSpot's error handler ends a crash by calling `abort()`, which raises `SIGABRT` (signal 6), so a crash with the default `-XX:+CreateCoredumpOnCrash` typically exits with **134**, even when the original fault was a `SIGSEGV`. The exit code therefore says "HotSpot crashed", and the `hs_err` files say why. ## Collect, then compare One crash log is an anecdote; five are a pattern. For each file, note: - the **signal line** and whether the pc is the same offset each time; - the **`Problematic frame:`** and its letter (`C` native outside the VM, `V` inside the VM, `v` VM-generated code, `J` compiled Java, `j` interpreted Java); - the **`Current thread`** line in the THREAD section: its name, its type and its state; - the **native and Java stacks** below it; - the **JDK build** from the version line, and whether the library version changed between crashes. An excerpt of the THREAD section, built from the JDK 25 print statements, for the classic shape: ```console --------------- T H R E A D --------------- Current thread (0x00007f3b2c01e800): JavaThread "thumb-worker-3" [_thread_in_native, id=4242, stack(0x00007f3a9b8ff000,0x00007f3a9b9ff000) (1024K)] Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) C [libimgproc.so+0x1a2b4] resize_rgb+0x54 C [libimgproc.so+0x19f00] Java_com_example_imaging_NativeResize_resize+0x120 j com.example.imaging.NativeResize.resize([BII)[B+0 j com.example.imaging.Thumbnailer.render(Ljava/nio/file/Path;)[B+57 ``` ## Reading the four common shapes | What the logs show | Most likely owner | What rules it in | |---|---|---| | Same `C` frame in the library, Java worker in `_thread_in_native` | the native library, or your JNI glue calling it | stack runs from `Java_…` into the library; header says the crash happened outside the JVM in native code | | Crashing thread is a compiler thread with `Current CompileTask:` | the JIT compiler | the task line names the method being compiled; a `replay_pid<pid>.log` compiler replay file is normally written to the working directory | | `J` frame in one method, only after warm-up | compiled code or an intrinsic | the crash vanishes when that method is excluded from compilation | | Moving frames, `V` frames in GC worker threads | memory corruption, often from native code | different frame each time; the collector crashes while walking a damaged object | The thread state matters. `_thread_in_native` means the Java thread had called into native code and had not returned: the VM was not running on that thread. HotSpot's own header line, `The crash happened outside the Java Virtual Machine in native code.`, is printed when the crashing Java thread is in that state. ## Telling your JNI code from the library The native stack separates the two layers. The frame named `Java_<package>_<class>_<method>` is the JNI entry point, the glue your team usually writes; the frames above it are the library. Typical glue bugs: - using the pointer from `GetPrimitiveArrayCritical` or `GetByteArrayElements` after releasing it; - passing the Java array length where the library expects a byte count, or the reverse; - caching a local reference across calls, or calling JNI functions with an exception pending; - sharing one native context between threads when the library is not thread-safe, which shows as crashes only under load. To separate them, run the same inputs in a test environment with **`-Xcheck:jni`** (it sets `CheckJNICalls`, described in the source as "Verify all arguments to JNI calls", and reports the misuse it detects), and log the inputs the crashing worker was handling. If the library crashes on a specific image even through a minimal correct caller, it is the library's bug; report it with that input and the `hs_err` file. Code written with the Foreign Function & Memory API instead of JNI fails the same way when it passes a wrong address or size to a downcall, and is read the same way. ## Ruling out the JIT and the collector If the crash is in compiled code or a compiler thread, a discriminating experiment is to exclude the suspect method from compilation (`-XX:CompileCommand=exclude,<class>::<method>`) in a test environment: a crash that disappears names the compiler. If `V` frames appear in GC threads, do not file a collector bug while a native library writes into Java arrays; first take the native code out of the path (a pure-Java fallback, or the work moved to a separate process) and see whether the collector crashes stop. ## When the log is not enough A crash log holds the crashing thread's stacks, its registers and a one-line entry per other thread. When the question is which object or buffer was damaged, a **core dump** of the crash is the next piece of evidence, and the header line `Core dump will be written` or `No core dump will be written` says whether you have one. Only after the evidence names an owner do you change one thing (a library upgrade, a glue fix, isolating the library) and compare the crash rate over the same number of days.

  • Why might the team move the native library into a separate process even after fixing the known bug?
    Because any memory bug in native code running in the JVM's address space kills the whole JVM and every request it was serving, and can corrupt the Java heap in ways that crash elsewhere. A separate worker process limits the damage to that worker, which can be restarted, at the cost of serialising images across a process boundary.
  • The crashing thread in every log is 'C2 CompilerThread0' with a Current CompileTask block. What do you collect and try?
    The compile task line names the method being compiled, and by default (`DumpReplayDataOnError` is on) a `replay_pid<pid>.log` compiler replay file is normally written to the working directory. Keep both for the bug report. In a test environment, exclude that method with `-XX:CompileCommand=exclude,<class>::<method>`; if the crash stops, it is a compiler bug, and the exclusion is a stopgap while you check whether a newer JDK update fixes it.

  • Treats exit code 134 as the cause instead of reading the hs_err files.
  • Blames the collector for V frames while a native library writes into Java arrays.
  • Diagnoses from a single crash log without comparing the others.
  • Changes JVM flags before any crash log names an owner.
  • Assumes JNI glue code cannot crash because it is only a thin wrapper.