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

(PUP-11853) Wait for request completion before closing #9084

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/puppet/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ def execute_streaming(request, options: {}, &block)
apply_auth(request, basic_auth) if redirects.zero?

# don't call return within the `request` block
close_and_sleep = nil
http.request(request) do |nethttp|
response = Puppet::HTTP::ResponseNetHTTP.new(request.uri, nethttp)
begin
Expand All @@ -380,12 +381,14 @@ def execute_streaming(request, options: {}, &block)
interval = @retry_after_handler.retry_after_interval(request, response, retries)
retries += 1
if interval
if http.started?
Puppet.debug("Closing connection for #{Puppet::HTTP::Site.from_uri(request.uri)}")
http.finish
close_and_sleep = proc do
if http.started?
Puppet.debug("Closing connection for #{Puppet::HTTP::Site.from_uri(request.uri)}")
http.finish
end
Puppet.warning(_("Sleeping for %{interval} seconds before retrying the request") % { interval: interval })
::Kernel.sleep(interval)
end
Puppet.warning(_("Sleeping for %{interval} seconds before retrying the request") % { interval: interval })
::Kernel.sleep(interval)
next
end
end
Expand All @@ -404,6 +407,10 @@ def execute_streaming(request, options: {}, &block)

done = true
end
ensure
# If a server responded with a retry, make sure the connection is closed and then
# sleep the specified time.
close_and_sleep.call if close_and_sleep
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/integration/http/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@
end
end

context 'ensure that retrying does not attempt to read the body after closing the connection' do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When running this test on 7.x, you can generate the original read body out of block error:

     Puppet::HTTP::HTTPError:
       Request to https://127.0.0.1:57572 failed after 1.01 seconds: attempt to read body out of block
     # ./lib/puppet/http/client.rb:455:in `raise_error'
     # ./lib/puppet/http/client.rb:166:in `rescue in connect'
     # ./lib/puppet/http/client.rb:136:in `connect'
     # ./lib/puppet/http/client.rb:366:in `execute_streaming'
     # ./lib/puppet/http/client.rb:275:in `post'
     # ./spec/integration/http/client_spec.rb:189:in `block (4 levels) in <top (required)>'
     # ./spec/lib/puppet_spec/https.rb:81:in `block in start_server'
     # ./spec/lib/puppet_spec/https.rb:41:in `pipe'
     # ./spec/lib/puppet_spec/https.rb:41:in `start_server'
     # ./spec/integration/http/client_spec.rb:186:in `block (3 levels) in <top (required)>'
     # ./spec/spec_helper.rb:180:in `block (2 levels) in <top (required)>'
     # ./vendor/bundle/ruby/2.7.0/gems/webmock-3.18.1/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # IOError:
     #   attempt to read body out of block
     #   ./lib/puppet/http/response_net_http.rb:18:in `body'

let(:client) { Puppet::HTTP::Client.new(retry_limit: 1) }
it 'raises a retry error instead' do
response_proc = -> (req, res) {
res['Retry-After'] = 1
res.status = 503
}

https_server.start_server(response_proc: response_proc) do |port|
uri = URI("https://127.0.0.1:#{port}")
kwargs = {headers: {'Content-Type' => 'text/plain'}, options: {ssl_context: root_context}}
expect{client.post(uri, '', **kwargs)}.to raise_error(Puppet::HTTP::TooManyRetryAfters)
end
end
end

context 'persistent connections' do
it "detects when the server has closed the connection and reconnects" do
Puppet[:http_debug] = true
Expand Down