skip to content

A JDK 25 HotSpot safepoint log shows Reaching safepoint near 400 ms with one runnable thread; how do you identify that late thread and the code it was running?

level: seniorimportance: should knowfreq 38%

answer

  1. who first, then where
  2. a timeout that names threads
  3. the default delay is ten seconds
  4. profile only the sync window
  5. a thread dump waits too

basics

~20 s

Name the thread with -XX:+SafepointTimeout and a SafepointTimeoutDelay below the stall, which logs the threads that had not arrived; then find its code with an async-profiler --ttsp profile, which samples only between the safepoint request and synchronization.

solid answer

~40 s

First I confirm the pattern: recurring long `Reaching safepoint` with `1 runnable`, so one thread is late rather than all of them starving for CPU. To get **who**, I restart with `-XX:+SafepointTimeout -XX:SafepointTimeoutDelay=100`; the default delay is 10,000 ms, far above a 400 ms stall. When a sync passes the delay, the JVM logs a warning listing each thread that has not arrived, with its name, ids and state but no stack. To get **where**, I run async-profiler 4.x with `--ttsp`, for example `asprof -e cpu --ttsp -t -d 300 -f ttsp.html <pid>`: it records samples only between the safepoint request and synchronization, so the late thread's frames are what fill the profile. A `jstack` does not help, because a thread dump is itself a safepoint and waits for the same thread.

code

bash · 8 lines
bash
# 1. who: log the threads that miss a 100 ms deadline (restart needed)
java -XX:+SafepointTimeout -XX:SafepointTimeoutDelay=100 \
     -Xlog:safepoint:file=/var/log/app/safepoint.log:time,uptime,level,tags \
     -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints \
     -jar app.jar

# 2. where: sample only the time-to-safepoint windows, per thread
asprof -e cpu --ttsp -t -d 300 -f /tmp/ttsp.html <pid>

go deeper

for a junior

Remember that a stop can be slow because one thread arrives late, and that the JVM can be told to name that thread.

for a middle

Explain what SafepointTimeout prints, why the delay must be set below the stall, and why its output names a thread without a stack.

for a senior

Combine the timeout warning with a --ttsp profile, rule out CPU starvation from the runnable count and an empty CPU profile, and explain why a thread dump cannot catch the culprit.

for a principal

Decide whether a low SafepointTimeoutDelay should stay on permanently on latency-critical services, weighing a cheap warning against noisy logs during expected long stops.

