myxce/.wolf/bugs/bug-1837.md
Rongsong Shen d26ff8870d
feat(buglog): one Markdown record per bug, checked against a record standard
The buglog was one 27K-line JSON file whose fields covered only symptom,
root cause and fix, as free text: 2% of entries named the code version a
bug was found on, 12% the build profile, 36% a way to reproduce, and 58% of
the file was OpenWolf hook auto-detections ("Missing guard clause",
"Function not marked async" on .md files).

- .claude/skills/buglog/bug-record.md: the record standard. Eight
  sections (Summary as the title, Description, Environment, Reproduction;
  Root cause, Fix, Verification, References once fixed), each with the
  content it must contain -- e.g. Environment needs the OS, the libc the
  failing binary loads (from its ELF interpreter: glibc 2.42 in the nix
  shell vs 2.40 on the host), toolchain, build profile and commit.
- .wolf/bugs/<id>.md: 500 records migrated from buglog.json; every field
  verified to round-trip. They carry legacy: true and an explicit "not
  recorded" where the old log had no such field; component derived from
  file paths, severity from keywords (else unknown), fix_commits from
  hashes in status_note/fix that git resolves (91 of 463 fixed).
- .wolf/buglog-archive.json: the 710 hook auto-detections, verbatim.
- .wolf/buglog.json is now an index generated from the records, keeping
  the fields OpenWolf's hooks and `openwolf bug search` read.
- scripts/buglog.py rewritten: new (template + Environment collected from
  the machine and --binary), check, status (refuses a status the record
  doesn't support; fixed needs Root cause, Fix, Verification and
  --commit hashes git resolves), note, recur, set, show, list, triage,
  env, index. 29 unittest cases in scripts/tests/test_buglog.py.
- scripts/git-hooks/pre-commit runs `check --staged`: incomplete staged
  records, a legacy record hand-changed to fixed without the fix-time
  sections, a new record claiming legacy: true, or a stale index refuse
  the commit.
- scripts/gitea-sync.py reads the records: issue body = the record's
  sections plus linked fix commits (origin remote, GITEA_COMMIT_REPO to
  override), and a body hash so an issue's text is refreshed when the
  record changes.
- bug-1866 rewritten to the full standard as the worked example.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 03:40:11 +00:00

7.8 KiB

id status status_updated status_note component severity timestamp last_seen occurrences files tags related legacy gitea_issue gitea_synced_status
bug-1837 fixed 2026-09-24 scheduler unknown 2026-09-20T22:05:00Z 2026-09-21T05:00:00Z 1
src/scheduler/src/sim_loop.c
scheduler
sim
sim_reconcile_unaccounted_jobs
picker-plugin
threading-contract
jbuf
xce-audit-invariants
related-to-bug-1759
related-to-bug-1779
tdd
allocator-thread-relay
user-directed
bug-1759
bug-1779
bug-1836
true 67 fixed

bug-1837: Found during /xce-audit-invariants scheduler (dimension 3, jbuf capacity-1 rendezvous audit), not…

Description

