Fix unknown race condition in obtaining Result objects

For unclear reasons, multiple Result objects can be created with the same test and machine, despite the ideal situation in that no machine is ever concurrently talking to the server.

In the event a duplicate occurs, all future requests would have created an additional duplicate, as Django objects obtained via .get() expect EXACTLY one response.

It is unclear how this race condition could have occured in practice
This commit is contained in:
andrew committed 2024-01-31 22:59:18 -05:00
1 parent 9a29658110
commit 1d75616503
1 file changed
+2 -3
+2 -3
View File
@@ -42,9 +42,8 @@ def get_workload(machine):
if not (test := select_workload(machine)):
return {}
# Fetch or create the Result object for the test
try: result = Result.objects.get(test=test, machine=machine)
except: result = Result(test=test, machine=machine)
# Avoid creating duplicate Result objects
result, created = Result.objects.get_or_create(test=test, machine=machine)
# Update the Machine's status and save everything
machine.workload = test.id;