## The question the evidence leaves open In a JDK 25 safepoint log line, `Reaching safepoint` is the **time-to-safepoint**: from the moment the VM thread asks every Java thread to stop until the last one is safe. `Threads: 1 runnable` means that, at the first check, only one Java thread was still running Java or VM code; every other thread was blocked or in native code and already safe. So a 400 ms reaching phase with one runnable thread says that one thread took about 400 ms to reach its next safepoint poll while every other thread sat stopped. Two facts are missing: **which thread** it was, and **what code** it was running. ## Rule out the cheap explanations first 1. **Is it really one thread?** If the long reaching phases come with many runnable threads, one latecomer is still possible, but so is a process starved of CPU, for example under a container CPU quota. The throttling metrics settle that before any late-thread hunt. 2. **Does it follow one operation or all of them?** A late thread delays any operation that happens to arrive while it runs, so the operation name in the line is usually a bystander. 3. **Does it line up with a workload?** Batch jobs, report endpoints and start-up warm-up are the usual suspects for long tight loops. ## Who: SafepointTimeout `SafepointTimeout` is a product flag, `false` by default. With it on, the VM thread checks during synchronization whether the wait has passed `SafepointTimeoutDelay` milliseconds (default `10000`, fractional values allowed). If it has, HotSpot logs, once per safepoint and at warning level under the `safepoint` tag, which unified logging prints to standard output by default: ```console [2210.507s][warning][safepoint] [2210.507s][warning][safepoint] # SafepointSynchronize::begin: Timeout detected: [2210.507s][warning][safepoint] # SafepointSynchronize::begin: Timed out while spinning to reach a safepoint. [2210.507s][warning][safepoint] # SafepointSynchronize::begin: Threads which did not reach the safepoint: [2210.507s][warning][safepoint] # "report-worker-3" #87 [41233] prio=5 os_prio=0 cpu=58231.44ms elapsed=2210.41s tid=0x00007f3a2c1e8000 nid=41233 runnable [0x00007f39f8bfe000] [2210.507s][warning][safepoint] java.lang.Thread.State: RUNNABLE [2210.507s][warning][safepoint] # SafepointSynchronize::begin: (End of list) ``` What it gives you, and what it does not: - **It names the thread**: its Java name, Java thread id, native thread id, CPU time so far and state. - **It does not print a stack trace.** It says who, not where. - **It does not stop the JVM.** Aborting is a separate diagnostic flag, `AbortVMOnSafepointTimeout`, which signals the blocking thread so that a fatal error log captures its stack and then kills the process. That belongs in a reproduction, not in production. - Set the delay below the stall you are chasing (here, `-XX:SafepointTimeoutDelay=100`), or the timeout never fires. ## Where: a time-to-safepoint profile async-profiler's `--ttsp` option (async-profiler 4.x) is an alias for `--begin SafepointSynchronize::begin --end RuntimeService::record_safepoint_synchronized`. It is **not an event type but a constraint**: whichever event you choose, samples are recorded only between the safepoint request and the moment all threads are synchronized. ```bash asprof -e cpu --ttsp -t -d 300 -f /tmp/ttsp.html <pid> ``` - `-e cpu` samples threads that are on CPU, which during that window are mainly the late thread itself and the VM thread waiting for it. - `-t` splits the profile per thread, so the thread named by the timeout warning gets its own tower. - async-profiler's documentation recommends starting the JVM with `-XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints`, so inlined frames are attributed accurately rather than to the nearest safepoint. A CPU profile that stays nearly empty during long reaching phases is evidence too: the late thread was not on CPU, which points back at scheduling and CPU throttling. ## Why a thread dump is the wrong tool `jstack` and `jcmd <pid> Thread.print` run the `PrintThreads` VM operation, which itself needs a global safepoint. It waits for the same late thread and prints that thread only after it has reached its poll, so you see where it stopped, not where it spent 400 ms. It also adds a stop of its own. ## What the stack usually shows | Profile shows | Likely cause | Next check | |---|---|---| | A tight loop over an `int` index in compiled code | A counted loop compiled without a safepoint poll | The collector and the `UseCountedLoopSafepoints` setting | | Almost nothing on CPU | The thread was not scheduled | Container CPU throttling metrics | Fix only what the stack names, then compare `Reaching safepoint` for the same operations before and after.

  • Why does SafepointTimeout print the list only once per stop, and why does that matter?
    The flag that guards the printout is reset at the start of each safepoint and set on the first print, so a long stall produces one list rather than one per spin. You get a snapshot of who was still running when the delay passed, which is enough to name the thread, but not a timeline of how long each one stayed late.
  • Would a wall-clock profile with --ttsp be better than a CPU profile?
    It answers a different question. With `-e wall`, async-profiler samples all threads regardless of state during the window, so the stopped threads, which are the majority, fill the profile with the frames where they wait. A CPU profile keeps only threads on CPU, which isolates the late one. Use wall-clock mode when a CPU profile stays empty and you suspect the late thread was not on CPU at all.

  • Take a jstack dump to see where the late thread is
  • SafepointTimeout prints the late thread's stack trace
  • SafepointTimeout kills the JVM once the delay passes
  • The default SafepointTimeoutDelay is short enough for a 400 ms stall
  • async-profiler's --ttsp is its own sampling event type
  • The operation named in the line is the one causing the stall