Skip to content

A ! call with no available parallelism holds the GPU indefinitely and crashes macOS (WindowServer watchdog) #942

Closed

Description

@mizchi

What you did

bend p1.bend -o p1_bend
./p1_bend --gpu 8GB

Two other Bend GPU binaries of the same shape were running concurrently on the same Mac.

What happened

The Mac crashed — WindowServer was killed by the WATCHDOG, taking the session with it. Bend itself printed nothing. It happened twice while running these benchmarks.

From the WindowServer stackshot that survived:

termination: WATCHDOG "monitoring timed out for service"
  WindowServer main thread is_alive_func returned unhealthy
  40 seconds since last successful checkin
captureTime 2026-09-21 15:10:58 / uptime 500s
Mac17,2 (M5, 10 core: 4P+6E, 32 GB) / macOS 26.6.2 (25G83)
thermalPressureLevel: Nominal

Threads that were stuck at capture time (elapsed = how long before the capture each last ran):

elapsed process where
164 s p1 (the attached file, --gpu 8GB) cube_run+692
85 s sequential BST inserts, lgk() = 0n cube_run+692
38.5 s kernel IOGPU Completion last time it ran
33 s sequential list drain cube_run+692
13 s WindowServer ws_main_thread com.apple.SkyLight.mtl_submit, TH_WAIT|TH_UNINT

Resolving cube_run+692 with nm and objdump gives the return address of [MTLCommandBuffer waitUntilCompleted] (gpu_pass is inlined into cube_run):

100001d9c: bl  _objc_msgSend$waitUntilCompleted
100001da0: ...                                  <- the frame on the stack

All three processes had 5 ms of user CPU, 35 MB RSS and three threads (no pool_work threads, so they were in GPU mode). Their main threads had last run 164 / 85 / 33 seconds earlier, so they were not spinning through passes — each was waiting on one single command buffer that never completed. GPU completions stopped for good at t≈461 s, and for the following 38.5 s WindowServer's compositing submit never returned, so the watchdog fired.

The CPU was not saturated: of kernel_task's 2941 seconds, 2880 were in idle threads. Thermal pressure was nominal.

What the three programs have in common is that all of them hand a strictly sequential computation to the GPU with !. The attached p1 walks 2^28 cells in one sequential BFS — its three arrays are affine, so no two pops can be in flight at once. Of the other two, one is an insert sequence that cannot be split (lgk() = 0n) and the other is a single-list drain.

The rest is inference. cube_run keeps issuing gpu_pass until root_done, but when the remaining work is one long sequential chain, a single lane walks that chain inside one dispatch, and that command buffer effectively never finishes. The GPU never comes back to any other submit — including WindowServer's compositing — and 40 seconds later the watchdog reboots the machine.

The comment at the top of p1 says "main still asks for the GPU with !, to show that the ask changes nothing". That ask, which should have been a no-op, takes the machine down.

A secondary finding: --gpu <size> is not bounded.

gpu_span() clamps to min(recommendedMaxWorkingSetSize, maxBufferLength, 2GB) (comp.ts:5071). But corpus_setup never consults gpu_span() once an explicit size is given:

u64 dflt = gpu ? gpu_span() : 1ull << 33;
u64 size = (gpu && bytes != 0 ? bytes : dflt) & ~16383ull;

The --gpu parser (comp.ts:6198) only checks n > 0. The whole span is then wrapped in newBufferWithBytesNoCopy … MTLResourceStorageModeShared (comp.ts:5078), so the driver wires the entire range. VM statistics at the crash (16 KB pages):

wire_count 990,741  -> 15.9 GB    free_count 111,765 -> 1.7 GB
pageins  1,030,668  -> 16 GB read back in the 500 s since boot

15.9 GB wired on a 32 GB machine. Taking a baseline of 3–4 GB, the ~12 GB difference matches 8 GB + 2 GB + 2 GB (one explicit span plus two default ones), but that is an inference, not a measurement.

For what it is worth, gates/perf.ts:233 passes MEMORY[bench] ?? "on" as $gm, and the largest entry in MEMORY is terrain: "1GB". No explicit span above the default clamp is exercised by the perf gate, so that path looks untested.

