Skip to content

Wait for demand after the server closes - #2394

Merged
hyperxpro merged 2 commits into
AsyncHttpClient:mainfrom
mkurz:fix/decompression-half-closure
Nov 21, 2026
Merged

hyperxpro merged 2 commits into
AsyncHttpClient:mainfrom
mkurz:fix/decompression-half-closure

Conversation

@mkurz

@mkurz mkurz commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Motivation:

Since #2386, when the connection closes while a suspended HTTP/1.1 response holds compressed input back, a body that the close cut off is dropped, but the rest of a body that has ended is still decompressed and handed on at once, bounded only by maxDecompressedResponseSize. Netty tears the pipeline down with the close, so nothing can wait for demand.

epoll and io_uring read the close even while reads are suspended, so this happens whenever the server closes right after the body, for example with Connection: close or a body delimited by the connection. With 16 MiB of zeros as gzip and a handler that suspends after every part and never resumes, main delivers 257 parts at once where NIO delivers 1.

This is the follow-up promised on #2362 for output left over when the connection closes, the case #2386 left open.

Modification:

  • While input is held back, Http1ContentDecompressor turns ALLOW_HALF_CLOSURE on for the channel if it was off. The server's FIN then reaches the pipeline as a ChannelInputShutdownEvent instead of a close.
  • On that event, a body that has ended successfully, including one delimited by the connection, keeps waiting for demand. NettyResponseFuture.setKeepAlive(false) makes sure the connection is closed once the response is done instead of being offered to the pool.
  • A body that the close cut off, or whose last chunk could not be parsed, does not wait. The channel is closed at once and channelInactive drops the input held back, as before.
  • The wait applies only while the request is still attached to the connection (future.channel() == ctx.channel()). After a reset, AsyncHttpClientHandler.exceptionCaught has already detached it and expects the close, so the channel is closed at once.
  • Half-closure is turned off again once nothing is held back, at the latest before the end of the body is handed on, and when the handler is removed. If the application turned it on itself, it is left on.
  • The Javadoc of ResponseBodyControl.suspend() and of Http1ContentDecompressor describe the new behaviour.
  • Overriding userEventTriggered drops Netty's internal @ChannelHandlerMask.Skip marker, so pom.xml gets the same scoped Revapi ignore as Http2Handler::userEventTriggered.

Result:

After an orderly close by the server, the rest of a suspended compressed body that has ended is delivered one part per resume on epoll and io_uring, over HTTP and HTTPS, and the connection is closed when the response is done. A connection is never offered to the pool with half-closure on or after the server shut its input down.

Not covered: a close on the client's side, a reset or any other close still reaches channelInactive, where the rest of a body that has ended is decompressed and handed on at once. kqueue should behave like epoll but was not run. Bodies without a content coding and HTTP/2 are unchanged.

The new tests in Http1ContentDecompressorSuspensionTest and Http1DecompressionSuspensionTest fail on main. ./mvnw -B -ntp -Dgpg.skip=true clean verify on JDK 11: 2,168 tests, 0 failures, Revapi passed.

Claude Code on behalf of Matthias Kurz.

A suspended response that held compressed input back still had all of
a body that had ended decompressed and handed on at once when the
server closed the connection, on native transports such as epoll,
which read the close even while reads are suspended. Only the
channel's pipeline held that input, and it is torn down with the
close.

While input is held back, Http1ContentDecompressor now turns on
half-closure for the channel, so that an orderly close by the server
only shuts the input down. The end of the body then decides, as it
does on a close:

- the rest of a body that has ended, including one delimited by the
  connection, still waits for demand, and the response no longer
  keeps the connection alive, so it is closed once the response is
  done;
- a body that the close cut off, or whose last chunk could not be
  parsed, does not wait: the channel is closed at once, which drops
  what is held back of it, and the request is retried or fails as
  before.

Half-closure is turned off again once nothing is held back, at the
latest before the end of the body is handed on, so a connection is
not offered for reuse with it on. If it was on already, it is left
on, and a close by the server while input is held back is handled the
same way.

NIO reads only when asked to, so in the tested cases it saw the close
only once the input held back had been used up and half-closure was
off again. TLS can still ask for a read while reads are suspended, for
example to complete a record.

Claude Code on behalf of Matthias Kurz

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With half-closure on, a reset by the server fails the read before the
transport shuts the input down. AsyncHttpClientHandler then detaches the
request from the connection and expects the transport to close it. The
input shutdown still let the rest of a body that had ended wait for
demand, so the connection stayed open, detached from its request. With
auto-read on, the request timeout still closed it, as the held body then
drained; with auto-read off, it did not.

The input shutdown now waits for demand only while the request is still
attached to the connection, and closes it otherwise. A reset is then
handled as before: the connection is closed at once, and the rest of a
body that has ended is handed on at once.

The exchange in Http1ContentDecompressorSuspensionTest now attaches its
request to the channel, as AHC does.

