﻿# SimPy AnyOf resource races: a satisfied condition does not select ownership for you

When a SimPy job requests alternative resources, account for every granted and pending request after AnyOf and clean up unused acquisitions explicitly.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/simulation-simpy-anyof-alternative-resource-ownership/
Language: en

## Alternative requests can create multiple obligations

A logistics twin sends a job’s acquisition requests to alternative loading stations and waits for AnyOf. The code picks the first reported station and forgets the remaining requests. An unused pending request may acquire a station later, or another request may already hold one. The simulated capacity leak can distort every subsequent queue. Model the choice and its cleanup as an explicit ownership decision, rather than assuming the condition event is an exclusive reservation operation.

## A condition result is not necessarily a single winner

The [condition API](https://simpy.readthedocs.io/en/stable/api_reference/simpy.events.html) defines AnyOf through satisfaction by at least one child. Its result contains events that occurred before the condition was processed. Consequently, the application must tolerate several reported acquisitions rather than assume a single winner object. Establish a deterministic selection rule that matches the modeled logistics policy. The selected station and the set of fulfilled child requests are separate facts that should both remain visible in the trace.

## Reconcile every unused request with its resource

The [resource API](https://simpy.readthedocs.io/en/stable/api_reference/simpy.resources.html) exposes pending queues and granted users. An unused pending request needs cancellation; an unused granted acquisition needs release. Inspect actual request and resource state rather than treating every nonselected child as unfulfilled. Maintain a ledger tying each request to its station, chosen role and cleanup result. This is especially important when acquisitions become available close together, where the condition’s observation and resource ownership are distinct stages.

## Do not confuse selection with service completion

After choosing a station, keep the selected ownership until the modeled service has finished or is deliberately abandoned. Cleaning up alternatives does not release the selected station early. Conversely, finishing service there does not prove the other acquisitions were cleaned up. Define these lifecycle boundaries separately. A context-manager design may help express release, but its nesting and exit points must reflect which acquisition is retained through service and which should be discarded immediately.

## Propose simultaneous-availability and delayed-grant cases

A proposed fixture should make several alternatives available before the selection is observed, then repeat with an unselected request remaining pending. Verify that only the selected acquisition survives into service and that later availability cannot grant a forgotten alternative. Inspect users and queues after the job exits. No SimPy resource-race experiment was executed here. Acceptance should establish capacity conservation for each station, not only successful completion of the chosen job.

## Bound the logistics interpretation

The [transactional recovery study](/journal/batchweave-transactional-batches-recovery-resource-boundaries/) covers a different ownership boundary; [stack reproducibility](/journal/software-stack-reproducibility/) supports retaining the event and resource implementations. Correct alternative acquisition establishes a coherent reservation policy. It does not prove optimal routing, fairness or physical station throughput. Evaluate those properties after speculative requests no longer reserve extra capacity invisibly and every selection has a corresponding acquisition and release history.
