h3.c icon indicating copy to clipboard operation
h3.c copied to clipboard

Deadlock (not corruption) at 243 frames: `_MTLCommandBuffer waitUntilCompleted` never returns

Open ChunxueLi opened this issue 3 weeks ago • 2 comments

Deadlock (not corruption) at 243 frames: _MTLCommandBuffer waitUntilCompleted never returns

Environment

  • Apple M4 Max (36 GiB unified memory), macOS 26.6.2 (Build 25G83)
  • h3.c at 8974cc0 plus PR #77 merged (SDPA query-block splitting compiled in)
  • Official original BF16 FL2VA shards, --ssd-streaming, no quantization
  • Also reproduced on a fresh reboot with nothing else running

Symptom

At --frames 243 (1248x704, 7 or 4 steps), denoising stalls at step 0 or mid-chain and never progresses. sample(1) on the hung process shows the main thread parked in:

h3_generate → h3_dit_denoise_euler_preview
  -[_MTLCommandBuffer waitUntilCompleted]  (2526/2526 samples)
    __psynch_cvwait

CPU ~0%. The command buffer never completes. Killing and retrying the exact same command hangs at the same place. No error, no crash log, no MTLCompiler messages.

What makes it confusing

  • The same prompt and anchors at 107 frames (latent_t=27, sequence ≈ 23166 rows) completes fine — 20+ runs across days, night and day, clean reboots, zero failures.
  • 243 frames (latent_t=61, sequence ≈ 52338 rows) hung on a clean boot, GPU idle, single process, 48% memory free.
  • Attempts that did not help:
    • PR #77 SDPA query splitting (H3_SDPA_MAX_QUERY_ROWS=2048) — PROFILE confirms blocks active (rows=64378 heads=56 query-block=2048), still deadlocks
    • H3_GPU_SAMPLER_WINDOW=1 + H3_DIT_COMMAND_BLOCKS=4 (command-chain split) — got further (2/4 steps) but parked at the same waitUntilCompleted
    • Killing ToDesk / screen-capture software, freeing memory, fresh reboot
  • Meanwhile a 107f run with the very same prompt and anchor completed right after a 243f hang.

Workaround in production

We generate at 107 frames per segment (4.5 s) and stitch with xfade. Works flawlessly for episodic production. Long takes are simply written as multiple short takes.

Suspected layer

MPSGraph fused SDPA kernel / AGX driver on large non-causal sequences (M4 Max, macOS 26.6). Corrupting variant already reported in #16 / #64 / #77; this adds a hang variant at even larger sequences, where the buffer never completes instead of returning garbage.

Happy to provide full logs, the exact command lines, or run additional probes on this machine.

Repro summary

# HANGS (denoise never progresses):
./h3 --ssd-streaming -d MODEL_DIR --first-frame anchor.png \
  -p "<any prompt>" --width 1248 --height 704 --frames 243 \
  --steps 4 --layers 50 --reuse 1 -o out.mp4

# COMPLETES (same everything, 107 frames):
./h3 --ssd-streaming -d MODEL_DIR --first-frame anchor.png \
  -p "<same prompt>" --width 1248 --height 704 --frames 107 \
  --steps 4 --layers 50 --reuse 1 -o out.mp4

ChunxueLi avatar Nov 11 '26 00:11 ChunxueLi

Follow-up data point: a "hung" 107f run that self-recovered after 24h+

One more observation that may help anyone else hitting this.

A 107-frame run using --motion-context (PR #76) appeared to hang exactly like the 243f runs above — stuck at denoise 3/4 with the thread sleeping in h3_dit_denoise_euler_preview → _MTLCommandBuffer waitUntilCompleted. I left it running overnight, assuming it was dead.

~24 hours later it completed on its own (FFmpeg 107/107, valid 4.46s output). The completion happened roughly 75 minutes after all other GPU work on the machine stopped — during the entire stall, a second h3 instance had been running a production batch concurrently on the same GPU.

Two practical takeaways:

  1. Concurrent h3 instances on M4 Max can stall each other's denoise for many hours. Anyone debugging these hangs should first verify exactly one instance is running, and observe for >12h before declaring a hang permanent.
  2. The 243f hang in my original report still reproduced on clean single-instance runs after a fresh reboot, so that case stands — but at least some "hangs" may be extreme stalls rather than true deadlocks.

ChunxueLi avatar Nov 11 '26 04:11 ChunxueLi

RESOLVED — the hang is a macOS GPU-driver regression, fixed in macOS 26.7.3

Closing the loop for anyone hitting this on Apple Silicon.

Timeline on one M4 Max 36GB machine (same hardware throughout):

Period macOS 243f behavior
late Oct – Nov 2 earlier 26.x completed normally (our v4 pipeline was built on 243f takes)
Nov 8 – Nov 12 26.6.2 hung 100% of the time: stuck at a denoise step in _MTLCommandBuffer waitUntilCompleted, 0% CPU, never recovers. ~10+ attempts, all workarounds failed (SDPA query chunking #77, command splitting, clean reboots, single instance). 107f never hung once in 20+ runs.
Nov 13 26.7.3 (25G342) same probe completes cleanly: 1248×704, 243 frames, 4 steps, --ssd-streaming, first-frame anchor, H3_SDPA_MAX_QUERY_ROWS=2048 → exit 0 in 3780s, valid 10.125s output

So the deadlock was introduced by a macOS 26.6.x GPU-stack change and fixed again in 26.7.3. Nothing to fix in h3.c — but one diagnostic note that cost us dearly: on a healthy M4 Max run, one 243f denoise step takes ~10–15 min (SSD streaming + 2× sequence length), and the progress bar shows 0% CPU the whole time because the thread sleeps in waitUntilCompleted. That is visually identical to the hang during the first minutes. Give any suspicious run up to ~30 minutes at one progress-bar cell before declaring a hang — and, as noted in our earlier comment, never run two h3 instances concurrently while debugging.

Thanks for the great project — closing out from our side.

ChunxueLi avatar Nov 13 '26 02:11 ChunxueLi