Skip to content

Prevent duplicate completion of suspended sale (race condition) #4788

Open

Description

@jekkos

Problem

Sale::save_value() does not atomically verify the existing sale is still in SUSPENDED status before clearing its details and applying balance changes. Two concurrent unsuspend requests can both pass the status check, both clear the payment rows, and both apply the gift-card credit — resulting in a double credit.

This is a pre-existing race, now amplified by the return gift-card credit path introduced in #4781.

Suggested fix

Atomically transition the sale status before applying balance changes:

UPDATE sales SET sale_status = ? WHERE sale_id = ? AND sale_status = SUSPENDED

Check affectedRows(). If zero, the sale was already completed by a concurrent request — roll back and return an error.

Context

Raised by CodeRabbit review on #4781. Tracked separately to keep that PR scoped to the #4779 regression fix.

Activity

  1. objecttothis commented on Oct 29, 2026

    @objecttothis
    Member

    Claude Issue Checker Agent (cc @objecttothis)

    Status: Still present in master.

    I reviewed Sale::save_value() in app/Models/Sale.php (master, around lines 520–575). The update path for an existing sale is:

    if ($saleId != NEW_ENTRY) {
        $this->clear_suspended_sale_detail($saleId);
    }
    
    $builder = $this->db->table('sales');
    ...
    $builder->where('sale_id', $saleId);
    $builder->update($salesData);

    Neither clear_suspended_sale_detail($saleId) (which unconditionally deletes sales_payments, sales_items_taxes, sales_items, sales_taxes for the sale) nor the subsequent sales table update() filters on sale_status = SUSPENDED. There is no atomic UPDATE ... WHERE sale_id = ? AND sale_status = SUSPENDED with an affectedRows() check anywhere in this method, so two concurrent unsuspend requests can both pass an earlier status check, both clear the payment rows, and both apply balance changes (e.g. gift-card credit) — the race condition described in the issue is not guarded against.

    No related commits addressing this were found in the current git history. This looks like it is still open for a fix.

  2. added a commit that references this issue on Nov 18, 2026
    6a82ee3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions