Skip to content

http: forward added trailers once from the filter holding them - #49241

Merged
wbpcode merged 4 commits into
envoyproxy:mainfrom
wbpcode:dev-filter-manager-held-trailers
Nov 14, 2026
Merged

wbpcode merged 4 commits into
envoyproxy:mainfrom
wbpcode:dev-filter-manager-held-trailers

Conversation

@wbpcode

@wbpcode wbpcode commented Nov 13, 2026

Copy link
Copy Markdown
Member

Commit Message: http: forward added trailers once from the filter holding them

Fixes the filter manager's handling of trailers added by a filter (addDecodedTrailers() /
addEncodedTrailers()):

  • Response: when a filter after the adder stops iteration on the data (e.g. one that buffers until
    the end of the stream), encodeData() returned before dispatching the added trailers, so that
    filter and every filter after it never saw them and the response could be held forever.
  • Request: when a filter stopped on headers continues on the data, its commonContinue()
    dispatched the added trailers, and decodeData() dispatched them again from the adder after the
    loop, so later filters (including the router) decoded the trailers twice.

The filter manager now records the single filter holding the trailers of each direction: the
filter that added them, then each filter they reach, until they leave the chain. Trailers are
forwarded once from the holder, and only while it iterates (at the end of a data pass, when a
filter stopping on the data ends the pass, and when the holder resumes). Trailers parked at a
filter that stopped all iteration are delivered to that filter once it resumes.

Additional Description: Split out of #48660, where the same issues were found; the fix does not
depend on per-filter buffers.
Risk Level: Medium
Testing: Unit tests + integration
Docs Changes: N/A
Release Notes: Added.
Runtime guard: envoy.reloadable_features.filter_manager_forward_held_trailers

@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/runtime-guard-changes: FYI only for changes made to (source/common/runtime/runtime_features.cc).

🐱

Caused by: #49241 was opened by wbpcode.

see: more, trace.

@wbpcode

wbpcode commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

/retest

2 similar comments
@wbpcode

wbpcode commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

/retest

@wbpcode

wbpcode commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

/retest

@agrawroh agrawroh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🚫 Suggested Fixes. One holder per direction is the right shape, and the new tests pin most of it, so only the new guard-off test blocks.

  • ⚠️ The new guard-off request test trips the decoder_filter_chain_complete_ assert in every build with assertions on, so asan, msan, tsan and two compile-time-option jobs abort filter_manager_test before its 116 invariant cases run.
  • 💡 The release note covers only added trailers, but the guard also changes codec trailers a filter holds, and "never forwarded past" is off since the codec did get added trailers once a later stopped filter resumed.
  • 💡 Each TrailersState pads a pointer and a bool to 16 bytes, so every filter manager grows 32 bytes.
  • 💡 The new integration case runs on the HTTP/2 only FilterIntegrationTest, so it never runs on HTTP/1 and its HTTP1 check is always true.
  • 💡 The "never sent" comment is off, since at main the codec gets these trailers once from filter 1's resume and only filter 1 misses them.
  • 💡 No guard-off test runs the legacy response dispatch at the end of encodeData(), since the only response guard-off case adds its trailers in a pass that stops early and resumes with them already present.
  • 💡 The invariant suite stays green with the new encodeData() stop-path forward deleted, since resume() restarts the stopper and its own resume sends the trailers.
  • 💡 concat(), bufferRequestBodyAtTwoFilters() and bufferResponseBodyAtTwoFilters() have no callers.

Comment thread test/common/http/filter_manager_test.cc Outdated
Comment on lines +2216 to +2222
filter_manager_->decodeHeaders(*request_headers_, false);
decodeData("aaa", true);

EXPECT_EQ((Chunks{{"aaa", false}}), records_[1].decoded_data);
// Sent once by the resumed filter 0 and again from filter 0 after the data loop.
EXPECT_EQ(2, records_[1].decoded_trailers);
EXPECT_EQ(2, records_[2].decoded_trailers);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

⚠️ The new guard-off request test trips the decoder_filter_chain_complete_ assert in every build with assertions on, so asan, msan, tsan and two compile-time-option jobs abort filter_manager_test before its 116 invariant cases run. Could filter 1 stop on the trailers, so the first dispatch never completes the chain and the double delivery still shows?

Suggested change
filter_manager_->decodeHeaders(*request_headers_, false);
decodeData("aaa", true);
EXPECT_EQ((Chunks{{"aaa", false}}), records_[1].decoded_data);
// Sent once by the resumed filter 0 and again from filter 0 after the data loop.
EXPECT_EQ(2, records_[1].decoded_trailers);
EXPECT_EQ(2, records_[2].decoded_trailers);
// Filter 1 holds the trailers, so the first dispatch does not complete the chain and the second
// one does not trip the decoder-complete assertion in debug builds.
records_[1].decode_trailers = [] { return FilterTrailersStatus::StopIteration; };
filter_manager_->decodeHeaders(*request_headers_, false);
decodeData("aaa", true);
EXPECT_EQ((Chunks{{"aaa", false}}), records_[1].decoded_data);
// Sent once by the resumed filter 0 and again from filter 0 after the data loop.
EXPECT_EQ(2, records_[1].decoded_trailers);
EXPECT_EQ(0, records_[2].decoded_trailers);

@@ -0,0 +1,7 @@
Fixed filter manager handling of trailers added by a filter (``addDecodedTrailers()`` /

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 The release note covers only added trailers, but the guard also changes codec trailers a filter holds, and "never forwarded past" is off since the codec did get added trailers once a later stopped filter resumed. Could the note, commit message and guard comment say all trailers now move once from their holder, skipping no filter?

// are parked before filter_, which stopped all iteration, and continue from it.
bool processed_{};
};
TrailersState<ActiveStreamDecoderFilter> request_trailers_state_;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 Each TrailersState pads a pointer and a bool to 16 bytes, so every filter manager grows 32 bytes. Could the flags sit beside forward_held_trailers_? That printed 1176 bytes, not 1192, with the filter manager and HCM tests green, and processed_ matched the holder's end_stream_ across those and the integration tests, so it may need no field.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

rather than the 32 bytes, I inclined to keep the code clean and easy to read.

}
}

TEST_P(FilterIntegrationTest, AddedResponseTrailersReachLaterBufferingFilter) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 The new integration case runs on the HTTP/2 only FilterIntegrationTest, so it never runs on HTTP/1 and its HTTP1 check is always true. The same body passes all 46 cases on the multi-protocol suite, and with the guard off its HTTP/1 cases hang too.

