[tcp] Add some safety conditions for memory allocation (#34003)

It looks like we're seeing a rare crash in the TCP read code, around
buffering for the next read. Unclear as yet whether this is due to
`memory_pressure_controller` rollout or not - see b/294609692,
b/294854129.

This change just adds some safety checks to try and ensure we never get
into the bad state - but it's unclear to me as yet how we do reach that
state.
pull/34012/head
Craig Tiller 2 years ago committed by GitHub
parent 18be986e3b
commit 65e8e04b6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/core/lib/iomgr/tcp_posix.cc

@ -1057,7 +1057,7 @@ static void maybe_make_read_slices(grpc_tcp* tcp)
static const int kBigAlloc = 64 * 1024;
static const int kSmallAlloc = 8 * 1024;
if (tcp->incoming_buffer->length <
static_cast<size_t>(tcp->min_progress_size)) {
std::max<size_t>(tcp->min_progress_size, 1)) {
size_t allocate_length = tcp->min_progress_size;
const size_t target_length = static_cast<size_t>(tcp->target_length);
// If memory pressure is low and we think there will be more than
@ -1067,8 +1067,8 @@ static void maybe_make_read_slices(grpc_tcp* tcp)
if (low_memory_pressure && target_length > allocate_length) {
allocate_length = target_length;
}
int extra_wanted =
allocate_length - static_cast<int>(tcp->incoming_buffer->length);
int extra_wanted = std::max<int>(
1, allocate_length - static_cast<int>(tcp->incoming_buffer->length));
if (extra_wanted >=
(low_memory_pressure ? kSmallAlloc * 3 / 2 : kBigAlloc)) {
while (extra_wanted > 0) {

Loading…
Cancel
Save