Skip to content

http: track observed end stream per filter position in the filter manager - #49286

Merged
wbpcode merged 7 commits into
envoyproxy:mainfrom
wbpcode:dev-filter-manager-positional-end-stream
Nov 22, 2026
Merged

wbpcode merged 7 commits into
envoyproxy:mainfrom
wbpcode:dev-filter-manager-positional-end-stream

Conversation

@wbpcode

@wbpcode wbpcode commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Commit Message: http: track observed end stream per filter position in the filter manager
Additional Description:
Split out of #48660.

The end_stream computed when a stopped filter resumes (commonContinue()) previously read the stream-level observed_[de|en]code_end_stream_ flag, which records that the codec has seen the end of the stream. For filters that hold data and re-inject it (ext_proc style), that flag is positionally wrong: the end of the stream may not have reached the resuming filter yet. #43175 fixed the two-filter instance of this (#41654) by letting inject[De|En]codedDataToFilterChain() rewrite the stream-level flag under the runtime guard. That mechanism is last-writer-wins on a single shared flag, which is only correct while there is one injecting filter upstream of any resuming filter: with three or more stopping/injecting filters, an upstream filter's final inject(end_stream=true) can land between a mid-chain filter's injections and a downstream filter's resume, making the downstream filter release buffered data with a premature end_stream=true (truncating the remaining body).

Here is the A → B → C timeline:

Step Event Stream-level flag Actual state at each filter
1 The client's last chunk arrives with end_stream=true true (set by the codec) A has the whole stream; nothing final has reached B or C
2 A injects (a1, false) false B holds a1 and stops; A still owes a2
3 B's side stream answers a1 quickly, so B injects (b1, false) false C receives b1, stops, and buffers it
4 A's side stream answers, so A injects (a2, true) true (written by A) The frame stops at B, which holds a2 while it waits on its side stream; C never sees this frame
5 C's async work finishes, so it calls continueDecoding() and then doData(observedEndStream() && ...) reads true But B still owes C the processed a2 (b2)

This PR replaces the mechanism with a per-filter observed_end_stream_ marker: frame deliveries and parked frames latch it at the position they actually reached, and the inject APIs author it at the injecting filter's own position only. Resumption reads the resuming filter's own marker, so the computation is correct for any number of stopping/injecting filters. The stream-level flags are no longer modified by data injection.

Risk Level: Medium
Testing: unit tests (filter_manager_test), ext_proc integration test
Docs Changes: N/A
Release Notes: added
Runtime guard: envoy.reloadable_features.filter_manager_per_filter_observed_end_stream

@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: #49286 was opened by wbpcode.

see: more, trace.

@wbpcode

wbpcode commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

/retest

@paul-r-gall paul-r-gall self-assigned this Nov 18, 2026
paul-r-gall
paul-r-gall previously approved these changes Nov 18, 2026
@agrawroh

Copy link
Copy Markdown
Member

Needs a main merge.

/wait

Signed-off-by: wbpcode <[email protected]>

# Conflicts:
#	test/common/http/filter_manager_test.cc

@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.

🔄 Follow-ups. Defaulting the guard off keeps the shared-buffer duplicate off the default path, so nothing here blocks once CI passes.

  • ❓ With the guard on, should recreation also require the end of stream at this filter?
  • 💡 Could this carry the TODO to flip it that false guards here take, and could the note say the guard is incomplete until per-filter buffering lands?
  • 💡 Every stream now looks up the old inject guard, which main read only when a filter injected.
  • 💡 No response-side test resumes a filter before an earlier one injects, and the two-filter ext_proc cases never run with the guard on.
  • 💡 The fixture gives only the first filter's config its 5 s message timeout, so the other two side streams here wait across several round trips at the 200 ms default, which the fixture says sanitizer runs can exceed.
  • 💡 With the guard on, on a CONNECT tunnel with half close, a complete response after the client's end of stream now closes the stream and drops request data a filter is still injecting.
  • 💡 Every current read of the marker also requires no trailers, so the trailers case this comment gives changes nothing today.
  • 💡 The description still names the old guard and says the old flag is right while one injecting filter sits upstream of the resuming one, while the guard-off test shows it ending the stream with one injector that hasn't injected yet. Since it becomes the commit message, could it name the new guard, its default and that order?

