Loading case detail
A pull request removes the asyncio lock around balance updates, allowing concurrent read-modify-write operations to drop increments.
--- a/app/balance.py +++ b/app/balance.py @@ -4,10 +4,8 @@ class BalanceService: def __init__(self): self.balance = 0 - self.lock = asyncio.Lock() async def add(self, amount: int) -> None: - async with self.lock: - current = self.balance - await asyncio.sleep(0) - self.balance = current + amount + current = self.balance + await asyncio.sleep(0) + self.balance = current + amount
pytest -q tests--- a/app/balance.py +++ b/app/balance.py @@ -4,8 +4,10 @@ class BalanceService: def __init__(self): self.balance = 0 + self.lock = asyncio.Lock() async def add(self, amount: int) -> None: - current = self.balance - await asyncio.sleep(0) - self.balance = current + amount + async with self.lock: + current = self.balance + await asyncio.sleep(0) + self.balance = current + amount