Is there a race condition when allocating stock?
See original GitHub issuethere are several methods in the partner.StockRecord
model that look a bit like this:
def allocate(self, amount, reference='', status=''):
"""
Convenience method for ring-fencing money against this source
"""
self.amount_allocated += amount
self.save()
I didn’t dig too deep but I didn’t find any use of select_for_update
in the source so I think even knowing that everything’s wrapped in a per-request transaction there’s a possible race condition here?
Should the methods do something like this to be safer?
def allocate(self, amount, reference='', status=''):
"""
Convenience method for ring-fencing money against this source
"""
self.__class__.objects.select_for_update().filter(pk=self.pk).update(
amount_allocated=F('amount_allocated') + amount)
Issue Analytics
- State:
- Created 8 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
6.3. Race Conditions and Critical Sections
A race condition is the general term for a bug that arises from the nondeterministic timing of execution. If the threads are scheduled...
Read more >Concurrency and Race Conditions - O'Reilly
This sequence of events is a demonstration of a race condition. Race conditions are a result of uncontrolled access to shared data. When...
Read more >Where's the Race Condition? - by Jim Cownie - CPU fun
The race condition that we are trying to avoid occurs when the owner thread is incrementing the base to claim the final iteration,...
Read more >Race condition - Wikipedia
Race conditions can occur especially in logic circuits, multithreaded, or distributed software programs.
Read more >What is a Race Condition? - TechTarget
A race condition is an undesirable situation that occurs when a device or system attempts to perform two or more operations at the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
coming back on my earlier comments, I think
select_for_update
is not necessary when doing an atomicupdate
, you would only use it where you are first selecting, then modifying the instance, then savingyou could manually send those signals for the affected rows, e.g.
Fixed in #2281.