Avoid a race condition when processing LGTMs. (#4334)

* Avoid a race condition when processing LGTMs.

* Also undo double votes
This commit is contained in:
Jason Robbins 2024-09-05 14:49:46 -07:00 коммит произвёл GitHub
Родитель f4731a9bd1
Коммит b88d7f9b77
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 15 добавлений и 0 удалений

Просмотреть файл

@ -368,6 +368,7 @@ def set_vote(feature_id: int, gate_type: int | None, new_state: int,
if not Vote.is_valid_state(new_state):
raise ValueError('Invalid approval state')
new_vote = None
now = datetime.datetime.now()
existing_list: list[Vote] = Vote.get_votes(feature_id=feature_id,
gate_id=gate_id, set_by=set_by_email)
@ -381,8 +382,17 @@ def set_vote(feature_id: int, gate_type: int | None, new_state: int,
gate_type=gate_type, state=new_state, set_on=now, set_by=set_by_email)
new_vote.put()
if gate:
votes = Vote.get_votes(gate_id=gate.key.integer_id())
# Check for double votes that could arise from a race condition.
double_votes = [v for v in votes
if v.set_by == set_by_email and v.set_on < now]
if new_vote and double_votes:
# This handler lost the race condition, back out and bail.
new_vote.key.delete()
return None
state_was_updated = update_gate_approval_state(gate, votes)
slo_was_updated = slo.record_vote(gate, votes)
if state_was_updated or slo_was_updated:

Просмотреть файл

@ -15,6 +15,8 @@
import re
import logging
import random
import time
from typing import Optional
import settings
@ -366,6 +368,9 @@ class IntentEmailHandler(basehandlers.FlaskHandler):
if (detect_lgtm(body) and
is_lgtm_allowed(from_addr, feature, gate_info)):
logging.info('found LGTM')
# Avoid a race condition when blink-dev is in both To: and Cc: lines.
time.sleep(random.uniform(0, 5))
gate = Gate.get_by_id(gate.key.integer_id()) # Reload after sleep
old_gate_state = gate.state
new_gate_state = approval_defs.set_vote(
feature_id, gate_info.gate_type, Vote.APPROVED, from_addr,