--- id: "bug-1837" status: "fixed" status_updated: "2026-09-24" status_note: "" component: "scheduler" severity: "unknown" timestamp: "2026-09-20T22:05:00Z" last_seen: "2026-09-21T05:00:00Z" occurrences: 1 files: ["src/scheduler/src/sim_loop.c"] tags: ["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"] related: ["bug-1759", "bug-1779", "bug-1836"] legacy: true gitea_issue: 67 gitea_synced_status: "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