diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index d6bdd023..1cad5a22 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -1378,6 +1378,7 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) { const RestoreResult rr = pc_->restore(r, whole_only); if (rr == RestoreResult::Declined) { s.restore_waited = true; + admit_next_step_ = true; RAD_DEBUG("admit %llu: the restore waits a step for %d deferred release(s)", (unsigned long long)r.id, n_deferred_); return RAD_E_FULL; @@ -2073,13 +2074,18 @@ const RadBatch* Scheduler::step() { (int64_t)plan_.e.size() + 1 < cfg_.max_seqs && free_cards_.size() > 1 ? share_tok : 0; admit_full_ = 0; + admit_next_step_ = false; const int as = admit_one(s, budget, areserve); if (as == kAdmitWait) { ++at; continue; } /* An overdue preempted prefill on an idle engine that still cannot be admitted will * not become admissible by waiting: nothing is running to finish and free the pool. - * Fail it (spec §17) and ask the next request, instead of parking the step loop. */ - if (as < 0 && running_.empty() && s.req && s.req->state == ReqState::Preempted && - preempt_wait_overdue(s)) { + * Fail it (spec §17) and ask the next request, instead of parking the step loop. Not + * when this step admitted someone ahead of it (the budget or a card went to them) or + * the refusal is a restore waiting a step for deferred releases: both clear by the + * next step, and failing them lost two of ten preempted requests under KV pressure + * once their prompts were published at the preemption (1. 11. 2026). */ + if (as < 0 && running_.empty() && plan_.e.empty() && !admit_next_step_ && s.req && + s.req->state == ReqState::Preempted && preempt_wait_overdue(s)) { RAD_WARN("request %llu failed: overdue preempted prefill could not be admitted " "with nothing running (%s)", (unsigned long long)s.req->id, rad_strerror(as)); diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index f2738c4a..a556a24b 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -525,6 +525,7 @@ private: int64_t span_min_ = 0; /* Set by admit_one when it held the admitted chunk back: what it would have taken. */ int64_t admit_full_ = 0; + bool admit_next_step_ = false; /* admit_one's refusal is a wait for the next step */ /* Whether this step's prefill may span (ChunkGeometry::span): set by step() for the step it * plans, true only for a prefill alone in its step, and read by plan_chunk. True outside a * step, so plan_chunk called directly answers for a span. */ diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index 24da048b..bed70235 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -2025,6 +2025,58 @@ TEST(overdue_preempted_prefills_keep_forcing_until_one_fits) { (void)b; (void)c; } +/* The overdue path fails a preempted prefill only when waiting cannot help. A restore that waits a + * step for a deferred release is a wait: failing it on an idle engine lost two of ten preempted + * requests under KV pressure once their prompts were published at the preemption (1. 11. 2026). */ +TEST(an_overdue_preempted_prefill_whose_restore_waits_a_step_is_not_failed) { + Fixture f; + f.build(/*blocks=*/26, /*states=*/64); /* 26 x 16 = 416 tokens */ + f.cfg.preempt_wait_ms = 0; + f.cfg.n_spec = 7; + CHECK_OK(f.start()); + + uint64_t a = f.submit(287, /*max_tokens=*/64, /*priority=*/5); + uint64_t b = f.submit(128, 64, /*priority=*/0); + /* B's growth meets A's blocks: B is preempted while A runs. */ + for (int i = 0; i < 8; ++i) { + REQUIRE(f.sch.step() != nullptr); + f.commit_plain(); + if (f.sch.find(b)->state == ReqState::Preempted) break; + } + REQUIRE_EQ((int)f.sch.find(b)->state, (int)ReqState::Preempted); + REQUIRE_EQ((int)f.sch.find(a)->state, (int)ReqState::Running); + + /* A is cancelled while a drafter pass names its blocks: they come back only at the next + * commit, and nothing runs. */ + Scheduler::BlockRow rows[4]; + REQUIRE_EQ(f.sch.block_begin(/*block=*/8, /*depth=*/7, /*window=*/0, /*mask=*/99, rows, 4), 1); + REQUIRE(f.sch.block_context(1) != nullptr); + CHECK_OK(f.sch.cancel(a)); + f.sch.block_end(); + REQUIRE(f.sch.has_deferred()); + REQUIRE_EQ(f.sch.metrics().running, 0); + + /* B's restore waits a step for that release; B must still be there after it. The cache + * gives back what it holds when asked. */ + f.pc.decline_whole = true; + f.pc.evict_frees = true; + f.pc.evict_grant = 1 << 20; + if (!f.sch.step() && f.sch.has_deferred()) f.sch.release_deferred(); + CHECK(f.sch.find(b)->state != ReqState::Failed); + /* As the engine does when a step plans nothing: it drains, then releases what was deferred. */ + bool ran = false; + for (int i = 0; i < 4 && !ran; ++i) { + if (!f.sch.step()) { + if (f.sch.has_deferred()) f.sch.release_deferred(); + continue; + } + ran = f.sch.find(b)->state == ReqState::Running; + f.commit_plain(); + } + CHECK(ran); + CHECK(f.sch.find(b)->state != ReqState::Failed); +} + TEST(a_mixed_step_has_the_right_boundaries_and_slot_mapping) { Fixture f; f.build();