Suggestions:

  • Replace commit + waitUntilCompleted with addCompletedHandler plus a deadline, and fail the way device fault does when it is exceeded. On a Mac with a display attached, a Bend GPU run currently crashes the machine instead of failing.
  • Reject, or warn about, a ! with no available parallelism before it reaches the GPU seam.
  • Clamp an explicit span against recommendedMaxWorkingSetSize / maxBufferLength / physical RAM, or at least warn when it is exceeded.

No dump survived from the first crash, so I cannot confirm it had the same cause. Everything above is from the second one.

The file

p1.bend
import Base
# P1: one big maze. A single W x H grid (W = 2^wb, H = 2^hb, N = 2^db
# cells) searched breadth-first by the textbook array queue: a flat
# Array<U32> grid (1 open, 0 wall), a flat Array<U32> queue with head
# and tail, a flat Array<U32> distance map. Cell c is a wall when the
# xorshift hash of the seed and c, mod 100, is under 30; cell 0, the
# start, is always open. The search is one sequential walk -- the pop
# order is the queue order, and the three arrays are affine, so no
# two pops can be in flight at once. main still asks for the GPU with
# !, to show that the ask changes nothing.
#
# This is P2's program with the grain turned all the way up: p2.bend
# is the same defs with db() = 10n under a fork tree, and at the same
# grain the two print the same number (see probes/).
#
# KEEP CONSISTENT: db() = wb() + hb().

def db() -> Nat:
  28n

def wb() -> Nat:
  14n

def hb() -> Nat:
  14n

def w() -> U32:
  U32.shln(1, wb())

def wmask() -> U32:
  U32.sub(U32.shln(1, wb()), 1)

def hmask() -> U32:
  U32.sub(U32.shln(1, hb()), 1)

def nm1() -> U32:
  U32.sub(U32.shln(1, db()), 1)

def cells() -> Nat:
  Nat.pow(2n, db())

type St is Type:
  St{g: Array<U32>, q: Array<U32>, d: Array<U32>, +head: U32, +tail: U32}

def b2u(b: Bool) -> U32:
  match b:
    case False{}:
      0
    case True{}:
      1

# xorshift: x ^= x<<13; x ^= x>>17; x ^= x<<5
def prng(+x: U32) -> U32:
  +b = U32.xor(x, U32.shln(x, 13n))
  +d = U32.xor(b, U32.shrn(b, 17n))
  U32.xor(d, U32.shln(d, 5n))

# 1 when cell c of the maze seeded s is open: the start always, any
# other cell when its hash mod 100 is 30 or more (30% walls)
def open(s: U32, +c: U32) -> U32:
  h = prng(U32.xor(s, U32.mul(U32.inc(c), 340573321)))
  b2u(Bool.or(U32.is_zero(c), U32.is_ge(U32.mod(h, 100), 30)))

# fill the grid: one write per cell, ascending
def fill(+j: Nat, +s: U32, g: Array<U32>) -> Array<U32>:
  match j:
    case 0n:
      g
    case 1n+p:
      +c = U32.sub(nm1(), U32.from_nat(p))
      fill(p, s, g[c] <- open(s, c))

# the neighbour is open and unseen: stamp its distance, queue it at
# the tail
def look.push(+n: U32, v: U32, seen: Bool, st: St) -> St:
  match seen:
    case True{}:
      st
    case False{}:
      St{g, q, d, head, tail} = st
      St{g, q[tail] <- n, d[n] <- v, head, U32.inc(tail)}

def look.seen(+n: U32, v: U32, g: Array<U32>, q: Array<U32>, head: U32,
              tail: U32, r: Array<U32> & U32) -> St:
  (d, dn) = r
  look.push(n, v, U32.is_ne(dn, 4294967295), St{g, q, d, head, tail})

# the neighbour is in bounds: skip a wall, else read its distance
def look.wall(+n: U32, v: U32, wall: Bool, st: St) -> St:
  match wall:
    case True{}:
      st
    case False{}:
      St{g, q, d, head, tail} = st
      look.seen(n, v, g, q, head, tail, d[n])

def look.open(+n: U32, v: U32, q: Array<U32>, d: Array<U32>, head: U32,
              tail: U32, r: Array<U32> & U32) -> St:
  (g, o) = r
  look.wall(n, v, U32.is_zero(o), St{g, q, d, head, tail})

# one neighbour n at distance v: skip it out of bounds, a wall or
# seen; else stamp its distance and push it
def look(+n: U32, v: U32, inb: Bool, st: St) -> St:
  match inb:
    case False{}:
      st
    case True{}:
      St{g, q, d, head, tail} = st
      look.open(n, v, q, d, head, tail, g[n])

# the four neighbours of cell c at distance v: left, right, up, down
def look4(+c: U32, +v: U32, st: St) -> St:
  +x = U32.and(c, wmask())
  +y = U32.shrn(c, wb())
  s1 = look(U32.sub(c, 1), v, U32.is_gt(x, 0), st)
  s2 = look(U32.inc(c), v, U32.is_lt(x, wmask()), s1)
  s3 = look(U32.sub(c, w()), v, U32.is_gt(y, 0), s2)
  look(U32.add(c, w()), v, U32.is_lt(y, hmask()), s3)

# the pop's answer: the state, and whether the queue still holds a cell
def pop.done(st: St) -> St & Bool:
  St{g, q, d, head, tail} = st
  (St{g, q, d, head, tail}, U32.is_lt(head, tail))

def pop.dist(g: Array<U32>, q: Array<U32>, c: U32, head: U32, tail: U32,
             r: Array<U32> & U32) -> St & Bool:
  (d, dc) = r
  pop.done(look4(c, U32.inc(dc), St{g, q, d, head, tail}))

def pop.cell(g: Array<U32>, d: Array<U32>, head: U32, tail: U32,
             r: Array<U32> & U32) -> St & Bool:
  (q, +c) = r
  pop.dist(g, q, c, head, tail, d[c])

# one pop: take the head cell and its distance, look at its four
# neighbours, then report whether the queue still holds a cell
def pop(st: St) -> St & Bool:
  St{g, q, d, head, tail} = st
  pop.cell(g, d, U32.inc(head), tail, q[head])

# the search loop, fuel-bounded by the cell count: a cell is pushed
# once at most, so the queue drains inside the fuel
def bfs(f: Nat, r: St & Bool) -> St:
  match f:
    case 0n:
      (st, more) = r
      st
    case 1n+p:
      (st, more) = r
      match more:
        case False{}:
          st
        case True{}:
          bfs(p, pop(st))

# the checksum: every cell in turn, the current read riding in as the
# pair r (the last step issues one masked dead read); a reached cell
# mixes its distance position-weighted and counts one, and the count
# seals the mix
def fold(+j: Nat, r: Array<U32> & U32, acc: U32, cnt: U32) -> U32:
  match j:
    case 0n:
      U32.xor(acc, U32.mul(cnt, 2246822519))
    case 1n+p:
      (d, +v) = r
      +c = U32.sub(nm1(), U32.from_nat(p))
      +hit = b2u(U32.is_ne(v, 4294967295))
      k = U32.mul(hit, U32.mul(v, U32.inc(c)))
      m = U32.xor(U32.mul(acc, 2654435761), k)
      fold(p, d[U32.inc(c)], m, U32.add(cnt, hit))

def finish(st: St) -> U32:
  St{g, q, d, head, tail} = st
  fold(cells(), d[0], 0, 0)

# the whole search: fill the grid, seed the queue with cell 0 at
# distance 0, search, fold
def solve(+m: U32) -> U32:
  s = U32.mul(U32.inc(m), 2654435761)
  g = fill(cells(), s, Array.new(U32, db(), 0))
  q = Array.new(U32, db(), 0)
  d = Array.new(U32, db(), 4294967295)
  finish(bfs(cells(), (St{g, q, d[0] <- 0, 0, 1}, True{})))

# db=28 wb=14 hb=14 (16384 x 16384, 268435456 cells)
#|1635505109
def main() -> IO(Unit):
  IO.print(U32.show(solve!(0)))

bend --version

bend 2.0.23

uname -sm

Darwin arm64

clang --version (the first line)

Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions