Skip to content

server: apply the header-read timeout before protocol detection - #312

Draft
iainmcgin wants to merge 1 commit into
mainfrom
iain/detection-timeout
Draft

iainmcgin wants to merge 1 commit into
mainfrom
iain/detection-timeout

Conversation

@iainmcgin

Copy link
Copy Markdown
Collaborator

A peer that connects and sends nothing, or only part of the HTTP/2 preface, is held open until it hangs up, even with the default 30 s header-read timeout: hyper-util's auto connection has no timer while it picks a protocol, and hyper starts header_read_timeout only after that. 0.8.1 and 0.9.0 have the same code path.

The accepted stream is now wrapped in a private reader that applies hyper-util 0.1.20's ReadVersion end test to the bytes read and fails the read with io::ErrorKind::TimedOut if the header-read timeout passes first. The connection then ends with CloseReason::Error. After detection the wrapper passes reads through, and hyper's own timer starts, so the first HTTP/1.1 head can take up to twice the timeout.

An HTTP/2 connection that has sent the preface is still not bound by this timeout, because idle HTTP/2 clients are normal. A client or proxy that pre-opens connections and sends nothing now has them closed after 30 s.

A header-read timeout too long to add to the clock (Duration::MAX) now disables the bound instead of panicking every HTTP/1.1 connection. Upgraded IO can no longer be recovered with hyper-util's upgrade::downcast, because the stream type is now private; 0.9.0's axum::serve_tls allowed that.

hyper-util's auto connection reads until it can pick HTTP/1.1 or HTTP/2
and has no timer while it does, and hyper arms its header-read timeout
only after that. A peer that connected and sent nothing, or only part of
the HTTP/2 preface, therefore held its task and socket until it hung up,
even with the default 30 second timeout.

Wrap the accepted stream so its reads fail with `TimedOut` if the
header-read timeout passes before the bytes read end detection, using
the same test as hyper-util. After detection the wrapper passes reads
through. An HTTP/2 connection that has sent its preface is still bounded
only by the idle and age retirement settings. A timeout too long to add
to the clock now disables the bound instead of panicking the connection.

Signed-off-by: Iain McGinniss <309153+iainmcgin@users.noreply.github.com>
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.

1 participant