Skip to content

Commit

Permalink
fix: potential race condition when checking if a capture is ongoing o…
Browse files Browse the repository at this point in the history
…r not
  • Loading branch information
Rafiot committed Jan 25, 2024
1 parent 9622531 commit fcfe975
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions bin/background_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,30 @@ def _build_ua_file(self) -> None:

def _retry_failed_enqueue(self) -> None:
'''If enqueuing failed, the settings are added, with a UUID in the 'to_capture key', and they have a UUID'''
for uuid in self.lookyloo.redis.zrevrangebyscore('to_capture', 'Inf', '-Inf'):
try_reenqueue = False
to_requeue: list[str] = []
for uuid, _ in self.lookyloo.redis.zscan_iter('to_capture'):
if self.lookyloo.redis.hexists(uuid, 'not_queued'):
# The capture is marked as not queued
try_reenqueue = True
to_requeue.append(uuid)
elif self.lookyloo.lacus.get_capture_status(uuid) in [CaptureStatusPy.UNKNOWN, CaptureStatusCore.UNKNOWN]:
# The capture is unknown on lacus side. It might be a race condition.
# Let's retry a few times.
retry = 3
while retry > 0:
time.sleep(1)
if self.lookyloo.lacus.get_capture_status(uuid) not in [CaptureStatusPy.UNKNOWN, CaptureStatusCore.UNKNOWN]:
# Was a race condition, the UUID is being processed by Lacus
# Was a race condition, the UUID has been or is being processed by Lacus
self.logger.info(f'UUID {uuid} was only temporary unknown')
break
retry -= 1
time.sleep(3)
else:
# UUID is still unknown
self.logger.info(f'UUID {uuid} is still unknown')
try_reenqueue = True
if not try_reenqueue:
to_requeue.append(uuid)

for uuid in to_requeue:
if not self.lookyloo.redis.zscore('to_capture', uuid):
# The capture has been captured in the meantime.
continue
self.logger.info(f'Found a non-queued capture ({uuid}), retrying now.')
# This capture couldn't be queued and we created the uuid locally
Expand Down

0 comments on commit fcfe975

Please sign in to comment.