Found during /xce-audit-invariants scheduler (dimension 3, jbuf capacity-1 rendezvous audit), not user-reported and not a live symptom: sim_reconcile_unaccounted_jobs() (sim_loop.c) calls picker->picker_iface->on_dispatch(picker, job->jobid) directly from the sim driver thread (sched_sim_driver_run()'s own thread, between xce_core_begin_session() and xce_core_signal_go()), but plugin.h documents on_dispatch()/on_pend() as 'Post-allocation hooks (MUST) -- allocator thread, after each allocation attempt' -- a MUST-level threading contract this call site violates by construction (driver-thread code calling an allocator-thread-only hook).

Environment

Not recorded (migrated from buglog.json).

Reproduction

Not recorded (migrated from buglog.json).

Root cause

bug-1759/bug-1779's own fix added this on_dispatch() call to sim_reconcile_unaccounted_jobs() specifically to prevent sim_picker_thread_fn() from re-offering (and re-PENDing) an already-reconciled job on a later round -- a real, necessary fix for that narrower symptom (a duplicate PEND report) -- but the fix's own header comment justifies it only in those terms ('the picker never re-decides an already-resolved job'), never addressing thread-safety of calling an allocator-thread-only hook from the driver thread instead. It currently causes no observable corruption only because the sole real (non-no-op) picker plugin, builtin_fcfs.c, happens to guard its entire candidate queue with an internal lock (priv->lock, taken in every hook including end_session()) -- an implementation detail plugin.h's own documented contract does not require picker plugins to provide ("the picker thread holds no lock the allocator thread needs"). A future or alternate picker plugin implemented per that documented (lock-free) contract would race sim_reconcile_unaccounted_jobs()'s driver-thread on_dispatch() call against the picker thread's own end_session()/next_job() tail-end activity from the prior round, unsynchronized.

Fix

FIXED (2026-09-21, TDD). Chose the fuller of the two directions the audit finding itself proposed: moved the on_dispatch() call onto the real allocator thread instead of documenting an exception to plugin.h's contract. Since the scheduler-sim-live-allocation spec, sim mode's sched_sim_driver_run() (sim_loop.c) spawns the SAME unmodified sched_job_picker()/sched_allocator() thread pair (sched_election.c) normal mode uses, so a real allocator thread now genuinely exists to route through in production. Mechanism: two new dedicated cbufs on xce_sched_core_t (core.h) -- reconcile_req (jobid) and reconcile_ack (rc) -- deliberately SEPARATE from jbuf, not a widened jbuf item type, since jbuf's item size/capacity are a pinned determinism invariant (test_core_jbuf_capacity_pin.c, Req 5 AC 7) that a byte-for-byte source-text-scanning regression test forbids changing. A new reserved sentinel, XCE_SCHED_RECONCILE_WAKE (== UINT64_MAX, core.h; same reserved-value idiom this codebase already uses for SCHED_SIM_START_TS_LATEST, sim.h, and jbuf's own pre-existing 0 end-of-session marker), is what the driver pushes onto jbuf itself to wake the allocator thread out of its cbuf_get(&core->jbuf) block -- never a real jobid, so it can never be mistaken for one by sched_get_job(). sim_reconcile_unaccounted_jobs()'s on_dispatch call site (sim_loop.c) now goes through a new sim_reconcile_invoke_on_dispatch() helper: pushes jobid onto reconcile_req, THEN pushes XCE_SCHED_RECONCILE_WAKE onto jbuf (ordering matters -- cbuf_put()'s own mutex gives happens-before, so reconcile_req is guaranteed populated by the time the allocator's cbuf_get(&core->jbuf) returns the sentinel), then blocks on reconcile_ack for the result -- one request in flight at a time, matching jbuf's own capacity-1 rendezvous discipline without touching jbuf itself. sched_election.c's sched_allocator() gained a matching branch: on XCE_SCHED_RECONCILE_WAKE, drain reconcile_req, call on_dispatch() (no plugin_job_election()/sched_make_allocation()/decision -- reconciliation already did its own accounting/charging on the driver thread), ack via reconcile_ack, continue -- a true no-op for normal mode, which never populates reconcile_req. A new xce_sched_sim_driver_t.allocator_thread_live bool (sim.h) tells sim_reconcile_invoke_on_dispatch() whether to route through the relay at all: sched_sim_driver_run() sets it true immediately after successfully spawning the allocator thread (same thread, program order -- no atomics needed, no startup race, since both the write and every later read happen on the driver thread only); it stays false for a bare xce_sched_sim_driver_t that never spawned any thread pair at all (test_sim_reconcile_unaccounted.c's own documented Testing Strategy: ~23 existing tests call sim_reconcile_unaccounted_jobs() directly, standalone, with no picker/allocator threads running), in which case a direct on_dispatch() call is safe by construction -- there is provably only one thread touching picker state, so plugin.h's allocator-thread-only contract has nothing to violate. This let all ~23 pre-existing direct-call tests in that file pass unchanged. Blast radius (grepped the full src+testsuite tree for core->jbuf and every hand-rolled white-box sched_allocator()/sched_job_picker() stub before assuming any of this was safe, per this project's own established discipline): 4 other test files drive sim_reconcile_unaccounted_jobs()'s success path through the REAL sched_sim_driver_run() round loop with their own local sched_allocator() stub (a simplified PEND-only stand-in, never the real sched_election.c symbol) -- test_sim_verify.c, test_sim_round_boundary_reconciliation.c, test_sim_contention_parity.c, test_sim_e2e_original_motivation.c. Confirmed empirically, not just by static grep: a first full-suite build+test run with the fix applied but before patching these four hung all four at meson's 30s per-test timeout (SIGTERM) -- exactly the deadlock a stub that never answers reconcile_ack would cause. Added the identical relay branch to each of their local sched_allocator() stubs; re-ran the full suite and all four passed. TDD: new test_reconcile_invokes_on_dispatch_on_allocator_thread_not_caller (test_sim_reconcile_unaccounted.c) uses a minimal test-owned picker plugin (not builtin_fcfs, whose own internal locking was exactly what made the pre-fix violation dormant/unobservable) whose on_dispatch() records pthread_self(), plus a one-shot relay_one_reconcile_request() thread standing in for sched_allocator(); asserts the recorded tid equals the spawned relay thread's tid and NOT the calling (test) thread's own tid. RED confirmed by temporarily forcing sim_reconcile_invoke_on_dispatch() back to an unconditional direct call and rebuilding: exactly this new test hung (the relay thread's pthread_join() never returned, since nothing was ever pushed onto jbuf/reconcile_req) and was killed at meson's 30s timeout, all 84 other tests still passed. GREEN confirmed after restoring the real fix: full scheduler suite 85/85 Ok, zero regressions, matching the exact pre-fix baseline count.

Verification

Not recorded (migrated from buglog.json).

References

  • Related: bug-1759, bug-1779, bug-1836
  • Gitea: #67