DEV: Fix FinalDestination::Resolver race condition (#19558)

We were adding to the resolver's work queue before setting up the `@lookup` and `@parent` information. That could lead to the lookup being performed on the wrong (or `nil`) hostname. This also lead to some flakiness in specs.
This commit is contained in:
David Taylor 2022-12-21 16:02:24 +00:00 committed by GitHub
parent 2304761223
commit f1ec8c869a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 5 deletions

View File

@ -8,12 +8,14 @@ class FinalDestination::Resolver
@result = nil
@queue ||= Queue.new
@queue << ""
ensure_lookup_thread
@lookup = addr
@parent = Thread.current
# Wakeup the worker thread
@queue << ""
# This sleep will be interrupted by the lookup thread
# if completed within timeout
sleep timeout

View File

@ -18,16 +18,12 @@ describe FinalDestination::Resolver do
expect {
result = FinalDestination::Resolver.lookup("sleep.example.com", timeout: 0.001)
# If the test gets this far, it failed
puts "Flaky test debug: Result was #{result.inspect}"
}.to raise_error(Timeout::Error)
start_thread_count = alive_thread_count
expect {
result = FinalDestination::Resolver.lookup("sleep.example.com", timeout: 0.001)
# If the test gets this far, it failed
puts "Flaky test debug: Result was #{result.inspect}"
}.to raise_error(Timeout::Error)
expect(alive_thread_count).to eq(start_thread_count)