Suggested change
TEST_P(FilterIntegrationTest, AddedResponseTrailersReachLaterBufferingFilter) {
TEST_P(MultiProtocolFilterIntegrationTest, AddedResponseTrailersReachLaterBufferingFilter) {

Comment thread test/common/http/filter_manager_test.cc Outdated
encodeHeaders(false);
encodeData("aaa", true);

// Previously the trailers were never sent.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 The "never sent" comment is off, since at main the codec gets these trailers once from filter 1's resume and only filter 1 misses them.

Suggested change
// Previously the trailers were never sent.
// Without the guard, filter 1 never receives these trailers.

if (forward_held_trailers_) {
// Trailers added in this pass follow the data from the filter holding them, if it iterates.
forwardResponseTrailers();
} else if (trailers_added_entry != encoder_filters_.end()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 No guard-off test runs the legacy response dispatch at the end of encodeData(), since the only response guard-off case adds its trailers in a pass that stops early and resumes with them already present. Emptying this branch still passed the filter manager and HCM suites. Could a guard-off case with no stopping filter cover it?

Comment thread test/common/http/filter_manager_test.cc Outdated
Comment on lines +2011 to +2013
if (c.stop_kind != TrailersStopKind::None && c.stop_kind != TrailersStopKind::ContinueOnData) {
resume(filterAt(c.stopper));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 The invariant suite stays green with the new encodeData() stop-path forward deleted, since resume() restarts the stopper and its own resume sends the trailers. Could a buffering stopper be left to resume on its own? With this change that mutant fails three response cases.

Suggested change
if (c.stop_kind != TrailersStopKind::None && c.stop_kind != TrailersStopKind::ContinueOnData) {
resume(filterAt(c.stopper));
}
if (c.stop_kind == TrailersStopKind::BufferData) {
// A filter that buffers until the end of the stream resumes only once the trailers reach it.
const size_t i = filterAt(c.stopper);
EXPECT_TRUE(request()
? dynamic_cast<ActiveStreamDecoderFilter*>(filters_[i]->decoder_callbacks_)
->canIterate()
: dynamic_cast<ActiveStreamEncoderFilter*>(filters_[i]->encoder_callbacks_)
->canIterate());
} else if (c.stop_kind != TrailersStopKind::None &&
c.stop_kind != TrailersStopKind::ContinueOnData) {
resume(filterAt(c.stopper));
}

Comment thread test/common/http/filter_manager_test.cc Outdated
TestResponseTrailerMapImpl{{"trailer", "value"}}));
}

static std::string concat(const Chunks& chunks) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 concat(), bufferRequestBodyAtTwoFilters() and bufferResponseBodyAtTwoFilters() have no callers. Could we drop them?

@agrawroh agrawroh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, Thanks! :)

@agrawroh agrawroh self-assigned this Nov 14, 2026
@wbpcode
wbpcode enabled auto-merge (squash) November 14, 2026 08:42
@wbpcode

wbpcode commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

/retest

2 similar comments
@wbpcode

wbpcode commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

/retest

@wbpcode

wbpcode commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

/retest

@wbpcode
wbpcode merged commit e989d8c into envoyproxy:main Nov 14, 2026
25 checks passed
paul-r-gall added a commit that referenced this pull request Nov 18, 2026
…ilers (#49151)

**Commit Message:** http: fix body drop when StopAllIteration filter
re-buffers until trailers

**Additional Description:**

#49241 landed the held-trailers forwarding fix while this PR was in
review, so merging `main` narrows this PR to the remaining
nested-continuation bug plus the extra unit/integration tests and
`addEncodedTrailers()` doc note.

When a filter paused in `decodeHeaders(false)` or `encodeHeaders(false)`
with `StopAllIterationAndBuffer` / `StopAllIterationAndWatermark`
resumes (`commonContinue()`), `iterate_from_current_filter_` stays
`true` for the whole outer `commonContinue()` call. If that filter then
re-buffers the body in `decodeData(false)` / `encodeData(false)`
(`StopIterationAndBuffer`) and returns `Continue` from
`decodeTrailers()` / `encodeTrailers()`,
`commonHandleAfterTrailersCallback()` runs a nested `commonContinue()`
*after* `end_stream_` has been marked `true` on the filter; because
`iterate_from_current_filter_` is still `true`, the nested
`doData(false)` starts at the current filter, hits `if
((*entry)->end_stream_) return;`, and drops the buffered body before
forwarding the trailers. Clearing `iterate_from_current_filter_` in
`commonHandleAfterTrailersCallback()` (under
`envoy.reloadable_features.filter_manager_forward_held_trailers`) starts
the nested pass at the next filter as intended.

**Risk Level:** Low (runtime guarded behind
`envoy.reloadable_features.filter_manager_forward_held_trailers`)
**Testing:** Unit tests in `test/common/http/filter_manager_test.cc` and
integration tests in `test/integration/filter_integration_test.cc`
**Docs Changes:** `addEncodedTrailers()` doc comment in
`envoy/http/filter.h`
**Release Notes:** Added
**Runtime guard:**
`envoy.reloadable_features.filter_manager_forward_held_trailers`
**AI Usage:** Generated by Gemini; reviewed, improved by paul-r-gall.

---------

Signed-off-by: Paul Ogilby <[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.

2 participants