Skip to content

Async::HTTP::Proxy: closing a proxied connection waits for the proxy to close the CONNECT tunnel #262

Backlog

Description

@ekmartin

🤖 This issue was written by Claude (Claude Code) on behalf of the reporter.

Problem

Closing a connection made through Async::HTTP::Proxy doesn't close the CONNECT tunnel. The proxy client's connection stays leased until the proxy closes its end, so Async::HTTP::Client#close on the proxy client blocks in @pool.wait_until_free ("Waiting for … pool to drain") for as long as the proxy keeps the tunnel open.

Reproduction

A fake CONNECT proxy relays one HTTP response through the tunnel, then closes its end 1s after the client's EOF:

require "async"
require "async/http"
require "async/http/proxy"
require "async/http/body/hijack"

Async do |task|
	proxy_endpoint = Async::HTTP::Endpoint.parse("http://127.0.0.1:8099")
	
	# A fake CONNECT proxy, which relays one HTTP response through the tunnel, then closes its end 1s after the client's EOF:
	app = Protocol::HTTP::Middleware.for do |request|
		Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
			stream.read_until("\r\n\r\n")
			stream.write("HTTP/1.1 200 OK\r\ncontent-length: 12\r\n\r\nHello World!")
			stream.flush
			
			# Wait for the client to close its side of the tunnel:
			while stream.read_partial
			end
			
			sleep(1)
		ensure
			stream.close
		end
	end
	
	server_task = task.async do
		Async::HTTP::Server.new(app, proxy_endpoint).run
	end
	
	proxy_client = Async::HTTP::Client.new(proxy_endpoint)
	
	# Requests through `client` are tunnelled through the proxy with CONNECT:
	client = Async::HTTP::Client.new(proxy_client.proxied_endpoint(Async::HTTP::Endpoint.parse("http://example.com")))
	
	response = client.get("/")
	puts "Response: #{response.read}"
	
	client.close
	
	start_time = Async::Clock.now
	proxy_client.close
	puts "proxy_client.close took %.2fs" % (Async::Clock.now - start_time)
ensure
	server_task&.stop
end

On main (b7029c7, v0.105.0, Ruby 4.0.7, async 2.46.0):

WARN: Waiting for Async::HTTP::Protocol::HTTP pool to drain: ...
Response: Hello World!
proxy_client.close took 1.00s

Expected: proxy_client.close returns promptly once client is closed, without depending on when the proxy closes its end of the tunnel.

Actual: it waits the full delay. A proxy that keeps the tunnel open after the client's EOF delays it for as long as it likes.

Cause

Proxy#connect without a block returns pipe.to_io and drops the Body::Pipe:

def connect(&block)
input = Body::Writable.new
response = @client.connect(authority: @address, headers: @headers, body: input)
if response.success?
pipe = Body::Pipe.new(response.body, input)
return pipe.to_io unless block_given?
begin
yield pipe.to_io
ensure
pipe.close
end

Proxied endpoints always connect this way (through IO::Endpoint::SSLEndpoint#connect and the client connection pool), so Pipe#close never runs. Closing the returned socket only ends the CONNECT request body. The pipe's reader keeps reading the CONNECT response body until the proxy closes it, and until then the connection isn't released to the proxy client's pool.

This is different from #144, which was resolved by closing the client and then the proxy client. Here both are closed in that order, and the second close still waits on the remote end.

Fix

#253 makes closing the returned socket (or leaving the block form) close the whole tunnel: the CONNECT response is closed straight away, and the request is closed once data already written has been forwarded. With that branch the script prints proxy_client.close took 0.00s. The PR includes a test for this on HTTP/1.0, HTTP/1.1 and HTTP/2.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions