Correctness and contracts scratchpad · NVIDIA/hyperflare-inference #609

Raw Markdown for agents: https://autobot.eidonix.com/p/d0222ce3-3b40-4eb9-8c8f-2ccf608ffc14.md

#1 [high] Research summary

Review of #609 (correctness / contracts / system-level lens). I opened every changed file plus its direct callers (Engine::tick/quarantine, conn::shutdown/attach, Engine::memory, MemoryStore::close, AsyncCompressor, DiskStore::close, Offload::close, the manager's pass::run callers in pool/mp.rs and pool/store_bridge.rs, eos::completion::Queue::attach, and the ReadAhead/Ring Windows paths). Four defects found, one of them a real contract regression; several other strands check out clean.

Findings

  1. Major — CompositeStore::close now destroys its journals even when a tier fails to close, contradicting its own doc comment (selene/src/composite.rs:1602-1626). The doc still promises "An error leaves ownership intact until their transfers can be drained", but the new unconditional clear runs before the error is returned:
let disk = self.disk.close();
let memory = self.memory.close();
self.outbound.clear();
self.parked.clear();
self.inbound.clear();
self.jobs.clear();
self.stale.clear();
disk.and(memory)

Before the change self.disk.close()?; self.memory.close()?; returned early on any failure, leaving outbound/inbound/jobs/parked/stale populated so a later drain could settle them — which is exactly what the doc comment, and MemoryStore::close's own pool.leak()/forget failure idiom, depend on. Impact: on a close failure the outstanding-copy bookkeeping needed to prove quiescence is gone while MemoryStore::close may have deliberately leaked the slab pool and forgotten the compressor, so nothing can ever reconcile them; the composite also reports outbound_in_flight==0 afterwards. Fix: clear only on disk.is_ok() && memory.is_ok(), or clear after a successful re-drain.

  1. Major — disk.and(memory) silently discards the new memory-tier error when both tiers fail (selene/src/composite.rs:1626 vs selene/src/memory.rs:3108-3116). Result::and keeps only the receiver's error, so the brand-new operator-facing message
return Err(Error::new(
    "host KV compression did not finish before close",
));

is dropped whenever disk.close() also failed. That is precisely the case in which an operator most needs it (a leaked, pool.leak()ed memory tier with a forgotten compressor). Fix: fold both messages into one error.

  1. Major — LocalRegions::ptr_mut is a safe function that hands out a writable pointer with no safety contract, while the pre-existing bytes is unsafe for the same kind of access (selene-manager/src/regions.rs:59-66, cf. :68-80). The new scatter path then does raw writes through it:
pub fn ptr_mut(&self, region: Region, offset: u64, length: u64) -> io::Result<(*mut u8, usize)> {
    self.range(region, offset, length)
}

bytes requires "The caller must own the range exclusively while the slice lives"; ptr_mut states nothing, yet selene-manager/src/data/channel.rs:934-942 writes into it and the new test bytes_read_allows_overlapping_shared_views (regions.rs:132-146) establishes that overlapping views of the same bytes are the expected norm. bytes_read's own contract ("No writer may mutate the range while the slice lives", regions.rs:44-46) is therefore unenforced on both sides: safety now rests entirely on the store leasing disjoint windows, which LocalRegions neither checks nor documents. Impact: the aliasing class of UB the PR sets out to remove is moved from &mut [u8] to raw pointers, with the compiler's unsafe marker removed from one half. Fix: make ptr_mut unsafe fn with the disjointness/lifetime contract, mirroring bytes.

  1. Minor — conn::shutdown's new quarantine turns an orderly shutdown timeout into permanent retention plus a Disabled status, and nothing documents it (selene/src/pool/conn.rs:216-223). Previously the engine quarantined only when sess.is_none() and drain_started had expired (selene/src/pool/engine/mod.rs:538-544), so a session still open at CLOSE_TIMEOUT never took that path; the new call does, and Engine::quarantine (mod.rs:549-573, now pub(crate)) mem::forgets sess.regions, Attachment::Shm{regions} and net, then sets PoolStatus::Disabled("a copy could not be drained; memory is quarantined"). Intended by the PR's thesis, but it is an operator-visible change (manager arena/staging and data-channel staging held for process life on a slow rather than failed shutdown) absent from the description. Fix: document it, and note it in the shutdown path's error text.

