Task 0522: SLC invoice cleanup blocked by Full Inventory locks

Investigated October 26, 2026. All times below are America/Denver. Production queries were read-only; application code and production records were not changed. Status counts were checked around 9:19 AM.

Finding

Full Inventory locking caused the Sunday afternoon INVP backlog. An invoice was created successfully, but its separate inventory/order completion process hit a five-second lock timeout while inserting a Full Inventory operation event. The foreign-key check needed a FOR KEY SHARE lock on the parent full_inventories record. Count submission/capture code takes FOR UPDATE on that same parent, which conflicts with the event insert.

The evidence supports this failure mechanism with high confidence. The historical identity of each blocking count transaction was not captured, so attributing a particular timeout to a particular counter or count submission is not possible from the retained evidence.

This was not confined to Stephen Foote. The Sunday affected orders were created/invoiced by Mary Khoun and Savanna Droge. A salesperson displayed on an order does not identify the user who actually invoiced it.

Scope and current state

Within the reported SLC range, 48 orders entered INVP between the first observed failure and the end of Sunday sales:

The six specifically named orders in the email need distinction: five were stuck and repaired; SLC6830550 is a separate ADD-order issue. It was created by Stephen Foote at 3:04 PM Sunday and has no recorded order-status transition after creation and no invoice found. The audit does not establish that it was sent to picking.

LAS6471884 is also separate: the audit shows ADD → PICK at 6:07 PM Sunday, with no INVP transition. Not every order in the reporter's broad range was affected.

There were 57 INVP orders across nine locations at the initial wider snapshot; 40 belong to this Sunday SLC incident. The other 17 predate it and have not been assigned this cause. No other location had a current INVP order created on Sunday in the checked dataset.

Timeline and direct evidence

SLC Full Inventory session 4 opened Saturday October 24 at 8:47 AM and remained open during this investigation. It had not entered apply. Ordinary sales successfully completed while the inventory was open, including SLC6830544 at 2:37:45 PM Sunday. An open inventory is therefore not an intended blanket sales freeze.

First affected order: SLC6830545, order ID 2156768, invoice ID 6110629 / invoice -4499159.

Event Sunday time
Invoice request started 2:38:55.491 PM
Order changed INVO → INVP 2:38:56.769 PM
HTTP request completed 200 OK 2:38:57.024 PM
New cleanup database connection authorized 2:38:57.038 PM
That connection failed on the Full Inventory event insert 2:39:02.335 PM

The database connection's endpoint and backend PID match between its authorization and failure. Sanitized PostgreSQL evidence:

2026-10-25 14:39:02.335 [backend 49076]
ERROR: canceling statement due to lock timeout
CONTEXT: SQL statement "SELECT 1 FROM ONLY public.full_inventories ... FOR KEY SHARE OF x"
STATEMENT: INSERT INTO full_inventory_operation_events (...) VALUES (...) RETURNING id, created_at

The invoice HTTP request ID was 01d5559d-c5e3-480c-8305-9f41a0a3c59c.

Additional repaired-order matches:

Order INVP transition Full Inventory event-insert timeout
SLC6830577 4:03:21.172 PM 4:03:26.610 PM
SLC6830580 4:05:15.793 PM 4:05:21.233 PM
SLC6830582 4:15:02.552 PM 4:15:08.086 PM
SLC6830584 4:50:54.248 PM 4:50:59.809 PM
SLC6830600 5:01:03.655 PM 5:01:09.116 PM
SLC6830606 5:09:04.156 PM 5:09:09.596 PM

Across 2–6 PM, PostgreSQL logged 51 purchasing-database lock-timeout failures on this event insert. These are event-insert failures, not 51 distinct orders. Application logs also show the identical failure in stock moves, including a 2:52:35 PM stack through StockMove.generate_stock_move → InventoryItemDetail.record_full_inventory_operation → FullInventories::OperationRecorder.record_locked!. This is direct evidence that the locking problem extends beyond invoice cleanup.

