﻿# Interrupted SimPy waits: reuse the pending request when the job still wants its place

Handle a SimPy interrupt during resource waiting by choosing between yielding the same pending request and abandoning it, without accidentally creating a second request.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/simulation-simpy-interrupted-wait-same-resource-request/
Language: en

## A notification need not abandon a queue position

A maintenance job waits for a simulated repair station and receives an interrupt asking it to update its status. The job still wants the station afterward. A generic retry handler creates a fresh resource request, leaving the earlier request unresolved. The model can now contain duplicate demand for the same job. Treat the interrupt as a decision point about the existing wait, not automatic permission to recreate the acquisition operation.

## Reuse the pending event when waiting continues

The [resource guide](https://simpy.readthedocs.io/en/stable/topical_guides/resources.html) explicitly offers continuing the wait by yielding the same request event again after catching an interrupt. This retains the request identity rather than introducing another acquisition. Preserve a reference to the pending request and decide whether the modeled notification changes the job’s interest. A status interrupt, a shutdown request and a real job cancellation need not share the same queue policy merely because they use the same interrupt mechanism.

## Abandoning a wait requires its own cleanup

If the job stops waiting, the pending event must be canceled. The [resource API](https://simpy.readthedocs.io/en/stable/api_reference/simpy.resources.html) describes cancellation and the context-manager behavior of requests. Scope matters: catching an interrupt inside an acquisition context can keep that context alive, while leaving it invokes cleanup. Design the scope around the modeled decision. Do not rely on the mere presence of a context manager to explain whether the job retained its wait or abandoned it.

## Distinguish pending requests from granted ownership

A pending request belongs to the queue; a granted request represents resource ownership. If interruption occurs around a grant, inspect the request’s actual state and the resource’s users before choosing cancellation or release. Canceling an unfulfilled wait and releasing an owned slot are different operations. Maintain a job-to-request ownership record so the model can show which acquisition was retained, which was abandoned and which already granted slot must eventually be released.

## Propose an identity-sensitive interruption fixture

A proposed fixture should hold a resource, enqueue a job and interrupt that waiting job with a notification that does not cancel it. Verify that the same request remains and that the job receives no duplicate grant. Repeat with a genuine abandonment and inspect that the queue no longer retains that request. No SimPy experiment was executed here. Acceptance should preserve identities and resource accounting rather than only compare the number of completed jobs.

## Keep queue semantics separate from a repair-time prediction

The [gRPC waiting guide](/journal/guide-grpc-wait-ready-bound/) concerns another pending-work boundary; [stack reproducibility](/journal/software-stack-reproducibility/) helps retain resource implementations. Here acceptance establishes what an interrupt does to a particular simulated wait. It does not validate repair durations, arrival rates or real maintenance capacity. Those inputs need separate evidence after a harmless notification no longer creates extra demand or changes the waiting population accidentally.