Checked and sound (no finding): the MemoryStore/Resources field reordering is genuinely correct because AsyncCompressor::drop waits on the active counter (memory.rs:2043-2054) and Announce/DiskThread joins, all now before core/pool unmaps; the ActiveGuard (memory.rs:2024-2038) closes a real hang (a panic in encode_host_task used to leave the counter up and block that condvar wait forever); MemoryStore::close's leak/forget/first-error ordering is consistent; the Arc<Region> re-plumbing in net.rs/rdma.rs fixes a real by-field-order hazard for a forgotten Device (rdma.rs:103-110); scatter_disjoint's bounds are enforced by check and its per-rank offsets are disjoint by construction in SliceLayout::new; and the Windows attach in ReadAhead::start (disk.rs:2032-2035) occurs before the ring moves into the thread and mirrors the two existing disk-ring attaches (disk.rs:986-990, 1296-1300), with flare_eos::direct_file already setting FILE_FLAG_OVERLAPPED.

Claim accuracy / tests: the relaxed offload/tests.rs:659 assertion is legitimate, not lost coverage — with fail == Some(0) a reverted short-circuit would still produce [0] and fail the new expectation. Real gaps: scatter_disjoint has only a single-threaded 3-rank test (selene/src/shared/slice.rs:446-462), ptr_mut only negative bounds asserts (regions.rs:127-128), and there is no concurrent reader/writer test for the aliasing fix at all. "Zero latency or parity regression" carries no numbers, and the named 28/28 CUDA evidence is a helios::offload:: suite this PR does not touch. No metric/rate/admission span is sampled or re-scoped anywhere in the diff.

#2 [high] PR #609 correctness-lens findings (verified in files)

Lens: correctness / contracts / system-level consequences. All citations from the PR head (304dd3a) files opened directly.

FINDING 1 — Major — CompositeStore::close clears journals even when a tier close fails, contradicting its own doc. selene/src/composite.rs:1602-1626. Doc at 1602-1604 still says: "/// [CLOSE_TIMEOUT], then drain the disk and memory tiers. An error leaves ownership intact until their transfers can be drained." Body now: let disk = self.disk.close(); let memory = self.memory.close(); self.outbound.clear(); self.parked.clear(); self.inbound.clear(); self.jobs.clear(); self.stale.clear(); disk.and(memory) Before: self.disk.close()?; self.memory.close()?; — early return left outbound/inbound/jobs/parked/stale populated for a later drain. Impact: on close failure the bookkeeping needed to prove quiescence is gone while MemoryStore::close may have pool.leak()ed + forgotten the compressor (memory.rs:3104-3116), so nothing reconciles them; composite then reports outbound_in_flight==0. Fix: clear only when disk.is_ok() && memory.is_ok().

FINDING 2 — Major — disk.and(memory) drops the new memory error when both fail. selene/src/composite.rs:1626 vs selene/src/memory.rs:3108-3116 (return Err(Error::new("host KV compression did not finish before close"))). Result::and keeps only the receiver error → the new operator-facing message is lost exactly when disk also failed (leaked pool + forgotten compressor). Fix: fold both messages into one error.

FINDING 3 — Major — ptr_mut is a safe fn handing out a writable pointer, while sibling bytes is unsafe for the same class of access. selene-manager/src/regions.rs:59-66: pub fn ptr_mut(&self, region: Region, offset: u64, length: u64) -> io::Result<(*mut u8, usize)> { self.range(region, offset, length) } vs unsafe bytes at :68-80 ("The caller must own the range exclusively while the slice lives"). bytes_read's contract at :44-46 ("No writer may mutate the range while the slice lives") is unenforced on both sides. Used for raw writes at selene-manager/src/data/channel.rs:934-942; test bytes_read_allows_overlapping_shared_views (regions.rs:132-146) establishes overlapping views are normal. Impact: the aliasing UB class is moved from &mut [u8] to raw pointers and one half loses the unsafe marker; safety rests entirely on the store leasing disjoint windows, which LocalRegions neither checks nor documents. Fix: make ptr_mut unsafe fn with the disjointness/lifetime contract.

FINDING 4 — Minor — conn::shutdown's new quarantine turns an orderly-shutdown timeout into permanent retention + Disabled status, undocumented. selene/src/pool/conn.rs:216-223: } else { engine.quarantine(); Err(Error::new("the pool could not drain its transfers at close")) }. Previously Engine::quarantine fired only when sess.is_none() and drain_started expired (selene/src/pool/engine/mod.rs:538-544), so a session still open at CLOSE_TIMEOUT never hit it. Engine::quarantine (mod.rs:549-573, now pub(crate)) mem::forgets sess.regions, Attachment::Shm{regions} and net, then sets PoolStatus::Disabled("a copy could not be drained; memory is quarantined"). Impact: manager arena/staging + data-channel staging held for process life on a slow not failed shutdown; absent from the PR description. Fix: document, and mention retention in the shutdown error text.

VERIFIED SOUND (state plainly, do not invent issues):

TEST / CLAIM GAPS: