Skip to content

http: add AsyncStream buffer size limit for upstream retry - #19611

Merged
mattklein123 merged 9 commits into
envoyproxy:mainfrom
Shikugawa:buffer-limit
Feb 23, 2022
Merged

mattklein123 merged 9 commits into
envoyproxy:mainfrom
Shikugawa:buffer-limit

Conversation

@Shikugawa

@Shikugawa Shikugawa commented Jan 19, 2022 •

Copy link
Copy Markdown
Member

Signed-off-by: Shikugawa [email protected]

Commit Message: http: add AsyncStream buffer size limit for upstream retry
Additional Description: This PR fixes the potential memory leak on long-lived HTTP streams, such as gRPC client streaming.
Risk Level: Low
Testing: Unit
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional API Considerations:]
Fixes #19699

@Shikugawa
Shikugawa requested a review from alyssawilk January 19, 2022 14:29
@Shikugawa Shikugawa changed the title http: add AsyncClient buffer size limit for upstream retry http: add AsyncStream buffer size limit for upstream retry Jan 19, 2022

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

Thanks for working on this. Small comments to get started.

/wait

Comment thread source/common/http/async_client_impl.cc Outdated
// buffering that we allow here.
if (buffered_body_ != nullptr) {
if (buffered_body_ != nullptr &&
buffered_body_->length() + data.length() <= buffered_body_size_limit_) {

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.

In the case where we exceed the limit, shouldn't we fail in some way? This is going to lead to corruption and hard to debug issues?

@Shikugawa Shikugawa Jan 26, 2022 •

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.

If an attempt is made to perform a retry when the buffer is exceeded, a failure flag is added as follows:

void AsyncStreamImpl::sendData(Buffer::Instance& data, bool) {
...
  if (buffer != nullptr) {
    if (isExceeded(buffer.size() + data.size(), BUFFER_LIMIT)) {
      should_fail_retry_ = true;
    } else {
      buffer.add(...)
    }
  }
...
}

If should_fail_retry_ is set to true, it tries to delete the stream before scheduling the retry.
This method requires some interface changes, but it avoids implementations that delete the stream when the buffer is exceeded even though retries will never occur in the future. What do you think?

Another idea would be to delete old messages when the buffer exceeds a certain size, like a ring buffer.

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.

For now, the current changes will fix the memory leak bug. Anyway, this change should be upstreamed immediately, and the follow-up changes can be implemented later (before the next release). In the meantime, we can deal with it by simply dumping the logs.

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.

Please add relevant TODOs, etc. You might also consider a power of 2 backoff warning log of some type until there is a better fix here. People should know this is happening.

@htuch

htuch commented Jan 26, 2022

Copy link
Copy Markdown
Member

@Shikugawa do you have an ETA for this? I'd like to share with envoy-announce@ when this lands so we can advise folks to bump.

@Shikugawa

Copy link
Copy Markdown
Member Author

@Shikugawa do you have an ETA for this? I'd like to share with envoy-announce@ when this lands so we can advise folks to bump.

Not yet. But I keep in mind it. I will notify you when this change is landed.

@Shikugawa

Copy link
Copy Markdown
Member Author

@mattklein123 Could you take a look?

Signed-off-by: Shikugawa <[email protected]>
Signed-off-by: Shikugawa <[email protected]>
@mattklein123

Copy link
Copy Markdown
Member

Please merge main to fix CI.

/wait

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

/wait

Comment thread source/common/http/async_client_impl.h Resolved
Comment thread source/common/http/async_client_impl.cc Resolved
Signed-off-by: Shikugawa <[email protected]>

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

/wait

Comment thread source/common/http/async_client_impl.cc Outdated
Comment on lines +162 to +163
// TODO(shikugawa): In addition to logging, the process of preventing too much data by using
// ring buffers, etc.

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.

I'm not sure what this means, can you clarify? Thank you.

Comment thread source/common/http/async_client_impl.h Outdated
namespace {
// Limit the size of buffer for data used for retries.
// The default is set to 64KB. Since the existence of a buffer for
// retries is non-trivial to the user, this value is not varied.

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.

Sorry what do you mean non-trivial to the user?

@Shikugawa Shikugawa Feb 18, 2022 •

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.

This means that the user does not need to be aware of the existence of the retry buffer at this time because the buffer size is fixed length, but it is difficult to understand, so it will be removed.

Signed-off-by: Shikugawa <[email protected]>
@Shikugawa

Copy link
Copy Markdown
Member Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #19611 (comment) was created by @Shikugawa.

see: more, trace.

@mattklein123

Copy link
Copy Markdown
Member

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #19611 (comment) was created by @mattklein123.

see: more, trace.

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

Some comment clarifications otherwise LGTM, thanks.

/wait

Comment thread source/common/http/async_client_impl.h Resolved
Comment thread source/common/http/async_client_impl.cc Resolved
Signed-off-by: Shikugawa <[email protected]>
@mattklein123
mattklein123 merged commit 7900682 into envoyproxy:main Feb 23, 2022
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.

memory leak in gRPC access logs

3 participants