Repository navigation
Conversation
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>
Fixed in 351ef6d: closing a tunnel no longer drops pending writes. Problem: writing What changed:
Trade-offs:
I've updated the PR description to match. |
Have you a code sample? |
|
Here you go! This outputs: |
|
So if I'm right... you're saying that the issue is calling async-http/lib/async/http/proxy.rb Lines 100 to 108 in b6bf5b6 So the question remains... "What happens to the 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. async-http/lib/async/http/body/pipe.rb Lines 27 to 28 in b6bf5b6 |
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 |
There was a problem hiding this comment.
🤖 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.
Problem
When
Proxy#connectis called without a block, it returnspipe.to_ioand drops theBody::Pipe. Proxied endpoints always connect this way, throughSSLEndpoint#connectand the client connection pool, soPipe#closenever 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, andClient#closeblocks with "Waiting for … pool to drain".Changes
Proxy#connectwithout a block now returns the pipe's socket extended withProxy::Tunnel. Itsclosecalls the newBody::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 withRST_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 realSocketbecauseOpenSSL::SSL::SSLSocketrequires one.Pipe#finishtoo. Previously it calledPipe#close, which stopped the writer and discarded data written just before the block exited.ConnectFailure#bodyexposes 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 asAsync::TimeoutError, propagates to the caller.Notes for review
Client#closeon the proxy client immediately afterwards may briefly log "Waiting for … pool to drain", as it already does onmain, but it no longer waits for the proxy to close its end.ConnectFailure#bodycan block. For example, an HTTP/1 error response with nocontent-lengthon a connection the proxy keeps open. It is bounded only by the caller's own timeout, and this is documented onConnectFailure#body.main.🤖 Generated with Claude Code