Skip to content

If a client cancels streams the connection is reset. #1010

Open

Description

@nickvikeras

Version

h2 0.4.20 (latest release)

Platform

Linux 6.8.0-1070-gcp x86_64

Summary

The maintainers seem to be already aware of this problem. I am just opening an issue for tracking because #983 hasn't gotten any review traction and #856 was closed without a fix.

I understand this was put in place to protect against an attack vector, but no other http2 library behaves like this. Cancelling streams is a perfectly normal thing to do regularly in steady state. If you cancel streams regularly with h2, the library resets your connection. This essentially makes the library unusable in production for any streaming use case because any other requests on the channel fail when the connection resets.

My use case is streaming multiple range scans back from bigtable, doing a merge-join across them, and cancelling the streams when the join has produced enough rows to fill a page. I can't know how many rows I need from the database until I have joined them, so every request ends in a cancellation. Cancellation is not an edge case.

Since I cancel every bigtable stream, the behavior #983 fixes in h2 is causing spurious fault spikes when the connection resets because the other range scans that were in-flight on those reset connections fail. This is ultimately causing my service to breach its availability SLA.

This is a huge pain point in the rust ecosystem.

Code Sample

Expected Behavior

I can cancel streams and my connection doesn't reset.

Actual Behavior

My connection resets when i cancel too many streams and all my other requests on that connection fail and I get paged and my users have a bad experience.

Additional Context

No response