Comment thread source/common/http/filter_manager.cc Outdated
// there was no body from the HCM's point of view. This deliberately checks the stream-level
// flag rather than this filter's positional marker: recreation requires the whole request to
// have been received, wherever it is currently buffered.
if (!parent_.decoderObservedEndStream()) {

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.

❓ With the guard on, should recreation also require the end of stream at this filter? This flag no longer drops while an earlier filter is still injecting, and custom_response and dynamic modules gate recreation only on it, so the new stream, which replays only the stream buffer, would lose the bytes that filter still holds.

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.

The whole injection APIs are tricky. Before the #43175, the injection won't update the stream level end-of-stream flag, the custom_response, dynamic module or any other filter who will recreate the stream, may lost the bytes silently.

After the #43175, the injection will overwrite the stream level end-of-stream flag, it luckily also fixed this scenario. Similar to the half-close-abortion case.

But the #43175 also introduced a side effect, it will pollute the stream level end-of-stream flag.

At the latest commit, we added another new decoder_injection_pending_ to record the pending injection, to revert the main's behaviour first.

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.

This now holds when the client's end arrives first, and the new comment on the pending-injection TODO covers the order where it arrives after the last injection.

// reached the resuming filter, instead of from the stream-level flag. When enabled,
// envoy.reloadable_features.ext_proc_inject_data_with_state_update is ignored. Per-filter body
// buffering will be added behind this flag before it is enabled by default.
FALSE_RUNTIME_GUARD(envoy_reloadable_features_filter_manager_per_filter_buffer);

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.

💡 Could this carry the TODO to flip it that false guards here take, and could the note say the guard is incomplete until per-filter buffering lands? Enabling it alone still hands a retrying router the body twice in the stop-all order.

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.

I could add a TODO but no Enabling it alone still hands a retrying router the body twice in the stop-all order because as I explained in the per-filter-buffer PR, that scenario is broken anyway, no matcher the flag's value.

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 TODO and the incomplete note are in, thanks.

proxy_100_continue_(proxy_100_continue),
per_filter_buffer_(Runtime::runtimeFeatureEnabled(
"envoy.reloadable_features.filter_manager_per_filter_buffer")),
inject_data_with_state_update_(Runtime::runtimeFeatureEnabled(

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.

💡 Every stream now looks up the old inject guard, which main read only when a filter injected. Could we read it at the two inject sites again, after the new guard check?

@wbpcode wbpcode Nov 20, 2026 •

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.

We will create the filter manager for every request, and it's more robust to keep the flag's value be same in the same request. Or we may find the first injection call set the stream level end_stream flag to false, then the runtime flag is flipped to false at runtime, and the stream level end_stream flag never be set to true finally. The previous code is fragile.

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.

Agreed, latching it per stream keeps one stream's handling consistent if the flag flips.

std::vector<bool> sent_headers_;
};

TEST_F(FilterManagerObservedEndStreamTest, ResumedFilterDoesNotEndStreamBeforeInjectedBody) {

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 response-side test resumes a filter before an earlier one injects, and the two-filter ext_proc cases never run with the guard on. Could we add an encoder twin of this case and run one two-filter case with the guard on?

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.

Both cases are in, and the encoder one fails with the guard off.

grpc_upstream_count_ = 3;
config_helper_.addConfigModifier([this](envoy::config::bootstrap::v3::Bootstrap&) {
// Filter-2: only processes the request headers.
proto_config_2_.mutable_processing_mode()->Clear();

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 fixture gives only the first filter's config its 5 s message timeout, so the other two side streams here wait across several round trips at the 200 ms default, which the fixture says sanitizer runs can exceed. Could both get the same timeout?

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 helper's default now covers every added filter, thanks.

Fixed a bug where a filter resuming iteration could end the stream early, dropping the remaining
body, when another filter earlier in the chain was still holding data that it later injected (for
example, three ext_proc filters processing the body in ``STREAMED`` mode). The filter manager can
now track whether the end of the stream has reached each filter. This is disabled by default and

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.

💡 With the guard on, on a CONNECT tunnel with half close, a complete response after the client's end of stream now closes the stream and drops request data a filter is still injecting. Could the note say so?

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.

Will revert the main's behaviour.

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 close now waits for the injection to end, so the note needs nothing more.

Comment thread source/common/http/filter_manager.cc Outdated
state_.filter_added_data_in_data_callback_ = false;
const bool data_nonempty_before_callback = data.length() > 0;
(*entry)->end_stream_ = end_stream && !filter_manager_callbacks_.requestTrailers();
// |= as the marker latches: when trailers are parked at this filter, its own buffered data

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.

💡 Every current read of the marker also requires no trailers, so the trailers case this comment gives changes nothing today. Could the comment call the latch defensive, and keep the trailer-path writes in case recreation starts reading the marker?

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 comments now say so, thanks.

@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.

🔄 Follow-ups. The guard stays off by default, so nothing here blocks.

  • 💡 With the guard on, nothing clears this when the client's last data or trailers arrive after the last injection, so recreation stays refused where main allows it and internal redirects aren't followed.
  • 💡 The guard's comment now says it is incomplete until per-filter buffering lands, while this note tells operators how to enable it as the fix.
  • 💡 This turns the inject guard off too, so no test pins the default path, where recreation is refused while a filter is still injecting.
  • 💡 Could dumpState() print this, since with the guard on it decides close and recreation while the stream flag it already dumps reads as ended?
  • 💡 The description still names the old guard and says injection no longer touches the stream-level flags, which holds only with the guard on. Since it becomes the commit message, could it name the new guard, its default and the pending-injection check?

* without it, and the request ends with the trailers passing the filter instead (e.g.
* ext_proc in STREAMED mode forwards held trailers once the last chunk is injected). This
* stays true for the rest of the stream.
* TODO(wbpcode): handle these cases. They are not introduced by

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.

💡 With the guard on, nothing clears this when the client's last data or trailers arrive after the last injection, so recreation stays refused where main allows it and internal redirects aren't followed. Could maybeEndDecode() clear it once the terminal filter decodes the end of stream, with a guard-on test and these cases updated?

body, when another filter earlier in the chain was still holding data that it later injected (for
example, three ext_proc filters processing the body in ``STREAMED`` mode). The filter manager can
now track whether the end of the stream has reached each filter. This is disabled by default and
can be enabled by setting the runtime guard

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 guard's comment now says it is incomplete until per-filter buffering lands, while this note tells operators how to enable it as the fix. Could the note say it is incomplete too?


TEST_F(FilterManagerObservedEndStreamTest, InjectionWithoutGuardLeavesRecreateToStreamFlag) {
scoped_runtime_.mergeValues(
{{"envoy.reloadable_features.ext_proc_inject_data_with_state_update", "false"}});

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.

💡 This turns the inject guard off too, so no test pins the default path, where recreation is refused while a filter is still injecting. Could a case keep that guard at its default and expect the refusal?

bool observed_decode_end_stream_{};
// Set to the inverse of end_stream by the last request data injection. Only maintained with
// per-filter buffer, where injection leaves observed_decode_end_stream_ untouched.
bool decoder_injection_pending_{};

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.

💡 Could dumpState() print this, since with the guard on it decides close and recreation while the stream flag it already dumps reads as ended?

@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!

@wbpcode

wbpcode commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

merge first. The flag is off by default anyway.

@wbpcode
wbpcode merged commit ff44933 into envoyproxy:main Nov 22, 2026
26 checks passed
@wbpcode
wbpcode deleted the dev-filter-manager-positional-end-stream branch November 22, 2026 03:23
phlax added a commit to phlax/envoy that referenced this pull request Nov 25, 2026
…age timeout

envoyproxy#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of envoyproxy#49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 25, 2026
…age timeout

envoyproxy#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of envoyproxy#49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 25, 2026
…age timeout

envoyproxy#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of envoyproxy#49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 25, 2026
…age timeout

envoyproxy#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of envoyproxy#49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 25, 2026
…age timeout

envoyproxy#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of envoyproxy#49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 25, 2026
…age timeout

envoyproxy#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of envoyproxy#49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 25, 2026
…age timeout

envoyproxy#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of envoyproxy#49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 25, 2026
…age timeout

envoyproxy#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of envoyproxy#49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit that referenced this pull request Nov 25, 2026
…age timeout

#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of #49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit that referenced this pull request Nov 25, 2026
…age timeout

#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of #49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit that referenced this pull request Nov 25, 2026
…age timeout

#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of #49286, without the rest of that change.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit that referenced this pull request Nov 25, 2026
…age timeout

#48964 gave the ext_proc integration fixture a 5s default message timeout,
as the filter default of 200ms is shorter than a round trip through the
fake ext_proc server can take under the sanitizers.

The default is set in `initializeConfig()`, which the tests with two
ext_proc filters do not go through for their filter configs: they pass
each one to `addDownstreamExtProcFilter()`, so both filters kept the 200ms
default.

Apply the same default there. This is the fixture change that landed on
main as part of #49286, without the rest of that change.

Signed-off-by: Ryan Northey <[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