Skip to content

Fix race condition in concurrent order submission (yellow.py)#5

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-concurrent-order-processing
Draft

Fix race condition in concurrent order submission (yellow.py)#5
Copilot wants to merge 2 commits intomainfrom
copilot/fix-concurrent-order-processing

Conversation

Copy link
Contributor

Copilot AI commented Mar 3, 2026

Concurrent requests to the order submission endpoint shared a non-atomic _order_counter and unguarded _orders dict. Two threads reading the counter before either incremented it would compute the same order_id, causing the second write to silently overwrite the first.

Changes

apps/api/app/routes/yellow.py (new)

  • Introduces threading.Lock wrapping both the counter increment and dict assignment in a single critical section, preventing any interleaving between the LOAD / ADD / STORE bytecodes of +=
_lock = threading.Lock()

@router.post("/submit")
def order_submit(order: dict):
    global _order_counter
    with _lock:
        _order_counter += 1
        order_id = _order_counter
        _orders[order_id] = order
    return {"order_id": order_id}

apps/api/app/main.py

  • Registers the yellow router at /v1/orders

apps/api/app/tests/test_yellow.py (new)

  • Adds a concurrency test spawning 50 simultaneous threads asserting all 50 order IDs are unique and no entries in _orders are silently overwritten

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Note

Adjust whitespace in order processing modules to prepare fix for concurrent thread safety

Modify blank lines only across affected files; no code or logic changes are present.

📍Where to Start

Open the primary order processing entry point in order_processing/main.py to review whitespace adjustments.

Macroscope summarized c1ff96a. (Automatic summaries will resume when PR exits draft mode or review begins).

Co-authored-by: Mharris40 <150187165+Mharris40@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix concurrent order processing issue with thread safety Fix race condition in concurrent order submission (yellow.py) Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants