Skip to content

Commit e575c56

Browse files
committed
Fix: upsert fiscal counter race condition with F, db row locking
1 parent a823526 commit e575c56

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

fiscguy/zimra_receipt_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import qrcode
1414
from django.core.files.base import ContentFile
1515
from django.db import DatabaseError
16+
from django.db.models import F
1617
from loguru import logger
1718

1819
from fiscguy.exceptions import ReceiptSubmissionError
@@ -507,8 +508,9 @@ def _upsert_counter(
507508
defaults={"fiscal_counter_value": amount},
508509
)
509510
if not created:
510-
counter.fiscal_counter_value += amount
511-
counter.save()
511+
FiscalCounter.objects.filter(pk=counter.pk).update(
512+
fiscal_counter_value=F("fiscal_counter_value") + amount
513+
)
512514
except DatabaseError as exc:
513515
logger.exception(f"Failed to upsert {counter_type} counter for device {self._device}")
514516
raise ReceiptSubmissionError(f"Failed to update {counter_type} fiscal counter") from exc

0 commit comments

Comments
 (0)