concurrency race conditions

    1

    Detect, reproduce, and fix race conditions using atomic operations, idempotency, and proper locking strategies.

    Free

    Security scanned

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+22 more

    concurrency race conditions

    Example session with this skill installed

    I have a Node.js function that checks a user's balance in Postgres and then creates an order. Sometimes users are able to double-spend and go into negative balance. How do I fix this?

    • Read your context and instructions
    • Compiled the concurrency race conditions

    I have analyzed the balance check and order creation flow. The issue is a classic

    Check-then-Act race condition. Because the check and the act are separate operations, two concurrent requests can both pass the balance check before either has performed the debit.

    Concurrency Analysis & Fix

    MetricDetail
    Race TypeCheck-then-act / Double-processing
    Current FaultApplication-level validation followed by INSERT
    Recommended FixAtomic DB Update with constraint or Pessimistic Locking
    Isolation NoteREAD COMMITTED (default) will not prevent this; use a single query.

    Atomic SQL Implementation

    Instead of fetching the balance to your Node.js backend, perform the check and debit in a single atomic statement.

    -- This ensures the balance never goes below zero and returns the row only if successful.
    UPDATE accounts 
    SET balance = balance - 50.00, 
        version = version + 1
    WHERE id = 'user_123' 
      AND balance >= 50.00;
    
    -- In your application code:
    -- If rows_affected == 0, throw "Insufficient funds or concurrent update".
    -- If rows_affected == 1, proceed to create the order record.
    

    Idempotency Guard

    To prevent a retry from creating a second order if the first one actually succeeded but timed out, use an idempotency key.

    INSERT INTO orders (id, user_id, amount, idempotency_key)
    VALUES ('order_abc', 'user_123', 50.00, 'request_uuid_from_client')
    ON CONFLICT (idempotency_key) DO NOTHING;
    

    Next steps

    • Replace the SELECT balance logic with the atomic UPDATE ... WHERE balance >= X statement.
    • Add a unique index to your idempotency_key column in the orders table.
    • Create a test script that fires 10 concurrent requests for the same user against a local Postgres instance to verify only one succeeds.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Concurrency bugs are the most expensive errors to fix because they rarely show up in development. They hide behind "works on my machine" and manifest as intermittent production outages, lost updates, or duplicate charges. This skill provides a systematic approach to identifying, reproducing, and fixing race conditions in distributed systems and multi-threaded applications.

    What it does

    • Classify race shapes by mapping symptoms to specific patterns like check-then-act, lost updates, or double-processing.
    • Select locking strategies between optimistic versioning, pessimistic SELECT FOR UPDATE locks, or atomic database operations.
    • Ensure idempotency by designing stable keys for at-least-once delivery systems and external side effects.
    • Enforce deadlock avoidance by establishing consistent lock acquisition orders across multiple resources.
    • Generate reproduction harnesses using real datastores and threading barriers to prove a fix works under load.

    How it works

    1. Identify the failure pattern by providing code snippets or logs describing the intermittent behavior.
    2. Review the datastore semantics to check if current transaction isolation levels actually protect against the observed race.
    3. Implement the atomic guard using the recommended mechanism for the specific contention level.
    4. Verify with a stress test that hits a live database instance rather than a mock.

    Frameworks & tools

    Works with any language or framework involving shared state or concurrent execution, including Node.js, Go, Python, Java, and SQL-compliant databases like PostgreSQL and MySQL.

    Why this beats prompting it yourself

    Generic AI prompts often suggest application-level checks that still contain races. This skill enforces database-level atomicity and insists on live-DB reproduction harnesses rather than unreliable mocks.

    Use cases

    • Preventing duplicate payments in a high-volume checkout flow.
    • Solving "flaky" integration tests that only fail in CI environments.
    • Fixing lost update bugs where concurrent editors clobber each other's changes.
    • Designing idempotent webhook consumers for external API integrations.

    Known limitations

    Does not support purely single-threaded code or environments without shared mutable state. Requires a live database for reproduction as lock semantics cannot be simulated in mocks.

    How to use

    Skills on Agensi run inside your AI agent. Copy the prompt, paste it into your agent, and it connects to Agensi and installs this skill for you. If a payment is needed, your agent shows it to you first.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill passes our trust checks below.

    Security scanned

    Passed our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Free forever

    No account required to browse

    Trust & safety

    Security scanned

    Verified clean today

    • Free to download with an account

    Listedtoday

    What's inside

    Frequently Asked Questions