feat(worker-rejoin-resync): reconcile unqueried job assignment (task 2.2)

Add record_job_reconcile_assign_fact(cluster_t*, job_t*, worker_t*,
xce_job_state_entry_t*) to job.c: eagerly sets job->job.state to match
the worker's reported outcome (mirroring dispatch_a_job_'s inline
mutation, minus allocation fields -- there are none to set, job->job.allocs
stays NULL per Requirement 4.8's documented scope). Calls the existing
persist_submit_job_dispatch_fact(..., alloc=NULL) directly, then leaves
the post-unlock persist_notify_job_updated_already_persisted() call to
the caller, same convention as task 2.1.

Fixes bug-1746 along the way: capnp_encode_joballoc() (persist.c, part
of bug-1678's original fix, predates this feature) unconditionally
dereferenced a NULL alloc -- never triggered before since
dispatch_a_job_ (the only prior caller) always passes non-NULL. Fixed
with a minimal guard producing a true zero-length buffer, matching
apply_committed_entry's alloc_len > 0 decode gate exactly. This
directly contradicted design.md's "tested, backward-compatible on both
seams" claim (true only for decode, never actually exercised for
encode) -- corrected in 4 places.

2 tests: applies-running-no-allocs-and-notifies, and a real restart-
replay round trip (write to real disk, fresh persist_recover(), confirm
jobid/host/state/NULL-allocs survive) -- proving this task's own
"durably survives a restart replay" completion criterion literally, not
just the in-memory mutation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y4M5MJM2zK6nU43da267Ku
This commit is contained in:
Rongsong Shen 2026-09-01 11:40:47 +00:00
parent b7ed6de2a1
commit ecfb4c942f
No known key found for this signature in database
GPG key ID: 5BCDAAE0123E9FE2
7 changed files with 550 additions and 3 deletions

View file

