Repository navigation
Description
Problem Description
In mitmproxy v12.3.1, a TLS HTTP/2 client connection to an HTTP/1 reverse
proxy can leave upstream socket writers and
ConnectionHandler.transports entries open after ordinary requests finish.
The HTTP/1 origin in the reproducer simply returned Connection: close and
closed its writer. Repeated requests on one long-lived H2 connection accumulated FDs until the proxy could no longer serve an independent client.
With the tested container's soft nofile=1024 limit, this produced
56.106 s and 56.099 s independent-probe outages in two fresh stock
runs. The FD limit is a condition of this result.
Proposal
Please make upstream transport cleanup cancellation-safe: close the writer
and remove its ConnectionHandler.transports entry on every terminal path,
including cancellation while an earlier async step is pending. Idempotent
finalization may be appropriate. A regression test could use many completed
H2-to-H1 requests on one H2 parent, a normal Connection: close origin, and
an independent service probe under a controlled FD limit.
Alternatives
Increasing nofile postponed the observed outage. Bounding HttpLayer.connections history may reduce lookup work and memory, but it would not by itself close writers owned by ConnectionHandler.transports. A per-parent upstream-transport cap would
limit the pressure, but completed transports should still be released.
Additional context
The isolated reproducer requested 2,048 normal responses through one H2
parent with a 64-stream pipeline; the control sent the same 2,048 requests
through H2 parents rotated every 64 requests. In 2/2 stock 4 GiB / 1 CPU
pairs, the one-parent client last acknowledged request 1,056, then saw a
502. The proxy reached 1,024 FDs and logged OSError: [Errno 24] Too many open files.
At nofile=4096, the 2,048-request profile stayed healthy but about
1,944 FDs remained after the client exited. In a separate 128-request
diagnostic pair, the one-parent arm retained 122 CLOSED transport
entries with cancelled handlers and writers not closing, versus 14 in
an eight-parent control; process socket FDs were 125 versus 17. Both
diagnostic arms remained healthy.
On the tested commit, ConnectionHandler.run() cancels transport handlers,
while handle_connection() closes/removes the writer only near its end.
The H2/H1 path also retains a parent-scoped
HttpLayer.connections history.
Tested with mitmproxy v12.3.1 (commit
51c1d6b2d8d3367a773411eb7a3eeccec82b3ea7), Linux Docker, stock
mitmdump without addons for the impact runs. The lab used
reverse:http://origin:8000 and block_global=false; an untrusted client
must be able to reach the listener.
Activity