Skip to content

HTTP: Preserve encoder state after header encoding failures - #17271

Merged
chrisvest merged 1 commit into
netty:4.2from
Gimini-3:fix-http-object-encoder-state
Aug 12, 2026
Merged

chrisvest merged 1 commit into
netty:4.2from
Gimini-3:fix-http-object-encoder-state

Conversation

@Gimini-3

Copy link
Copy Markdown
Contributor

Motivation:

encodeInitHttpMessage() updated the encoder state before running header sanitization and encoding. If either step threw, the allocated buffer was released but the encoder remained in a content state even though no output had been produced. A subsequent valid response was then rejected with unexpected message type.

This can be reproduced without a fatal error by encoding a 204 No Content response backed by ReadOnlyHttpHeaders. Sanitization fails with UnsupportedOperationException, and the next response fails because the encoder is still in state 3.

#17089 made the header buffer exception-safe, but its regression test ended the channel immediately after the failure and did not exercise another response.

Modification:

  • Compute the next content state in a local variable.
  • Commit it to the encoder only after header sanitization and encoding complete successfully.
  • Add a regression test for a sanitization failure and extend the existing header encoding failure test to verify that a valid response can still be encoded afterward.

Result:

An initial-message header failure no longer leaves the encoder unusable. Successful encoding and wire output are unchanged.

Verification:

./mvnw -pl codec-http test

8,982 tests run, 0 failures, 0 errors, 4 skipped.

@Gimini-3
Gimini-3 force-pushed the fix-http-object-encoder-state branch from ccf6988 to dc02cb5 Compare August 12, 2026 12:05
@Gimini-3
Gimini-3 marked this pull request as ready for review August 12, 2026 12:07
@testlens-app

testlens-app Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

⚠️ TestLens detected flakiness ⚠️

Test Summary

Build PR / macos-aarch64-java11-boringssl build > Netty/Transport/Native/KQueue

Test Runs Flakiness
KQueueDatagramUnicastIPv6MappedTest > testSimpleSendHeapByteBuf(TestInfo) ❌ ✅ 4% 🟡

🏷️ Commit: dc02cb5
▶️ Tests: 812 executed
⚪️ Checks: 24/24 completed


Learn more about TestLens at testlens.app.

@normanmaurer normanmaurer added this to the 4.2.18.Final milestone Aug 12, 2026
@chrisvest chrisvest added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Aug 12, 2026
@chrisvest
chrisvest merged commit dd7c9dc into netty:4.2 Aug 12, 2026
38 of 39 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #17275

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #17274

@github-actions github-actions Bot removed needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Aug 12, 2026
normanmaurer pushed a commit that referenced this pull request Aug 13, 2026
…lures (#17275)

Auto-port of #17271 to 4.1
Cherry-picked commit: dd7c9dc

---
Motivation:

`encodeInitHttpMessage()` updated the encoder state before running
header sanitization and encoding. If either step threw, the allocated
buffer was released but the encoder remained in a content state even
though no output had been produced. A subsequent valid response was then
rejected with `unexpected message type`.

This can be reproduced without a fatal error by encoding a `204 No
Content` response backed by `ReadOnlyHttpHeaders`. Sanitization fails
with `UnsupportedOperationException`, and the next response fails
because the encoder is still in state 3.

#17089 made the header buffer exception-safe, but its regression test
ended the channel immediately after the failure and did not exercise
another response.

Modification:

- Compute the next content state in a local variable.
- Commit it to the encoder only after header sanitization and encoding
complete successfully.
- Add a regression test for a sanitization failure and extend the
existing header encoding failure test to verify that a valid response
can still be encoded afterward.

Result:

An initial-message header failure no longer leaves the encoder unusable.
Successful encoding and wire output are unchanged.

Verification:

`./mvnw -pl codec-http test`

8,982 tests run, 0 failures, 0 errors, 4 skipped.

Co-authored-by: Gimin Kim <[email protected]>
normanmaurer added a commit that referenced this pull request Aug 13, 2026
…lures (#17274)

Auto-port of #17271 to 5.0
Cherry-picked commit: dd7c9dc

---
Motivation:

`encodeInitHttpMessage()` updated the encoder state before running
header sanitization and encoding. If either step threw, the allocated
buffer was released but the encoder remained in a content state even
though no output had been produced. A subsequent valid response was then
rejected with `unexpected message type`.

This can be reproduced without a fatal error by encoding a `204 No
Content` response backed by `ReadOnlyHttpHeaders`. Sanitization fails
with `UnsupportedOperationException`, and the next response fails
because the encoder is still in state 3.

#17089 made the header buffer exception-safe, but its regression test
ended the channel immediately after the failure and did not exercise
another response.

Modification:

- Compute the next content state in a local variable.
- Commit it to the encoder only after header sanitization and encoding
complete successfully.
- Add a regression test for a sanitization failure and extend the
existing header encoding failure test to verify that a valid response
can still be encoded afterward.

Result:

An initial-message header failure no longer leaves the encoder unusable.
Successful encoding and wire output are unchanged.

Verification:

`./mvnw -pl codec-http test`

8,982 tests run, 0 failures, 0 errors, 4 skipped.

---------

Co-authored-by: Gimin Kim <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
@normanmaurer normanmaurer mentioned this pull request Oct 1, 2026
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…lures (netty#17275)

Auto-port of netty#17271 to 4.1
Cherry-picked commit: dd7c9dc

---
Motivation:

`encodeInitHttpMessage()` updated the encoder state before running
header sanitization and encoding. If either step threw, the allocated
buffer was released but the encoder remained in a content state even
though no output had been produced. A subsequent valid response was then
rejected with `unexpected message type`.

This can be reproduced without a fatal error by encoding a `204 No
Content` response backed by `ReadOnlyHttpHeaders`. Sanitization fails
with `UnsupportedOperationException`, and the next response fails
because the encoder is still in state 3.

netty#17089 made the header buffer exception-safe, but its regression test
ended the channel immediately after the failure and did not exercise
another response.

Modification:

- Compute the next content state in a local variable.
- Commit it to the encoder only after header sanitization and encoding
complete successfully.
- Add a regression test for a sanitization failure and extend the
existing header encoding failure test to verify that a valid response
can still be encoded afterward.

Result:

An initial-message header failure no longer leaves the encoder unusable.
Successful encoding and wire output are unchanged.

Verification:

`./mvnw -pl codec-http test`

8,982 tests run, 0 failures, 0 errors, 4 skipped.

Co-authored-by: Gimin Kim <[email protected]>
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.

4 participants