@ -20344,6 +20344,33 @@
],
"occurrences": 1,
"last_seen": "2026-09-01T00:20:00Z"
},
{
"id": "bug-1746",
"timestamp": "2026-09-01T00:00:00Z",
"error_message": "worker-rejoin-resync task 2.2 implementation: persist_submit_job_dispatch_fact(p, jobid, state, ts, alloc=NULL) crashes with a NULL-pointer SEGV instead of producing the \"no allocation\" WAL_JOB_DISPATCH payload design.md (Data Models / Out-of-Boundary section) and bug-1745's own design-review fix claimed was already \"a tested NULL-alloc path\" reusable verbatim. Confirmed via AddressSanitizer: SEGV on unknown address 0x0 in encode_JobAlloc (lib/controller.capnp.c:9805), called from capnp_encode_joballoc (controller/persist.c:235), called from persist_submit_job_dispatch_fact (controller/persist.c:7856), called from the new record_job_reconcile_assign_fact (controller/job.c) -- reproduced twice, deterministically, both via the full meson controller-test suite and via an isolated -t test_worker_resync run.",
"file": "src/controller/persist.c",
"root_cause": "persist_submit_job_dispatch_fact() (persist.c:7844) unconditionally calls capnp_encode_joballoc(alloc, ...) regardless of whether alloc is NULL. capnp_encode_joballoc() (persist.c:225) calls encode_JobAlloc(cs, &cja, (xce_job_alloc_t*)a) directly -- NOT the NULL-safe encode_JobAlloc_ptr() wrapper that already exists in the same generated file (lib/controller.capnp.c:9388, used correctly by every OTHER encode call site) and already has an explicit NULL guard (if s==NULL, sets a capn_null pointer instead of encoding). encode_JobAlloc() itself (lib/controller.capnp.c:9322, generated capnp binding) has no NULL guard at all: its first statement dereferences its xce_job_alloc_t* parameter unconditionally (reads s->jobid). design.md (worker-rejoin-resync) and bug-1745's own design-validation fix pass both asserted this call is safe -- quoting design.md: bug-1678 already gave WAL_JOB_DISPATCH a persist_submit_job_dispatch_fact() primitive with both apply seams decoding an optional trailing JobAlloc, a NULL alloc being the documented, tested backward-compatible case -- but that claim conflates two different things: the DECODE/apply seams (apply_committed_entry, apply_wal_entry) genuinely ARE NULL-safe and tested for a payload with no trailing alloc bytes (test_wal_job_dispatch_fact.c constructs such payloads by hand, e.g. test_wjd_apply_committed_entry_transitions_pend_to_run) -- but NO existing test, and no existing production call site (dispatch_a_job_ is the only one, job.c:1731, and it always passes a freshly-calloc'd non-NULL j->job.allocs), ever calls persist_submit_job_dispatch_fact() itself with alloc=NULL. The function's own doc comment (persist.h, near line 1447) actually states the opposite precondition: alloc must be non-NULL, the real call site (dispatch_a_job_) always has one by construction. The ENCODE side was never made NULL-safe when bug-1678 fixed the DECODE side.",
"fix": "FIXED (coordinator-authorized scope expansion of worker-rejoin-resync task 2.2 to include exactly this one function in persist.c, mirroring task 1.1's typemap.h/clusterconfig.h expansion precedent -- no schema or generated-code change). Added a NULL guard as the first statement of capnp_encode_joballoc() (persist.c:225): `if (a == NULL) { *out = NULL; *out_len = 0; return 0; }`. Produces a genuinely ZERO-LENGTH buffer for NULL input -- NOT a capnp-encoded null-pointer JobAlloc (what the existing encode_JobAlloc_ptr() wrapper's NULL branch would produce, which is non-zero bytes on the wire). This distinction is load-bearing: both apply seams (apply_committed_entry persist.c:4206-4227, apply_wal_entry persist.c:4759-4781) discriminate 'is there an allocation to decode' purely by `alloc_len > 0` on the trailing bytes after the {jobid,state[,start_time]} header -- a non-zero null-pointer-encoding byte count would have been misdecoded as a real (if empty/garbage) allocation to adopt, silently producing a non-NULL-but-garbage job.allocs on replay instead of the required NULL. Verified: (1) record_job_reconcile_assign_fact()'s 2 new tests (test_reconcile_assign_applies_running_no_allocs_and_notifies, test_reconcile_assign_survives_restart_replay -- the latter exercising a real disk WAL append + persist_recover() restart-replay round trip) now PASS cleanly, no SEGV. (2) record_job_reconcile_pending_fact()'s 3 existing task-2.1 tests still PASS unchanged. (3) dispatch_a_job_()'s existing non-NULL-alloc call site (job.c:1731, the only other caller) is completely unaffected -- its 5 dispatch-commit tests in test_dispatch_commit.c (test_dc_success_commits_alloc_state_and_resets_counter, test_dc_success_stamps_start_time_synchronously, test_dc_success_notifies_job_update_hook_exactly_once, test_dc_worker_not_found_leaves_job_pend_and_alloc_uncommitted, and critically test_dc_success_appends_wal_job_dispatch_durably -- a real disk WAL + persist_recover() round trip through the non-NULL-alloc path) all still PASS unchanged, confirming the new guard is a pure early-return for the NULL case with zero effect on the non-NULL path. (4) Full controller test suite (meson test -C src/xce.build 'controller test'): 6/6 OK, zero regressions anywhere. Also confirmed no secondary UBSan issue from the resulting `memcpy(dst, NULL, 0)` in persist_submit_job_dispatch_fact() when alloclen==0 (allocbuf stays NULL) -- this build runs under UBSAN_OPTIONS=halt_on_error=1 and the full suite passed clean, so glibc's memcpy tolerates a NULL/0 call here in practice.",
"tags": [
"worker-rejoin-resync",
"task-2.2",
"persist-c",
"wal-job-dispatch",
"bug-1678",
"bug-1745",
"null-pointer-deref",
"asan",
"record_job_reconcile_assign_fact",
"fixed"
],
"related_bugs": [
"bug-1678",
"bug-1745",
"bug-1631"
],
"occurrences": 2,
"last_seen": "2026-09-01T01:00:00Z"
}
]
}