Fix discovery timeouts leaking Timeout::ExitException
FetchEvent wrapped its wait in Timeout.timeout and FetchLatestEvent wrapped the relay loop in another one. The nesting leaked Timeout::ExitException, which is neither a Timeout::Error nor a StandardError, so it escaped the rescue and surfaced as a 500 on the metadata endpoint. - FetchEvent waits on the condition variable with a 5s timeout instead of Timeout.timeout, and wakes immediately when the relay errors - FetchLatestEvent uses a monotonic deadline instead of Timeout.timeout, and skips a relay that raises, so one bad relay cannot abort discovery - PublishEvent uses thread.join(TIMEOUT) instead of Timeout.timeout
This commit is contained in:
5 files changed
+64
-29
No files matched your search
@@ -1,6 +1,6 @@
|
||||
module NostrManager
|
||||
class FetchEvent < NostrManagerService
|
||||
TIMEOUT = 10
|
||||
TIMEOUT = 5
|
||||
|
||||
def initialize(filter:, relay_url:)
|
||||
@filter = filter
|
||||
@@ -11,6 +11,7 @@ module NostrManager
|
||||
def call
|
||||
filter, client, relay = @filter, @client, @relay
|
||||
event = nil
|
||||
errored = false
|
||||
mutex = Mutex.new
|
||||
received_event = ConditionVariable.new
|
||||
log_prefix = "[nostr][#{@relay.name}]"
|
||||
@@ -22,7 +23,10 @@ module NostrManager
|
||||
|
||||
client.on :error do |e|
|
||||
Rails.logger.info "#{log_prefix} Error: #{e}"
|
||||
Thread.current.exit
|
||||
mutex.synchronize do
|
||||
errored = true
|
||||
received_event.signal
|
||||
end
|
||||
end
|
||||
|
||||
client.on :message do |m|
|
||||
@@ -43,18 +47,16 @@ module NostrManager
|
||||
client.connect relay
|
||||
end
|
||||
|
||||
begin
|
||||
Timeout.timeout(TIMEOUT) do
|
||||
mutex.synchronize do
|
||||
received_event.wait(mutex) if event.nil?
|
||||
end
|
||||
end
|
||||
rescue Timeout::Error
|
||||
Rails.logger.debug "#{log_prefix} Timeout: No event received within #{TIMEOUT} seconds"
|
||||
ensure
|
||||
thread.exit
|
||||
mutex.synchronize do
|
||||
received_event.wait(mutex, TIMEOUT) if event.nil? && !errored
|
||||
end
|
||||
|
||||
if event.nil? && !errored
|
||||
Rails.logger.debug "#{log_prefix} Timeout: No event received within #{TIMEOUT} seconds"
|
||||
end
|
||||
|
||||
thread.exit
|
||||
|
||||
event
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,23 +10,36 @@ module NostrManager
|
||||
|
||||
def call
|
||||
events = []
|
||||
deadline = monotonic_now + TIMEOUT
|
||||
|
||||
begin
|
||||
Timeout.timeout(TIMEOUT) do
|
||||
@relays.each do |url|
|
||||
event = NostrManager::FetchEvent.call(filter: @filter, relay_url: url)
|
||||
|
||||
if event.present? && events.none? { |e| e["id"] == event["id"] }
|
||||
events << event
|
||||
break if events.size >= @max_events
|
||||
end
|
||||
end
|
||||
@relays.each do |url|
|
||||
if monotonic_now >= deadline
|
||||
Rails.logger.debug "[nostr] Timeout after #{TIMEOUT}s, found #{events.size} events"
|
||||
break
|
||||
end
|
||||
rescue Timeout::Error
|
||||
Rails.logger.debug "[nostr] Timeout after #{TIMEOUT}s, found #{events.size} events"
|
||||
|
||||
event = fetch(url)
|
||||
next if event.blank?
|
||||
next if events.any? { |e| e["id"] == event["id"] }
|
||||
|
||||
events << event
|
||||
break if events.size >= @max_events
|
||||
end
|
||||
|
||||
events.max_by { |e| e["created_at"] }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch(url)
|
||||
NostrManager::FetchEvent.call(filter: @filter, relay_url: url)
|
||||
rescue StandardError => e
|
||||
Rails.logger.info "[nostr] Error fetching from #{url}: #{e.class}: #{e.message}"
|
||||
nil
|
||||
end
|
||||
|
||||
def monotonic_now
|
||||
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -48,13 +48,11 @@ module NostrManager
|
||||
client.connect relay
|
||||
end
|
||||
|
||||
begin
|
||||
Timeout.timeout(TIMEOUT) { thread.join }
|
||||
rescue Timeout::Error
|
||||
if thread.join(TIMEOUT).nil?
|
||||
Rails.logger.debug "#{log_prefix} Timeout: no relay response within #{TIMEOUT}s"
|
||||
ensure
|
||||
thread.exit
|
||||
end
|
||||
|
||||
thread.exit
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -224,6 +224,19 @@ RSpec.describe "Settings", type: :request do
|
||||
end
|
||||
end
|
||||
|
||||
context "when a discovery relay fails" do
|
||||
it "returns an empty result instead of raising" do
|
||||
allow(NostrManager::FetchEvent).to receive(:call)
|
||||
.and_raise(Timeout::Error, "execution expired")
|
||||
|
||||
get nostr_metadata_settings_path(step: "relays"),
|
||||
headers: { "HTTP_ACCEPT" => "application/json" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)).to eq("next" => "profile", "event_id" => nil)
|
||||
end
|
||||
end
|
||||
|
||||
context "with step=profile" do
|
||||
it "discovers the profile and asks for the media server list next" do
|
||||
expect(NostrManager::DiscoverUserEvent).to receive(:call)
|
||||
|
||||
@@ -14,6 +14,15 @@ RSpec.describe NostrManager::FetchLatestEvent, type: :model do
|
||||
end
|
||||
end
|
||||
|
||||
describe "with all relays failing" do
|
||||
it "returns nil instead of raising" do
|
||||
allow(NostrManager::FetchEvent).to receive(:call).and_raise(Timeout::Error)
|
||||
|
||||
result = described_class.call(relays: ["wss://a", "wss://b"], filter: filter)
|
||||
expect(result).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "with event on 1 relay only" do
|
||||
it "returns the event" do
|
||||
allow(NostrManager::FetchEvent).to receive(:call) do |args|
|
||||
|
||||
Reference in new issue
Block a user