Claude Code on behalf of Matthias Kurz

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mkurz
mkurz requested a review from hyperxpro November 20, 2026 23:34
hyperxpro pushed a commit that referenced this pull request Nov 21, 2026
## Summary

- Since #2362, AHC decompresses HTTP/1.1 `br`, `zstd` and `snappy`
bodies with Netty's pull-based `Decompressor` API, so that they wait for
demand. Netty added that API in 4.2.17. With an older Netty,
`Http1ContentDecompressor` caught the `LinkageError` and left these
codings to `HttpContentDecompressor`, which decompresses them as soon as
they are read.
- AHC declares a Netty that has the API (4.2.20 on `main`, 4.2.21 with
#2395), so this PR drops that fallback. The README now says that AHC
needs Netty 4.2.17 or later.

## Change

- **`Http1ContentDecompressor`:** the `pullAvailable` flag and the
`LinkageError` catch go. `newPullDecompression` now only creates the
`PullDecompression`.
- **Unchanged:**
- `PullDecompression.create` still checks whether Brotli and zstd are
available, since brotli4j and zstd-jni are optional dependencies. A body
whose library is missing is still left undecoded, as
`HttpContentDecompressor` does.
- The size limit and the split into parts of at most 64 KiB are
unchanged.
- **Test:** `gzipAndDeflateDoNotNeedNettysDecompressorApi` goes,
together with the class loader that hid the API from it, as an older
Netty would.
- **Docs:** the README, the Javadoc of `ResponseBodyControl.suspend()`
and the class Javadoc of `Http1ContentDecompressor` and
`PullDecompression` no longer describe the fallback. The README now says
that AHC needs Netty 4.2.17 or later, as it uses the API Netty added
there, and that on-demand decompression applies over HTTP/1.1, and its
paragraph on truncated bodies no longer names Brotli and Snappy bodies
decompressed as soon as they are read. `PullDecompression` still notes
that Netty marks the API `@UnstableApi`, so a Netty upgrade may require
changes there.

## Notes

- With AHC's declared Netty dependency, the fallback for older Netty
versions is unnecessary. With Netty older than 4.2.17, br, zstd and
snappy responses now fail instead of being decompressed as soon as they
are read.
- This PR and #2395 rewrite the same sentences in the README, the
Javadoc of `ResponseBodyControl.suspend()` and the class Javadoc of
`Http1ContentDecompressor`. #2395 removes the exception for `zstd` at a
non-positive `io.netty.compression.defaultMaxForwardBytes`, which this
PR keeps. Whichever is merged second, I'll merge `main` into it and
resolve that.

## AI disclosure

Claude Code on behalf of Matthias Kurz. The commit includes
`Co-Authored-By` per `AGENTS.md`.

## Test plan

- [x] `./mvnw -B -ntp -Dgpg.skip=true clean verify` on JDK 11: 2,097
tests, 0 failures, 0 errors, 11 skipped; Revapi passed. No test-skipping
flags were used. That is one test fewer than on `main`: the removed
class loader test.
- [x] The `Http1ContentDecompressor*Test`,
`Http2ContentDecompressorTest`, `Http1Decompression*Test` and
`ResponseBodyControlTest` classes: 141 tests, 0 failures.
- [x] With the README change from review (`82fde4d68`), the same `clean
verify`: 2,097 tests, 0 failures, 0 errors, 22 skipped; Revapi passed.
Docker was not running, so the 11 Testcontainers proxy tests skipped
themselves; they ran and passed with `32bb77670` (2,097 tests, 11
skipped).
- [x] Merges with #2394 without conflicts.
- [x] With `-Dnetty.version=4.2.17.Final`, all client tests (`./mvnw -pl
client clean test`, JDK 11): 2,097 tests, 2 failures, both in tests that
compare AHC's output and read requests with Netty's own decoders, which
changed after 4.2.17 (`decodesLikeNettyForEveryBodyAndSplit`,
`requestsReadsLikeNettyWhenAutoReadIsOff`).

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@hyperxpro
hyperxpro merged commit 19a84ac into AsyncHttpClient:main Nov 21, 2026
13 checks passed
mkurz added a commit to mkurz/async-http-client that referenced this pull request Nov 21, 2026
Brings in AsyncHttpClient#2396, which conflicts with this branch in the README, the
Javadoc of ResponseBodyControl.suspend() and the class Javadoc of
Http1ContentDecompressor, and AsyncHttpClient#2394, which changed the same files.

The resolution keeps the Netty 4.2.17 requirement from AsyncHttpClient#2396 and the
note on Netty 4.2.21 from this branch. As this branch no longer sends
zstd to HttpContentDecompressor, it also drops the clause on Zstd
bodies decompressed as soon as they are read from the README's
paragraph on truncated bodies, which merged without a conflict.

Claude Code on behalf of Matthias Kurz

Co-Authored-By: Claude Opus 5.5 <[email protected]>
hyperxpro pushed a commit that referenced this pull request Nov 21, 2026
## Summary

- Upgrades Netty from 4.2.20.Final to
[4.2.21.Final](https://github.com/netty/netty/releases/tag/netty-4.2.21.Final).
It mainly reverts a build mistake in 4.2.20: Netty's native libraries
for x86_64 and aarch64 required glibc 2.33. AHC declares the native
epoll and io_uring jars at its Netty version, and when they do not load,
it falls back to NIO, silently unless `useNativeTransport` is set. On
4.2.20 that would happen on systems with an older glibc, such as RHEL 8
or Ubuntu 20.04. AHC 3.0.14 used Netty 4.2.18, so no AHC release has
4.2.20.
- 4.2.21 also fixes `ZstdDecoder` and `ZstdDecompressor` at a
non-positive `io.netty.compression.defaultMaxForwardBytes`
(netty/netty#18289). This PR drops AHC's workarounds for that bug, so
zstd now waits for demand at any value of the property, like br and
snappy. A zero or negative value of the property now needs Netty 4.2.21
or later.

## Change

- **Netty 4.2.21.Final.**
- **Drop the `ZstdDecoder` replacement from #2375.** At a non-positive
value of the property, `Http1ContentDecompressor` replaced the
`ZstdDecoder` that `HttpContentDecompressor` creates with one whose
buffers hold `MAX_PART_SIZE`, because Netty's decoder handed on an empty
body at 0 and failed below it.
- **Drop the zstd guard from #2362.** At such a value, zstd went to
`HttpContentDecompressor` instead of `PullDecompression`, because
Netty's `ZstdDecompressor` handed out nothing at 0 while reporting more
output. It now takes the pull path there too:
  - it waits for demand;
  - parts are at most 64 KiB;
  - the size limit applies;
- a truncated stream fails with `Truncated zstd stream` instead of
ending quietly.
- **Docs:** the README and the Javadoc of
`ResponseBodyControl.suspend()` and of `Http1ContentDecompressor` no
longer make an exception for zstd. The README and `suspend()` say
instead that a zero or negative value of the property needs Netty 4.2.21
or later. With Netty 4.2.17 to 4.2.20, zstd responses then fail at a
negative value, and at 0 they never end and keep the event loop busy, as
Netty's `ZstdDecompressor` hands out empty buffers while it reports more
output. `MAX_FORWARD_BYTES` is private, since only
`Http1ContentDecompressor` uses it now.
- **Test:** `Http1ContentDecompressorForwardThresholdTest` runs with the
property at 0 and -1 in a fresh JVM. It now also checks zstd while
suspending after every part (one part per resume, none larger than
`MAX_PART_SIZE`) and a truncated zstd body (it fails, and nothing is
left allocated).

## Notes

- With the property at its default (64 KiB), nothing changes for zstd.
- Other changes in 4.2.21 that touch AHC's paths:
- Netty now also rejects control characters such as NUL and ESC, and
DEL, in a response's reason phrase (netty/netty#18331). CR and LF were
already rejected; HTAB is still allowed.
  - `SslHandler` changes: netty/netty#18253, #18323, #18363 and #18406.
  
  AHC's tests pass without changes.

## AI disclosure

Claude Code on behalf of Matthias Kurz. The commit includes
`Co-Authored-By` per `AGENTS.md`.

## Test plan

- [x] `./mvnw -B -ntp -Dgpg.skip=true clean verify` on JDK 11: 2,098
tests, 0 failures, 0 errors, 11 skipped; Revapi passed. No test-skipping
flags were used.
- [x] With the README and Javadoc change from review (`5fd35bad8`), the
same `clean verify`: 2,098 tests, 0 failures, 0 errors, 22 skipped;
Revapi passed. Docker was not running, so the 11 Testcontainers proxy
tests skipped themselves; they ran and passed in the runs above, on the
same code.
- [x] The version change alone, without the other changes, passes the
same `clean verify`: 2,098 tests, 0 failures.
- [x] The extended test fails where it should:
- with the guard put back, at 0 and -1: "the suspended zstd body did not
wait for demand after its first part";
- without the workarounds on Netty 4.2.20: at 0, the check hangs until
its 60 s timeout; at -1, it fails with `IllegalArgumentException:
initialCapacity : -1`.
- [x] The `Http1ContentDecompressor*Test`,
`Http2ContentDecompressorTest`, `Http1Decompression*Test` and
`ResponseBodyControlTest` classes: 142 tests, 0 failures.
- [x] Merged with #2394 (`ef18b0b68`): no conflicts; the same classes,
203 tests, 0 failures; `clean verify` on JDK 11, 2,159 tests, 0
failures, 0 errors, 11 skipped; Revapi passed.

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@mkurz
mkurz deleted the fix/decompression-half-closure branch November 21, 2026 08:30
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.

2 participants