Use this when results depend on timing between jobs.
Race Conditions
A race condition is a bug where the result depends on timing, such as two jobs allocating the same IP or editing the same file. Prevent it with locks, atomic operations and idempotent design.
1. Race conditions: concurrent tasks interfere (same file, same IP allocation, same config).
2. Use locks or atomic APIs.
3. Idempotent allocation (check-then-act is racy).
4. Test concurrently.
5. Serialise critical sections.
parallel tasks → shared resource → lock/atomic op
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Common mistakes Narrow the fault
- Read-then-write without locking.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Race conditions depend on timing between jobs.
- Typical case: two jobs allocate the same IP.
- Locks and atomic operations prevent them.
- Shared resources identified — risk
- Atomic operations / compare-and-swap — safety
- Tests with concurrency — detection
ReferenceWhy it matters1 section · depth on demand
What it is
A race condition happens when the result depends on the timing of parallel operations (two jobs allocating the same IP).
ReferenceImportant terms & boundaries2 sections · depth on demand
Example
Two parallel jobs read "next free IP = .25", both assign it → duplicate address. Fix: let IPAM allocate atomically (available-ips POST) or lock the prefix during allocation.
In depth
A race condition happens when the result depends on the timing of parallel operations. Two automation runs read the same state, both decide to act, and their changes collide — two scripts allocate the same IP address, two jobs edit the same file, two pipelines apply Terraform at once.
Typical races and fixes:
| Race | Fix |
|---|---|
| Two runs allocate the same resource | Atomic allocation in the source of truth ("next available" API) |
| Concurrent writes to state | Locking (Terraform state lock, NETCONF lock) |
| Read-modify-write on a file | File lock or a single writer |
| Parallel jobs on one target | Serialise per target (queue, mutex) |
exec 9>/var/lock/backup.lock
flock -n 9 || { echo "another run is active"; exit 1; }
Races are intermittent by nature, so they rarely appear in testing; design them out rather than hoping to catch them.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Parallelism & Concurrency > Race Conditions. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.python.org/3/library/concurrent.futures.html · confidence 94%