Skip to content

Close the CONNECT tunnel when a proxied socket is closed - #253

Open
ekmartin wants to merge 8 commits into
socketry:mainfrom
ekmartin:ek-conductor/async-http-proxy-fix
Open

ekmartin wants to merge 8 commits into
socketry:mainfrom
ekmartin:ek-conductor/async-http-proxy-fix

Conversation

@ekmartin

@ekmartin ekmartin commented Sep 30, 2026 •

Copy link
Copy Markdown

Problem

When Proxy#connect is called without a block, it returns pipe.to_io and drops the Body::Pipe. Proxied endpoints always connect this way, through SSLEndpoint#connect and the client connection pool, so Pipe#close never runs. Closing the returned socket only half-closes the CONNECT request body. The pipe's reader keeps reading the CONNECT response until the proxy closes its end. Until then the proxy client's connection stays leased, and Client#close blocks with "Waiting for … pool to drain".

Changes

  • Closing the returned socket closes the tunnel. Proxy#connect without a block now returns the pipe's socket extended with Proxy::Tunnel. Its close calls the new Body::Pipe#finish, which stops the reader (closing the CONNECT response) and closes the socket. The pipe's writer then forwards any data already written to the socket and ends the CONNECT request normally. HTTP/2 then resets the stream with RST_STREAM(NO_ERROR), via the orderly-shutdown path from Preserve orderly HTTP/2 duplex shutdown. #248, and HTTP/1 closes the connection, which can't be reused after a tunnel anyway. It stays a real Socket because OpenSSL::SSL::SSLSocket requires one.
  • The block form uses Pipe#finish too. Previously it called Pipe#close, which stopped the writer and discarded data written just before the block exited.
  • ConnectFailure#body exposes up to 8 KiB of the proxy's response body, since proxies such as smokescreen put the rejection reason there. I/O and protocol errors while reading it are ignored. Anything else, such as Async::TimeoutError, propagates to the caller.

Notes for review

  • Closing now completes a scheduler tick or so later. The pipe's writer, then the HTTP/1 tunnel task or HTTP/2 output task, have to forward the remaining data before the connection is released. A Client#close on the proxy client immediately afterwards may briefly log "Waiting for … pool to drain", as it already does on main, but it no longer waits for the proxy to close its end.
  • A proxy that stops reading can delay the release. If the proxy stops accepting data (HTTP/2 flow control, or TCP backpressure on HTTP/1), forwarding the pending data waits until it does.
  • Reading ConnectFailure#body can block. For example, an HTTP/1 error response with no content-length on a connection the proxy keeps open. It is bounded only by the caller's own timeout, and this is documented on ConnectFailure#body.
  • Not addressed: after a rejected CONNECT, the HTTP/1 pool reports busy for one scheduler tick. This already happens on main.

🤖 Generated with Claude Code

ekmartin and others added 2 commits September 30, 2026 00:20
Closing the socket returned by `Proxy#connect` without a block only
half-closed the CONNECT request body, so the proxy client's connection
stayed leased (and `Client#close` blocked) until the proxy closed its end
of the tunnel. Closing it now stops the pipe and closes the CONNECT request
and response with an error. The block form closes the same way.

`ConnectFailure#body` also exposes up to 8 KiB of the proxy's response body.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Closing the tunnel stopped the pipe's writer and closed the CONNECT request
with an error, discarding any data written to the socket that hadn't been
forwarded yet (e.g. `QUIT\r\n` followed immediately by `close`). Instead,
stop only the reader and close the socket, so the writer forwards the
remaining data and ends the request normally. HTTP/2 then resets the stream
with `NO_ERROR` and HTTP/1 closes the connection. The block form closes the
same way.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ekmartin

Copy link
Copy Markdown
Author

🤖 This comment was written by Claude (Claude Code) on behalf of @ekmartin.

Fixed in 351ef6d: closing a tunnel no longer drops pending writes.

Problem: writing QUIT\r\n and closing the socket straight away delivered zero bytes on HTTP/1.1 and HTTP/2, while the base revision delivered all six. Tunnel#close stopped the pipe's writer and closed the CONNECT request with an error. That discarded anything still sitting in the socket, and anything already queued in the request body, because Writable#read raises the stored error before returning queued chunks. The block form's pipe.close on main had the same problem for data written just before the block exited.

What changed:

  • New Body::Pipe#finish: it stops only the reader, which closes the CONNECT response, and then closes the socket. The writer forwards whatever is left and ends the CONNECT request normally. After that, HTTP/2 resets the stream with RST_STREAM(NO_ERROR), using the orderly-shutdown path from Preserve orderly HTTP/2 duplex shutdown. #248, and HTTP/1 closes the connection.
  • Both forms use it: Tunnel#close and the block form of Proxy#connect both call finish.
  • The error-based close is gone: Tunnel::Closed and Pipe#close(error) are removed, so HTTP/2 proxies no longer see an INTERNAL_ERROR reset. Pipe#close is unchanged from main.
  • Tests:
    • a new test writes QUIT\r\n and closes immediately, for both forms on HTTP/1.0, HTTP/1.1 and HTTP/2 (it failed 6 of 6 before the fix);
    • a new unit test checks that Pipe#finish forwards buffered data.