Activity

  1. seanmonstar commented on Nov 23, 2026

    @seanmonstar
    Member

    Let me try to lay out some of the details of how it works first:

    • There is a concept of "local reset strikes", such that as a peer sends bad things and forces us to send a reset, eventually over a certain amount this will cause the connection to close. This is a defense mechanism (other implementations have something like this) since dealing with a bunch of errors has compute cost and is a vector of attack.
    • When a stream is canceled locally (such as dropping the handles), a CANCEL is sent (which does not count as a reset strike), and the stream is put in a list of recently reset streams.
    • While a stream is part of the "recently reset streams", any incoming frames are ignored (hpack and window size taken care of). This "for a little while", to account for the remote peer sending frames before it learned of the reset.
    • We bound the "recently reset streams" list, because every stream we keep around has a memory cost. Unlimited size could allow the list to grow unbounded. It also has a time limit, since eventually we want to recap that memory, and the remote should know by then that the stream is closed.
    • Eventually, once a stream is finally forgotten about, any new frame that is received will trigger a local STREAM_CLOSED reset. This will count against the "local reset strikes". Without that, a malicious remote can close a stream, and if frames are ignored forever, the remote can force us to consume endless compute. (This can happen with any other implementation that does not limit how much it ignores of forgotten streams.)

    If there is something about the above points that happens to be wrong, that detail would be a bug we should fix.

    But beyond that, I feel like this is working as intended.


    That said, if you have a workflow that requires canceling a significant amount of streams, and are willing to pay the memory cost of "remembering" more of them at a time, there are max_concurrent_reset_streams and max_concurrent_reset_duration options to increase. Is that not sufficient?

  2. nickvikeras commented on Nov 23, 2026

    @nickvikeras
    Author

    While a stream is part of the "recently reset streams", any incoming frames are ignored (hpack and window size taken care of). This "for a little while", to account for the remote peer sending frames before it learned of the reset.

    This is where it's going wrong for me, because I cancel a lot of streams.

    That said, if you have a workflow that requires canceling a significant amount of streams, and are willing to pay the memory cost of "remembering" more of them at a time, there are max_concurrent_reset_streams and max_concurrent_reset_duration options to increase. Is that not sufficient?

    IIRC, there is no way to set these parameters through Tonic. I originally started going down the path of opening a Tonic PR to add that, but then saw #983. I guess making these parameters tunable through Tonic is the path I should pursue for the long term.

    Thanks

  3. nickvikeras commented on Nov 23, 2026

    @nickvikeras
    Author

    Also curious if you think it makes sense to adjust the defaults at all? I haven't done any sort of testing to see how much memory my use case would need, but I'd be happy to provide that data to you in a bit.

  4. seanmonstar commented on Nov 23, 2026

    @seanmonstar
    Member

    I am interested any data that might help provide better defaults, yea. They're mostly "that seems reasonable" guesses. (Currently defaults are up to 50 streams kept for 1 second.)

    Also, it would be helpful to know if the limit and strikes are hit nearly at once, or if it's a slow overflow and eventually the strikes kill the connection.

  5. nickvikeras commented on Nov 23, 2026

    @nickvikeras
    Author

    Oh looks like someone already added this to Tonic: grpc/grpc-rust#2972

    I will try and move my service off my h2 fork and tune these params to see if I can solve the problem that way.

  6. adriangb commented on Nov 23, 2026

    @adriangb

    I agree the defense makes sense when the peer is untrusted. My case is a trusted peer:

    ┌─────────────┐        ┌──────────────┐  fan-out  ┌────────────────┐
    │  Untrusted  │───────>│     Edge     │──────────>│    Internal    │
    │   Client    │        │   Service    │──────────>│     Cache      │
    └─────────────┘        └──────────────┘──────────>└────────────────┘
                                      one long-lived HTTP/2 connection
    

    The Edge service is connecting to the Cache in a trusted manner. The downstream client may disconnect, and we do want to immediately cease work on the fanned-out requests to the cache. But cancelling the Edge -> Cache requests does not mean that Edge is misbehaving or that there is anything wrong. Closing the whole connection only causes more load (errors, retries), not less, because it fails every other request on it.

       Edge Service                       Internal Cache
            │                                   │
            │──────── request (stream 1) ──────>│
            │                                   │
       client disconnects, edge cancels:        │
            │──────── RST_STREAM(CANCEL) ──────>│
            │                                   │
            │<─────── late response DATA ───────│  (already in flight)
            │                                   │
       h2 has forgotten stream 1:               │
            │──────── RST_STREAM(STREAM_CLOSED)>│  +1 strike, never decays
            │                                   │
       ... repeat until 1024 strikes ...        │
            │                                   │
            │──────── GOAWAY ──────────────────>│  all requests on this
            │                                   │  connection fail
    

    I exposed the client knobs in tonic (grpc/grpc-rust#2972, tonic 0.14.9), so it's now easier to work around this. But I do think there's some change we could make here to make this less of a problem with or without config tweaking.

    I had Claude survey (I have not verified it by hand) what other HTTP/2 clients do when frames arrive for a stream they cancelled:

    Library Late frames on a stream the client cancelled Can this close the connection?
    h2 Ignored while in the reset window (50 streams, 1s), then RST_STREAM plus a strike Yes, after 1024 strikes over the connection's lifetime
    Go x/net/http2, grpc-go Ignored, flow control returned No
    Netty, grpc-java Ignored, or a stream-level RST_STREAM that nothing counts No
    nghttp2, Envoy, gRPC C core Ignored for any id at or below the last one sent, flow control returned No
    python-hyper/h2 RST_STREAM that nothing counts (65,536 closed streams remembered) Only an edge case (late HEADERS after eviction)

    Every reset-flood limit it found decays over time: nghttp2 uses a token bucket (burst 1000, 33/s), Netty a fixed window (200 per 30s), Envoy a ratio, and Go a gauge. It found no other implementation with a counter that only goes up.

    Some suggestions that come out of this:

    1. Client: drop frames for any id at or below the last id the client opened, and return connection flow control. This is the direction of fix(client): ignore frames on forgotten streams #983 and fix(client): don't let late frames on canceled streams close unrelated requests #1009. There is open discussion around what sort of attack this might expose h2 users to (and that other implementations are already exposed to). Claude thinks that there isn't any risk, but I did not verify that myself so I won't argue it.
    2. Count each stream at most once. Today a forgotten stream that gets a late frame is reset, re-remembered, and can be forgotten and counted again. With a burst of more than 50 cancels the reset queue churns, so one cancelled stream with a large response can cost many strikes. fix(client): ignore frames on forgotten streams #983 cites Downstream reports of increased go aways with "too_many_internal_resets" #856, where 44 cancels became 1024 resets.
    3. Make max_local_error_reset_streams decay, for example as a token bucket. It still caps resets per second for an attacker. But a healthy long-lived connection would not reach the limit just by running long.

    Even if (1) is blocked, should we look into (2) and (3)?

  7. seanmonstar commented on Nov 24, 2026

    @seanmonstar
    Member

    Count each stream at most once.

    No, the point of the protection is that a malicious peer does not just continuously send frames on an old stream.

    Make max_local_error_reset_streams decay, for example as a token bucket.

    Maybe. I've been considering cleaning up several of these concepts into a temperature/overhead mechanism, kind of like Tomcat has. But, I think this is a separate issue.

    Ultimately, I think there's two cases that different people might be running into:

    • A giant burst past the max_concurrent_reset_streams limit. Configuring it higher should help there.
    • A slow but constant amount of old streams sending past the eviction, ticking of the max reset strikes on a long lived connection.

    I have a PR up to adjust the defaults to be a bit smarter, based on configuration intent: #1013

  8. nickvikeras commented on Nov 25, 2026

    @nickvikeras
    Author

    I configured h2 to use these values on my bigtable client using @adriangb's recent contribution to tonic:

    • http2_max_concurrent_reset_streams = 2_000
    • http2_reset_stream_duration = 10s
    • http2_max_local_error_reset_streams = None

    This solved the problem for me and the additional memory use is negligible, at least for my use case.

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