Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition in recording results #305

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ChrisBr
Copy link
Contributor

@ChrisBr ChrisBr commented Feb 6, 2025

I believe there is a race condition between acknowledge and record.

When we call acknowledge it will immediately remove the test from the running set. We only record failure in the next step in record. However, we check the running set in exhausted? in the summary to decide when the queue is empty. There is a possibility of removing a test from the running set and the summary exiting before we've actually recorded the failure. acknowledge and record should be a single operation to avoid this.

elsif queue.acknowledge(example)
reporter.record(result)

def exhausted?
queue_initialized? && size == 0
end

def size
redis.multi do |transaction|
transaction.llen(key('queue'))
transaction.zcard(key('running'))
end.inject(:+)
end

until exhausted? || time_left <= 0 || max_test_failed? || time_left_with_no_workers <= 0

redis.call('zrem', zset_key, test)
redis.call('hdel', owners_key, test) -- Doesn't matter if it was reclaimed by another workers

@ChrisBr ChrisBr force-pushed the cbruckmayer/record-idempotent branch 2 times, most recently from 76e6509 to d5e6392 Compare February 6, 2025 15:07
@ChrisBr ChrisBr force-pushed the cbruckmayer/record-idempotent branch from d5e6392 to 8cb16a3 Compare February 6, 2025 15:08
@@ -11,6 +11,157 @@ class << self
self.requeue_offset = 42

class Worker < Base
class ErrorReport
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Find new location for ErrorReport and FailureFormatter. This may break public API?

@ChrisBr
Copy link
Contributor Author

ChrisBr commented Feb 6, 2025

@byroot any opinion on this? RspecQueue makes this quite a difficult refactor... The other option is to move acknowledge down into the reporter and call it as part of report_success / report_error which may be simpler.

@byroot
Copy link
Contributor

byroot commented Feb 6, 2025

any opinion on this?

I haven't spent too much time looking at the implemementation, but yes, the error has to be recorded in the same transaction as the test is acknowledged for it to be fully safe, which is what I do in megatest: https://github.com/byroot/megatest/blob/16fcf099b78fd9d9f8b0b5e5132beff30f5285f2/lib/megatest/redis_queue.rb#L300-L320 (should be a multi instead of a pipeline though).

But the challenge as you noticed is to get all that data at once, which is challenging when there isn't an extension API designed to give you that.

For the rspec part, IIRC it was marked as deprecated ages ago, you could just remove it if it's really in the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants