﻿# SimPy preemption: subtract service received, not the entire time since arrival

Preserve remaining service explicitly after SimPy preemption and separate time spent using a resource from time spent waiting to reacquire it.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/simulation-simpy-preemption-remaining-service-not-wait-time/
Language: en

## An interrupted timeout is not a remaining-work model

A maintenance twin gives a job a required amount of service and lets urgent work preempt it. The handler waits for another resource grant and starts a full new service timeout. The model now restarts work even though the description promises resumable service. A different handler subtracts time since arrival and accidentally counts queueing as useful work. Define the physical service rule before encoding interruption: resume, restart and discard represent different systems.

## Keep the work balance in application state

The official [machine-shop example](https://simpy.readthedocs.io/en/stable/examples/machine_shop.html) explicitly maintains remaining duration when work is interrupted. The resource does not infer the application’s progress rule from a timeout. Retain required service, completed service and remaining service as distinct quantities. Update them only from productive intervals under the model’s assumptions. If an interruption destroys prior progress or incurs setup work, represent that separately rather than hiding it inside a generic retry delay.

## Start service accounting at acquisition

The [preemption API](https://simpy.readthedocs.io/en/stable/api_reference/simpy.resources.html) exposes usage_since as the simulation time when the preempted process started using the resource, along with the resource lost. That boundary differs from job arrival and request creation. For constant-rate resumable service, elapsed productive time is measured from actual acquisition until interruption. Reacquisition waiting belongs to response time but not completed service. With multiple resources, retain which resource was lost so an unrelated ownership interval cannot be subtracted from this job’s work.

## Reacquire before continuing the remaining service

After losing ownership, the job must follow the chosen reacquisition policy before service continues. Catching the interrupt does not mean the old slot is still held. A context-managed request can express the ownership scope, but the remaining-work state must survive outside the interrupted service segment. Do not count service while the job is queued, and do not allow an old timeout’s eventual timestamp to mark the whole job complete after the process has moved to another wait.

## Propose repeated-preemption conservation checks

A proposed fixture should preempt a known resumable job repeatedly, inserting explicit reacquisition waits. Verify that accumulated productive intervals equal its required service within the declared numeric tolerance and that completion cannot occur while ownership is absent. Inspect response time separately. Add a restart-policy case to prove the implementation can distinguish the intended models. No SimPy preemption experiment was executed here; this is a work-accounting check, not a measured maintenance-capacity estimate.

## Do not transfer a resumption rule between systems

The [vLLM preemption guide](/journal/vllm-kv-cache-preemption/) describes another continuation policy; [stack reproducibility](/journal/software-stack-reproducibility/) supports preserving the simulation implementation. Here acceptance establishes the declared service balance under interruptions. It does not show that a real repair, manufacturing stage or accelerator workload is physically resumable without loss. That assumption needs domain evidence, and changing it should remain visible as a different model rather than a harmless implementation optimization.
