Skip to content

Add timer to hyper server for http2_keepalive_interval - #1726

Merged
djc merged 4 commits into
grpc:masterfrom
AmeliasCode:master
Jun 14, 2024
Merged

djc merged 4 commits into
grpc:masterfrom
AmeliasCode:master

Conversation

@AmeliasCode

Copy link
Copy Markdown
Contributor

Motivation

As described in this issue: #1725 the recent upgrade to hyper 1 has broken services that use http2_keepalive_interval. This was due to a missing timer, resulting in panics with the message You must supply a timer. See here: https://github.com/hyperium/hyper/blob/master/src/common/time.rs#L36

Solution

This solution adds in a timer to the server via this method: https://docs.rs/hyper-util/0.1.5/hyper_util/server/conn/auto/struct.Http2Builder.html#method.timer

The timer is a standard tokio timer: https://docs.rs/hyper-util/0.1.5/hyper_util/rt/tokio/struct.TokioTimer.html

Fixes #1725

@djc djc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@djc
djc added this pull request to the merge queue Jun 14, 2024
Merged via the queue into grpc:master with commit 98478ac Jun 14, 2024
@akiradeveloper

Copy link
Copy Markdown

@AmeliasCode Thanks to your work. Unfortunately, my test still fails with the fix.

   Compiling tonic v0.11.0 (https://github.com/hyperium/tonic#98478acc)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 56.86s
     Running `target/debug/tonic-hyper10-debug 1`
test 1 (fail)
thread 'main' panicked at /Users/akira/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hyper-1.3.1/src/common/time.rs:37:17:
You must supply a timer.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Apparently, your test adds http2_keepalive_interval at the server-side but my test add it on client side. I guess this is the reason my test isn't fixed.

            let endpoint = Endpoint::from_static("http://localhost:50000")
                // commenting out this line turn it into pass.
                .http2_keep_alive_interval(std::time::Duration::from_secs(1))
                .keep_alive_while_idle(true)
                .timeout(std::time::Duration::from_secs(5))
                .connect_timeout(std::time::Duration::from_secs(5));

Is enabling keepalive at client-side invalid? If so, it should be removed from the API.

nit: The commit ordering is very strange. The integration test should have been added at first.

@AmeliasCode

Copy link
Copy Markdown
Contributor Author

@akiradeveloper Sorry for missing that you were experiencing the error on the client side. I was seeing it on the server side so that's what I was focusing on. I have a fix for the client side as well that I'll raise in a moment.

LucioFranco added a commit that referenced this pull request Oct 29, 2026
… timeout applies (#2980)

## Motivation

Since the hyper 1.0 upgrade, the server's connection builder only gets a
timer on its HTTP/2 side (added in #1726 for keepalive). hyper's HTTP/1
`header_read_timeout` defaults to 30s, but hyper silently disables it
when no timer is set.

As a result, a server built with `accept_http1(true)`, which `tonic-web`
requires, never times out a client that doesn't finish sending its
request headers. A slowloris client can hold connections open
indefinitely and exhaust the server's file descriptors. Users can't fix
this from outside, since the connection builder is internal and `Server`
exposes no HTTP/1 options.

## Solution

Set `TokioTimer` on the HTTP/1 builder too. This only turns on hyper's
existing 30s default header read timeout. It adds no new API and changes
nothing for HTTP/2-only servers.

Adds `http1_header_read_timeout_is_enforced`, which sends a partial
HTTP/1 request head, advances the clock past 30s, and checks that the
server closed the connection. It fails without the fix and passes with
it.
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.

http2_keep_alive_interval doesn't work

3 participants