Trade-offs:

  • The connection is released a tick or two later, once the pending data has been forwarded. A Client#close on the proxy client straight afterwards may briefly log "Waiting for … pool to drain", as it already does on main, but it no longer waits for the proxy to close its end.
  • A proxy that stops reading can delay the release. With HTTP/2 flow control or TCP backpressure, the release waits until the proxy reads the pending data. The previous version avoided this by discarding the data.

I've updated the PR description to match.

@n-at-han-k

n-at-han-k commented Oct 18, 2026 •

Copy link
Copy Markdown

When Proxy#connect is called without a block, it returns pipe.to_io and drops the Body::Pipe. Proxied endpoints always connect this way, through SSLEndpoint#connect and the client connection pool, so Pipe#close never runs. Closing the returned socket only half-closes the CONNECT request body. The pipe's reader keeps reading the CONNECT response until the proxy closes its end. Until then the proxy client's connection stays leased, and Client#close blocks with "Waiting for … pool to drain".

Have you a code sample?

@ekmartin

Copy link
Copy Markdown
Author

Here you go!

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

This outputs:

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

== PR branch ==
WARN: Waiting for Async::HTTP::Protocol::HTTP pool to drain: ...
Response: Hello World!
proxy_client.close took 0.00s

@n-at-han-k

n-at-han-k commented Oct 18, 2026 •

Copy link
Copy Markdown

So if I'm right... you're saying that the issue is calling #to_io returns the IO object, but the newly created pipe is left inaccessible. Shown here:

pipe = Body::Pipe.new(response.body, input)
return pipe.to_io unless block_given?
begin
yield pipe.to_io
ensure
pipe.close
end

So the question remains... "What happens to the Body::Pipe, and how is it closed?"

It seems that it's kept alive by the two tasks below, so in order to answer the question we must understand the choice to use transient, and how tasks currently come to a finish.

task.async(transient: true, &self.method(:reader))
task.async(transient: true, &self.method(:writer))

ekmartin and others added 6 commits November 19, 2026 20:47
If the CONNECT request raised (e.g. the proxy closed the connection, or
the caller's timeout expired), `Proxy#connect` never closed the request
body. On HTTP/1, the task writing the body was left waiting forever, even
after the client was closed. On HTTP/2, that task and the stream remained
until the connection was closed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Closing the socket of a tunnel now closes its descriptor and signals a
task on the owning reactor, which finishes the pipe. This allows the
socket to be closed from any thread, and a clone of the socket no longer
closes the tunnel.

After the socket is closed, the tunnel waits up to `close_timeout` (10
seconds by default) for pending data to be sent, then closes the CONNECT
response, which resets an HTTP/2 stream whose peer has stopped reading.

`Proxy#connect` closes the request body in an `ensure`, without the
error, as `Async::Cancel` is not defined by async 2.35.1, and passing the
error made the HTTP/2 task sending the body fail with it too. Instead,
the HTTP/2 client cancels the stream when the caller stops waiting for
the response while the request body is still being sent.

`ConnectFailure#body` no longer buffers more of a chunk than it needs.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- The HTTP/2 client cancels an abandoned stream from a separate task, so
  a caller whose timeout expired is not delayed when the connection can't
  be written to.
- The tunnel waits for its close timeout using `Thread::Queue`, as
  interrupting a wait on a condition variable with an exception fails on
  Ruby 3.3.6.
- Closing an HTTP/2 response with an error resets the stream even if the
  response has no body, so the close timeout also applies to a CONNECT
  response which the proxy has already finished.
- `HTTP2::Output` rescues `Async::Stop`, as `Async::Cancel` is not defined
  by async 2.35.1.
- `Proxy.new` accepts headers as a trailing hash again.
- Closing the socket of a tunnel shuts it down, so that a clone of the
  socket observes the end of the stream.
- Closing both sides of the socket separately closes the tunnel, and the
  task which closes the tunnel exits once both directions have finished.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
# GC.start
end
rescue Async::Cancel, StandardError => error
rescue Async::Stop, StandardError => error

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

🤖 This comment was written by Claude (Claude Code) on behalf of the PR author.

Why Async::Stop and not Async::Cancel: the gemspec allows async >= 2.35.1, and 2.35.1 defines Async::Stop but has no Async::Cancel constant. With rescue Async::Cancel, evaluating this clause raised NameError: uninitialized constant Async::Cancel whenever the task writing the body was stopped or failed, so the task ended with that NameError.

In current async (checked on 2.46.0) Async::Stop is an alias (Stop = Cancel), so this rescues the same exception there.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants