fix double-free kv cache for requests that have already finished and been freed during preemption (#18694)

This commit is contained in:
JD
2026-02-13 13:17:44 -08:00
committed by GitHub
parent 008ea46af1
commit 191d354f53
2 changed files with 10 additions and 1 deletions
@@ -834,8 +834,16 @@ class PrefillAdder:
# Iterate running requests to find preemptible requests # Iterate running requests to find preemptible requests
priority_sign = 1 if server_args.schedule_low_priority_values_first else -1 priority_sign = 1 if server_args.schedule_low_priority_values_first else -1
# NOTE: A request finishes in two phases:
# 1) check_finished + release_kv_cache (in process_batch_result)
# 2) filter out of batch (in get_next_batch_to_run / update_running_batch)
# Preemption runs between these two phases (inside get_new_batch_prefill),
# so running_batch may still contain requests whose KV cache is already freed.
# We must skip them here to avoid a double-free on release_req.
valid_running_reqs = ( valid_running_reqs = (
r for r in self.running_batch.reqs if r not in self.preempt_list r
for r in self.running_batch.reqs
if r not in self.preempt_list and not r.finished()
) )
sorted_valid_running_reqs = sorted( sorted_valid_running_reqs = sorted(
@@ -76,6 +76,7 @@ class TestPrefillAdder(CustomTestCase):
req.output_ids = [0] * output_len req.output_ids = [0] * output_len
req.sampling_params = SimpleNamespace(max_new_tokens=max_new_tokens) req.sampling_params = SimpleNamespace(max_new_tokens=max_new_tokens)
req.time_stats = SimpleNamespace(wait_queue_entry_time=wait_time) req.time_stats = SimpleNamespace(wait_queue_entry_time=wait_time)
req.finished.return_value = False
return req return req
def create_adder(self, running_batch): def create_adder(self, running_batch):