Skip to content

fix: duplicate request body when when using FULL_DUPLEX_STREAMED ext_proc body mode with retries enabled - #46095

Closed
yuehaii wants to merge 28 commits into
envoyproxy:mainfrom
yuehaii:envoy-dup-body-extproc
Closed

yuehaii wants to merge 28 commits into
envoyproxy:mainfrom
yuehaii:envoy-dup-body-extproc

Conversation

@yuehaii

@yuehaii yuehaii commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Commit Message:
when the ExtProc HTTP filter is configured with requestBodyProcessingMode: FULL_DUPLEX_STREAMED and the router has retries enabled, the upstream receives the request body twice. once from the normal ext_proc forwarding path, and once replayed from the router's retry buffer.

Additional Description:
the flag skip_body_on_next_continue_ is added to ActiveStreamDecoderFilter. when set, bufferedData() returns a null pointer which causing doData() to be skipped. the router's retry buffer buffered_request_data_ is never modified, so retry correctness is preserved.

Risk Level:
Low

Testing:

==================== Test output for //test/extensions/filters/http/ext_proc:filter_full_duplex_test:
Note: Google Test filter = HttpFilterTest.FullDuplexStreamedNoBodyDuplicationOnRetryBuffer:HttpFilterTest.FullDuplexStreamedNoBodyDuplicationOnRetryBufferNoTrailers
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from HttpFilterTest
[ RUN ] HttpFilterTest.FullDuplexStreamedNoBodyDuplicationOnRetryBuffer
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_quic_always_support_server_preferred_address to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_inject_data_with_state_update to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_return_stop_iteration to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_stream_close_optimization to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_runtime_initialized to: false
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_quic_always_support_server_preferred_address to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_inject_data_with_state_update to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_return_stop_iteration to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_stream_close_optimization to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_runtime_initialized to: false
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_quic_always_support_server_preferred_address to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_inject_data_with_state_update to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_return_stop_iteration to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_stream_close_optimization to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_runtime_initialized to: false
[ OK ] HttpFilterTest.FullDuplexStreamedNoBodyDuplicationOnRetryBuffer (79 ms)
[ RUN ] HttpFilterTest.FullDuplexStreamedNoBodyDuplicationOnRetryBufferNoTrailers
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_quic_always_support_server_preferred_address to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_inject_data_with_state_update to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_return_stop_iteration to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_stream_close_optimization to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_runtime_initialized to: false
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_quic_always_support_server_preferred_address to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_inject_data_with_state_update to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_return_stop_iteration to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_stream_close_optimization to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_runtime_initialized to: false
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_quic_always_support_server_preferred_address to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_inject_data_with_state_update to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_return_stop_iteration to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_ext_proc_stream_close_optimization to: true
[external/abseil-cpp/absl/flags/internal/flag.cc : 148] RAW: Restore saved value of envoy_reloadable_features_runtime_initialized to: false
[ OK ] HttpFilterTest.FullDuplexStreamedNoBodyDuplicationOnRetryBufferNoTrailers (60 ms)
[----------] 2 tests from HttpFilterTest (140 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (140 ms total)
[ PASSED ] 2 tests.

Release Notes:
fix request body duplicated at upstream when using FULL_DUPLEX_STREAMED ext_proc body mode with retries enabled

Fixes #45470

@gemini-code-assist

Copy link
Copy Markdown
Contributor

I apologize, but I do not have the capability to trigger a retest of the pull request. As an AI assistant, I can only provide code reviews and summaries. To trigger a retest, please check your CI/CD platform's documentation or use the specific command provided by your repository's CI system.

@yuehaii

yuehaii commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

/retest

@tyxia

tyxia commented Jul 14, 2026

Copy link
Copy Markdown
Member

@yanjunxiang-google PTAL as well when you have time.

@paul-r-gall

Copy link
Copy Markdown
Contributor

I think that this change should be self-contained within the ext_proc filter. FilterManager has already been quite polluted with ext_proc-specific code, and I don't want to propagate that behavior.

@tyxia

tyxia commented Jul 23, 2026

Copy link
Copy Markdown
Member

I agree with @paul-r-gall that we should decouple from filter manager if possible.

/wait

@yanjunxiang-google

Copy link
Copy Markdown
Contributor

Looking at the logs, the data is correctly processed by the ext_proc filter, then shipped to the router filter.

If router filter successfully sends the data to the backend, why the retry mechanism is triggered? Should it clear the buffered data after it ships data upstream, thus the retry will not happen at all?

@yuehaii

yuehaii commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

I agree with @paul-r-gall that we should decouple from filter manager if possible.

/wait

let me try to refactor code to self-contained within the ext_proc filter.

@yuehaii

yuehaii commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Looking at the logs, the data is correctly processed by the ext_proc filter, then shipped to the router filter.

If router filter successfully sends the data to the backend, why the retry mechanism is triggered? Should it clear the buffered data after it ships data upstream, thus the retry will not happen at all?

the retry buffer is kept alive through the entire response. only when the full response is received and the stream ends does the buffer get destroyed.
the upstream could still fail during mid-response (connection reset, timeout, etc.) in FULL_DUPLEX_STREAMED Mode, and the router may need to retry from scratch with the buffered body.

the duplication happens during normal successful processing. e.g. when ext_proc trailer response arrives , the doData() still been called via continueDecoding(), this replays buffered_request_data_ back through the filter chain as if new data arrived.

@yanjunxiang-google

Copy link
Copy Markdown
Contributor

Looking at the logs, the data is correctly processed by the ext_proc filter, then shipped to the router filter.
If router filter successfully sends the data to the backend, why the retry mechanism is triggered? Should it clear the buffered data after it ships data upstream, thus the retry will not happen at all?

the retry buffer is kept alive through the entire response. only when the full response is received and the stream ends does the buffer get destroyed. the upstream could still fail during mid-response (connection reset, timeout, etc.) in FULL_DUPLEX_STREAMED Mode, and the router may need to retry from scratch with the buffered body.

the duplication happens during normal successful processing. e.g. when ext_proc trailer response arrives , the doData() still been called via continueDecoding(), this replays buffered_request_data_ back through the filter chain as if new data arrived.

Thanks for working on this!

emm, could you please give a more detail root cause analysis, with code pointers, about with the route retries enabled, why the issue happens with ext_proc, and why it does not happen without ext_proc?

@yuehaii

yuehaii commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

Looking at the logs, the data is correctly processed by the ext_proc filter, then shipped to the router filter.
If router filter successfully sends the data to the backend, why the retry mechanism is triggered? Should it clear the buffered data after it ships data upstream, thus the retry will not happen at all?

the retry buffer is kept alive through the entire response. only when the full response is received and the stream ends does the buffer get destroyed. the upstream could still fail during mid-response (connection reset, timeout, etc.) in FULL_DUPLEX_STREAMED Mode, and the router may need to retry from scratch with the buffered body.
the duplication happens during normal successful processing. e.g. when ext_proc trailer response arrives , the doData() still been called via continueDecoding(), this replays buffered_request_data_ back through the filter chain as if new data arrived.

Thanks for working on this!

emm, could you please give a more detail root cause analysis, with code pointers, about with the route retries enabled, why the issue happens with ext_proc, and why it does not happen without ext_proc?

  1. this is body response callstack when FULL_DUPLEX_STREAMED enabled with ext_proc:

FilterManager::decodeData --> handle_->decodeData
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/common/http/filter_manager.cc#L770C41-L770C60

route Filter::decodeData --> acceptDataFromRouter // sent to upstream 1st time

upstream_requests_.front()->acceptDataFromRouter(copy, end_stream);

route Filter::decodeData --> addDecodedData
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/common/router/router.cc#L1168C17-L1168C31

FilterManager::addDecodedData --> commonHandleBufferData
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/common/http/filter_manager.cc#L842C12-L842C34
commonHandleBufferData --> bufferedData()->move(provided_data);
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/common/http/filter_manager.cc#L211C5-L211C41
bufferedData()->move(provided_data) write buffered_request_data_

Buffer::InstancePtr& ActiveStreamDecoderFilter::bufferedData() {

  1. this is trailer response callstack when FULL_DUPLEX_STREAMED enabled with ext_proc

ext_proc handleTrailersResponse -> continueIfNecessary -> continueProcessing -> continueDecoding
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/extensions/filters/http/ext_proc/processor_state.cc#L563C5-L563C24
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/extensions/filters/http/ext_proc/processor_state.cc#L598C5-L598C23
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/extensions/filters/http/ext_proc/processor_state.cc#L979C25-L979C41

ActiveStreamDecoderFilter::continueDecoding() --> commonContinue --> doData --> decodeData
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/common/http/filter_manager.cc#L496C54-L496C68
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/common/http/filter_manager.cc#L132C5-L132C11
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/common/http/filter_manager.cc#L426C11-L426C21

ext_proc Filter::decodeData --> onData

const auto status = onData(decoding_state_, data, end_stream);

onData --> handleDataFullDuplexStreamedMode --> handleDataStreamedModeBase
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/extensions/filters/http/ext_proc/ext_proc.cc#L1127C14-L1127C46
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/extensions/filters/http/ext_proc/ext_proc.cc#L1022C10-L1022C36
handleDataStreamedModeBase --> sendBodyChunk // sent to upstream 2st time (dup)
https://github.com/envoyproxy/envoy/blob/295ef9d9d87c645e2f1cc344c05220f6794036b7/source/extensions/filters/http/ext_proc/ext_proc.cc#L991C5-L991C18

@yuehaii
yuehaii force-pushed the envoy-dup-body-extproc branch from 9f0dbef to 7d1e17a Compare July 27, 2026 01:03
@tyxia tyxia removed their assignment Jul 27, 2026
@yanjunxiang-google

Copy link
Copy Markdown
Contributor

/wait

@yanjunxiang-google

Copy link
Copy Markdown
Contributor

/assign @stevenzzzz

@paul-r-gall

Copy link
Copy Markdown
Contributor

Sorry, I've been on vacation for a while.

Really this is a case of "filter manager buffering is completely broken when filters do really complicated stop/continue/buffer actions". I keep meaning to get around to a FilterManager-level fix here, and it just hasn't been able to get onto my list of things to do.

Is it reasonable/possible to protect this behind a feature flag? e.g. Buffer::Instance* retry_buffer = flag ? retry_buffer_.get() : decodingBuffer();.

@yuehaii

yuehaii commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Is it reasonable/possible to protect this behind a feature flag? e.g. Buffer::Instance* retry_buffer = flag ? retry_buffer_.get() : decodingBuffer();.

I think a runtime feature flag can gate whether the router uses its own private retry buffer or the shared filter-chain buffer. maybe defaulting to the original filter-chain behavior for safety.

…_retry_buffer to control whether the router uses its own private retry buffer or the shared filter-chain buffer

Signed-off-by: hai.yue <[email protected]>
@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: #46095 was synchronize by yuehaii.

see: more, trace.

"envoy.reloadable_features.router_use_private_retry_buffer") &&
retry_buffer_ && retry_buffer_->length() > 0) {
Buffer::OwnedImpl copy(*retry_buffer_);
callbacks_->addDecodedData(copy, false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could you please explain why here is false, and above is true?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, since there's no buffered request data, I suspect that it doesn't matter, but confirmation from author of code would be great.

@yuehaii yuehaii Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think 'true' means the data is arriving as a stream, it need to be accumulated. in decodeData() func, the downstream is still sending, more chunks may follow.

and the 'false' mean it is the final buffer, it is time to inject the entire accumulated body back into buffer. in setupRedirect() func, the full body already in retry_buffer_.

please let me know if I mis-understand the logic.

Comment thread source/common/router/router.cc Resolved
Comment thread source/common/router/router.cc Resolved
Comment thread test/common/router/router_test.cc Resolved
	1. remove incorrect runtime guard envoy_reloadable_features_report_load_when_rq_active_is_non_zero
	2. account streams in retry buffer
	3. reset retry buffer when buffer overflow
	4. rollback private retry buffer in old test cases add release note

Signed-off-by: hai.yue <[email protected]>
@yuehaii

yuehaii commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

@paul-r-gall and @yanjunxiang-google , good day. I've adjusted the code accordingly. could you please take another look at the PR when you get a chance? thanks for advise.

@KBaichoo

Copy link
Copy Markdown
Contributor

PTAL when you have time @paul-r-gall @yanjunxiang-google

@yanjunxiang-google

yanjunxiang-google commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

@wbpcode @botengyao @yanavlasov
This PR have changes in router filter. PTAL!

@wbpcode

wbpcode commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Seems another problem because the across-filters shared data buffer + inject* API. Because inject* is used to drive filters after ext proc, the ext proc self still in stop status of decodedata. Once contineDecoding is called, the data in shared buffer will be treated as fresh unprocessed data.

And i think retry isn't the only buggy scenario. For any filter after extproc, if the filter stop chain and buffer data, then the continue decoding/conitnueEnconding will replay the data in the shared buffer which is unexpected.

I guess the per-filter-buffer would be the final solution. And I drafted one recently, and it's much simpler than i initially thought.

#48556 is a first pre-PR of per-filter-buffer support. And it also addressed other related issue of extproc and inject*.

I will share the left part tomorrow morning, after i get up.

/wait

@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot added the stale stalebot believes this issue/PR has not been touched recently label Nov 22, 2026
@wbpcode wbpcode reopened this Nov 24, 2026
@wbpcode wbpcode closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale stalebot believes this issue/PR has not been touched recently waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Body duplication when using the ExtProc filter and enabling retry policy

8 participants