Rails/Loki alone did not expose the cleanup exception. The invoice request returned 200 and the cleanup ran separately; the decisive historical evidence came from PostgreSQL's error log. Absence of an HTTP 500 does not show completion.

Code mechanism

  1. app/controllers/orders_controller.rb:5 schedules clean_up_order after invoicing; :718 forks it with Spawnling. There is no durable retry job at this call site.
  2. app/models/order.rb:928 marks the order INVP after invoice creation. clean_up_order starts at :979, sells inventory, updates costs, changes invoice PROC to SHIP/TRAN, and removes/completes the order in transactions.
  3. app/models/inventory_item_detail.rb:51 wraps QOH changes in the Full Inventory boundary and inserts an operation event at :71. This additional insert happens when an active inventory window exists.
  4. app/services/full_inventories/operation_recorder.rb:57 inserts the event. Its foreign key requires a parent FOR KEY SHARE lock.
  5. app/services/full_inventories/count_submission.rb:19 takes a parent FOR UPDATE lock for a count submission; count_boundary.rb:14 also takes that lock before stock acquisition. Those locks conflict with event foreign-key checks and can create parent/stock lock-order cycles with ordinary inventory operations.
  6. warehouse_operation_boundary.rb:161 sets SET LOCAL lock_timeout = '5s'. This applies to later statements in the transaction, including the foreign-key check; it is not limited to advisory-lock acquisition.
  7. The failed completion transaction rolls back, leaving the already-created invoice in PROC and order in INVP. The forked cleanup call has no automatic retry at this seam; staff must use the existing completion retry workflow.

The retained errors prove parent-row lock contention, not a PostgreSQL deadlock verdict. A specific lock-order cycle remains a code-level risk to reproduce, not an assertion that PostgreSQL logged a deadlock for these orders.

Proposed fix and verification before implementation

Treat this as a product locking bug with inventory/financial regression risk, plus separately authorized recovery of the remaining orders. Do not close it as manual cleanup only.

  1. Reproduce a count holding the inventory parent while a real inventory sale/cleanup attempts to record its event, using two database connections in Docker tests. Establish failure before changing code. Include same-stock and different-stock cases.
  2. Review the parent-lock protocol in count submission/capture and related count writers. Use the least restrictive lock mode that still serializes count/lifecycle edits; evaluate FOR NO KEY UPDATE so ordinary event foreign-key checks can proceed. Verify warehouse/stock/parent acquisition order. Do not simply increase the timeout or bypass operation recording.
  3. Verify counts remain atomic/idempotent and ordinary sales, returns and stock moves preserve their QOH changes and matching operation events exactly once. Cover cancel/apply/end-window races before accepting a weaker lock.
  4. Verify the actual invoice completion path reaches SHIP/TRAN and completes the order during concurrent counting, without duplicate invoices, inventory relief, or financial postings. Examine failure visibility/retry at the existing forked cleanup seam; any move to a durable job needs separate idempotency review.
  5. After the fix is approved and verified, recover the remaining orders through the existing completion workflow. Do not re-invoice or directly rewrite status flags. Recovery was not executed during this investigation.

No Rails tests were run: this was a historical, read-only investigation and no implementation was made. The retained production exception, status audit, successful controls, and 46 timing matches establish the incident; a controlled concurrent regression test is the first build step.

Remaining affected Sunday SLC orders

As of the final read-only check, all 40 below had an INVP order and PROC invoice:

6830545 6830547 6830548 6830549 6830551 6830552 6830553 6830554
6830555 6830556 6830557 6830558 6830559 6830560 6830561 6830562
6830563 6830570 6830578 6830579 6830581 6830583 6830585 6830587
6830588 6830589 6830590 6830591 6830593 6830594 6830595 6830596
6830597 6830598 6830599 6830601 6830602 6830603 6830604 6830605

Evidence sources and limits