Callback on_request_error hanging when (re-)raising error
I would like to have a single base config for httpx that always raises the error. This saves me from having to call afterwards on the response.raise_for_status for every httpx request.
When not raising an error in on_request_error it returns the response as expected
# irb, httpx 1.4.4
HTTPX.plugin(:callbacks).on_request_error do |request, error|
puts "[HTTPX] Request error (#{request.verb} #{request.uri})";
puts error
end.get('http://unknownhost')
[HTTPX] Request error (GET http://unknownhost)
unknown DNS error (error code 2)
=> #<HTTPX::ErrorResponse:0x0000000131570af8...
When raising an error in on_request_error it hangs:
HTTPX.plugin(:callbacks).on_request_error do |request, error|
puts "[HTTPX] Request error (#{request.verb} #{request.uri})";
puts error
raise error # Same thing happens with raise 'foo'
end.get('http://unknownhost')
[HTTPX] Request error (GET http://unknownhost)
unknown DNS error (error code 2)
(no output, keeps hanging until pressing control-c)
^C/Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:196:in 'Array#each': abort then interrupt! (IRB::Abort)
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:196:in 'Enumerable#filter_map'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:196:in 'HTTPX::Selector#next_timeout'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:31:in 'block in HTTPX::Selector#next_tick'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:30:in 'Kernel#catch'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:30:in 'HTTPX::Selector#next_tick'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:62:in 'HTTPX::Selector#terminate'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/session.rb:76:in 'HTTPX::Session#close'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/plugins/callbacks.rb:94:in 'HTTPX::Plugins::Callbacks::InstanceMethods#close'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/session.rb:289:in 'HTTPX::Session#send_requests'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/session.rb:106:in 'HTTPX::Session#request'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/chainable.rb:10:in 'HTTPX::Chainable#get'
from (irb):63:in '<main>'
from <internal:kernel>:168:in 'Kernel#loop'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/irb-1.15.2/exe/irb:9:in '<top (required)>'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/bin/irb:25:in 'Kernel#load'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/bin/irb:25:in '<main>'
/Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/resolver/native.rb:307:in 'HTTPX::Resolver::Native#parse': unknown DNS error (error code 2) (HTTPX::NativeResolveError)
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/resolver/native.rb:242:in 'block in HTTPX::Resolver::Native#dread'
from <internal:kernel>:168:in 'Kernel#loop'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/resolver/native.rb:201:in 'HTTPX::Resolver::Native#dread'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/resolver/native.rb:110:in 'block in HTTPX::Resolver::Native#consume'
from <internal:kernel>:168:in 'Kernel#loop'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/resolver/native.rb:109:in 'HTTPX::Resolver::Native#consume'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/resolver/native.rb:60:in 'HTTPX::Resolver::Native#call'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:185:in 'HTTPX::Selector#select_one'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:122:in 'HTTPX::Selector#select'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:38:in 'block in HTTPX::Selector#next_tick'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:30:in 'Kernel#catch'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/selector.rb:30:in 'HTTPX::Selector#next_tick'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/session.rb:313:in 'block (2 levels) in HTTPX::Session#receive_requests'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/session.rb:313:in 'Kernel#catch'
from /Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/session.rb:313:in 'block in HTTPX::Session#receive_requests'
from <internal:kernel>:168:in 'Kernel#loop'
... 24 levels...
Only seems to happen (looping?) somehow in on_request_error. In on_response_completed it does correctly immediately raise the error:
HTTPX.plugin(:callbacks).on_response_completed do |request, response|
puts "[HTTPX] Response #{response.status}"
response.raise_for_status
end.get('http://localhost/not-found')
[HTTPX] Request error (GET http://unknownhost)
[HTTPX] Response 404
/Users/harm.dewit/.asdf/installs/ruby/3.4.2/lib/ruby/gems/3.4.0/gems/httpx-1.4.4/lib/httpx/response.rb:155:in 'HTTPX::Response#raise_for_status': HTTP Error: 404 {"server" => ["nginx/1.27.4"], "date" => ["Thu, 17 Apr 2025 15:41:24 GMT"], "content-type" => ["text/html"], "content-length" => ["153"], "connection" => ["close"]} (HTTPX::HTTPError)