Data inconsistency with concurrent order placement

Hi there,

I am new to CS-Cart and experimenting with it to integrate into one of our systems. I have setup a trial version with XAMPP by following this instructions. How To: Install CS-Cart on XAMPP — CS-Cart 4.18.x documentation

Everything works fine and I wanted to test an edge case, which is, how order process work in following scenario.

I came up with the following script which sends two concurrent requests to the cs-cart API to place 2 orders.

    ch := make(chan string)
    for i := 0; i < 2; i++ {
        go func(ch chan string) {
            req := getOrderRequest(...); // send a request to CS-Cart
            resp, err := client.Do(req)
            if err != nil {
                ch <- "error" +err.Error()
            }
            ch <- "Status:" +resp.Status
            defer resp.Body.Close()
        }(ch)
    }

Request payload contains following data,

    order := Order {
        UserID: userId,
        PaymentID: 12,
        ShippingID: 1,
        Products: map[string]Product {
            "1": {
                ProductId: 2,
                Amount: 3,
            },
        },
    }

As you can see I try to place two order for the same product. In each order requests I place amount as 3.

Then in the store I made the available stock of this product as 5.

Result: The final stock after two orders will be 2 and two orders will be placed with each order containing 3 items in it. ( If I cancel the two orders, stock will be re initialized to 2 + 3 + 3 = 8 ( not 5 ) )

Expected Result: One request should fail as there’s no enough stock as I enabled track inventory from settings

Is there something wrong Im doing here ?

cs-cart version: multivendor_v4.18.2.SP1
xampp version: v3.0.0

Thanks

You can try this add-on, which locks the order change status for orders containing the same products, preventing them from being successfully completed if there is insufficient stock.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.