[GH-ISSUE #615] ds4: Pool can leave a batch to nobody - start() can miss a worker going to sleep (store -> load without a fence), and a worker that starts late takes the first batch as already seen; a pool nobody help()s then hangs #258

Open
opened 2026-10-29 21:23:24 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @SeriousRising on GitHub (Oct 28, 2026).
Original GitHub issue: https://github.com/mw00/project-maya/issues/615

Symptom

Found by reading, then reproduced by a new test (below); we have not seen it in the engine. A CPU-lane worker can sleep through a batch in two ways. On one pool the batch still finishes, one thread short, because the caller's help() and the other workers drain it. With two pools (NH = 2, a 2-socket host) run_nodes helps only pools[0] and then waits for pools[1]: if every worker of pool 1 misses the batch, nobody runs it and the engine waits for ever.

Cause

src/ds4/ds4_pool.hpp (v1.1.35):

  1. start() can miss a worker going to sleep. :67 gen_.store(g, std::memory_order_release); then :68 if (sleeping_.load(std::memory_order_acquire) > 0) { ... cv_.notify_all(); }. The worker, :105-106: sleeping_.fetch_add(1); then cv_.wait(lk, [&] { return gen_.load() != seen || quit_.load(); });.
    :67 -> :68 is a store followed by a load of another location. Release / acquire do not order that pair, and x86 lets the load complete while the store is still in the store buffer (the store-buffering / Dekker shape). So the starter can read sleeping_ == 0 and skip the notify while the worker, which has just done its fetch_add, reads the old gen_ in the wait predicate and sleeps.
  2. A worker that starts late takes the first batch as already seen. loop() reads its seen when its thread first runs (:95 uint64_t seen = gen_.load();). If the constructor returns and the caller start()s before that thread has run, the worker takes the new generation as seen and sleeps through that batch. We did not trace whether the engine starts a second NUMA pool right after building it.

src/ds4/ds4_model.cu:8567-8587 (run_nodes): for X: pools[X]->start(...), then pools[0]->help() only, then for X: pools[X]->wait(). So pool 1's batch is run by pool 1's workers alone.

Proposed fix

  • A seq_cst fence between the starter's store and load. The worker side is already seq_cst (fetch_add and the predicate's load() use the default order). With a seq_cst fence F between them, C++20 [atomics.order]/4 rules the bad outcome out: the starter reads 0 => F precedes the worker's RMW; the worker reads the old gen_ => its load precedes F; the RMW precedes the load; contradiction. On x86 this is one mfence per start() (two per CPU-lane call).
  • loop() starts from seen = 0, the value of gen_ at construction, before any start() can run.

A PR with both (and a test) follows.

Reproduction

New test in tests/ds4_pool_test.cpp (built by hand as its header says): pool_start_wakes_sleeper runs 200000 one-job batches on a one-worker pool with spin_us = 0, start() + wait() and no help(), and fails (a watchdog: 5 s without progress) if one never runs. 10 runs each:

ds4_pool.hpp Xeon E5-2697 v4 VM (16 vCPU) Ryzen 7 9800X3D
v1.1.35 10 / 10 hang (at batch 0) 10 / 10 hang
fence only 9 / 10 hang, all at batch 0 (race 2) 0 / 30
seen = 0 only 10 / 10 hang, at batches 5 .. 10410 (race 1) -
both 0 / 10 0 / 30

Not platform-specific. The NH = 2 hang itself is from reading: we have no 2-socket box. #56 reports decode varying between restarts on a dual-socket PC; we have no evidence it is this.

Not in the fix, seen while reading: glmfast::Workers (src/core/glm_fast_state.hpp, the design this pool copies) has the same shape: gen_.store(release) then sleeping_.load(acquire) at :164-165, and seen = gen_.load() when its thread starts at :202. Not tested.

Originally created by @SeriousRising on GitHub (Oct 28, 2026). Original GitHub issue: https://github.com/mw00/project-maya/issues/615 ### Symptom Found by reading, then reproduced by a new test (below); we have not seen it in the engine. A CPU-lane worker can sleep through a batch in two ways. On one pool the batch still finishes, one thread short, because the caller's `help()` and the other workers drain it. With two pools (NH = 2, a 2-socket host) `run_nodes` helps only `pools[0]` and then waits for `pools[1]`: if every worker of pool 1 misses the batch, nobody runs it and the engine waits for ever. ### Cause `src/ds4/ds4_pool.hpp` (v1.1.35): 1. **`start()` can miss a worker going to sleep.** `:67` `gen_.store(g, std::memory_order_release);` then `:68` `if (sleeping_.load(std::memory_order_acquire) > 0) { ... cv_.notify_all(); }`. The worker, `:105-106`: `sleeping_.fetch_add(1);` then `cv_.wait(lk, [&] { return gen_.load() != seen || quit_.load(); });`. `:67` -> `:68` is a store followed by a load of another location. Release / acquire do not order that pair, and x86 lets the load complete while the store is still in the store buffer (the store-buffering / Dekker shape). So the starter can read `sleeping_ == 0` and skip the notify while the worker, which has just done its `fetch_add`, reads the old `gen_` in the wait predicate and sleeps. 2. **A worker that starts late takes the first batch as already seen.** `loop()` reads its `seen` when its thread first runs (`:95` `uint64_t seen = gen_.load();`). If the constructor returns and the caller `start()`s before that thread has run, the worker takes the new generation as seen and sleeps through that batch. We did not trace whether the engine starts a second NUMA pool right after building it. `src/ds4/ds4_model.cu:8567-8587` (`run_nodes`): `for X: pools[X]->start(...)`, then `pools[0]->help()` only, then `for X: pools[X]->wait()`. So pool 1's batch is run by pool 1's workers alone. ### Proposed fix - A seq_cst fence between the starter's store and load. The worker side is already seq_cst (`fetch_add` and the predicate's `load()` use the default order). With a seq_cst fence F between them, C++20 [atomics.order]/4 rules the bad outcome out: the starter reads 0 => F precedes the worker's RMW; the worker reads the old `gen_` => its load precedes F; the RMW precedes the load; contradiction. On x86 this is one `mfence` per `start()` (two per CPU-lane call). - `loop()` starts from `seen = 0`, the value of `gen_` at construction, before any `start()` can run. A PR with both (and a test) follows. ### Reproduction New test in `tests/ds4_pool_test.cpp` (built by hand as its header says): `pool_start_wakes_sleeper` runs 200000 one-job batches on a one-worker pool with `spin_us = 0`, `start()` + `wait()` and no `help()`, and fails (a watchdog: 5 s without progress) if one never runs. 10 runs each: | `ds4_pool.hpp` | Xeon E5-2697 v4 VM (16 vCPU) | Ryzen 7 9800X3D | |---|---|---| | v1.1.35 | 10 / 10 hang (at batch 0) | 10 / 10 hang | | fence only | 9 / 10 hang, all at batch 0 (race 2) | 0 / 30 | | `seen = 0` only | 10 / 10 hang, at batches 5 .. 10410 (race 1) | - | | both | **0 / 10** | 0 / 30 | Not platform-specific. The NH = 2 hang itself is from reading: we have no 2-socket box. #56 reports decode varying between restarts on a dual-socket PC; we have no evidence it is this. Not in the fix, seen while reading: `glmfast::Workers` (`src/core/glm_fast_state.hpp`, the design this pool copies) has the same shape: `gen_.store(release)` then `sleeping_.load(acquire)` at `:164-165`, and `seen = gen_.load()` when its thread starts at `:202`. Not tested.
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#258
No description provided.