fix-inventory-sync-race-condition

inventory-servicefix/inventory-sync-race9d2e7ac126m+291-125

rg "inventory sync|warehouse event|sku" src/modules/inventory

The sync service reads current stock before processing all events, so a second event can replay against an outdated count.

src/modules/inventory/sync/inventory-sync.service.ts+86-31

src/modules/inventory/locks/sku-lock.ts+52-14

I added per-SKU locking and idempotency checks so duplicate events are skipped instead of replayed.

src/modules/inventory/repositories/stock.repository.ts+37-22

src/modules/inventory/events/warehouse-event.handler.ts+41-18

src/modules/inventory/sync/inventory-sync-race.spec.ts+102

The race spec dispatches two events for the same SKU and verifies that only one stock write wins the lock.

src/modules/inventory/locks/sku-lock.spec.ts+58

Lock coverage confirms that completed event ids are cached, while new event ids can still update stock normally.

pnpm build --filter inventory-service

The build failed on an API timeout while preparing the integration fixture. The code changes are present, but this run needs a retry.

I stopped after the timeout so the failure state stays explicit instead of hiding it behind another automatic retry.

Run failed