Skip to content

Commit ed8899e

Browse files
[Backport][v1.83.x] Fixing bug in GoAway and gRPC Message Compression (#43270)
Backport of #43227 to v1.83.x. --- [export] [Bug] Fixing bug in GoAway and gRPC Message Compression ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/964572909](http://cl/964572909) --------- Co-authored-by: Tanvi Jagtap <[email protected]>
1 parent 5f8d6df commit ed8899e

14 files changed

Lines changed: 421 additions & 278 deletions

File tree

‎src/core/ext/filters/http/message_compress/compression_filter.cc‎

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <grpc/support/port_platform.h>
2222
#include <inttypes.h>
2323

24+
#include <cstdint>
2425
#include <functional>
2526
#include <memory>
2627
#include <optional>
@@ -44,6 +45,7 @@
4445
#include "src/core/lib/surface/call.h"
4546
#include "src/core/lib/transport/transport.h"
4647
#include "src/core/telemetry/call_tracer.h"
48+
#include "src/core/transport/message_size_service_config.h"
4749
#include "src/core/util/grpc_check.h"
4850
#include "src/core/util/latent_see.h"
4951
#include "absl/status/status.h"
@@ -75,8 +77,6 @@ ServerCompressionFilter::Create(const ChannelArgs& args, ChannelFilter::Args) {
7577

7678
ChannelCompression::ChannelCompression(const ChannelArgs& args)
7779
: max_recv_size_(GetMaxRecvSizeFromChannelArgs(args)),
78-
message_size_service_config_parser_index_(
79-
MessageSizeParser::ParserIndex()),
8080
default_compression_algorithm_(
8181
DefaultCompressionAlgorithmFromChannelArgs(args).value_or(
8282
GRPC_COMPRESS_NONE)),
@@ -118,26 +118,25 @@ MessageHandle ChannelCompression::CompressMessage(
118118
return message;
119119
}
120120
// Try to compress the payload.
121-
SliceBuffer tmp;
122-
SliceBuffer* payload = message->payload();
123-
bool did_compress = grpc_msg_compress(algorithm, payload->c_slice_buffer(),
124-
tmp.c_slice_buffer());
121+
std::optional<SliceBuffer> compressed =
122+
MessageCompress(algorithm, *message->payload());
123+
125124
// If we achieved compression send it as compressed, otherwise send it as (to
126125
// avoid spending cycles on the receiver decompressing).
127-
if (did_compress) {
126+
if (compressed.has_value()) {
128127
if (GRPC_TRACE_FLAG_ENABLED(compression)) {
129128
const char* algo_name;
130-
const size_t before_size = payload->Length();
131-
const size_t after_size = tmp.Length();
129+
GRPC_CHECK(grpc_compression_algorithm_name(algorithm, &algo_name));
130+
const size_t before_size = message->payload()->Length();
131+
const size_t after_size = compressed->Length();
132132
const float savings_ratio = 1.0f - (static_cast<float>(after_size) /
133133
static_cast<float>(before_size));
134-
GRPC_CHECK(grpc_compression_algorithm_name(algorithm, &algo_name));
135134
LOG(INFO) << absl::StrFormat(
136135
"Compressed[%s] %" PRIuPTR " bytes vs. %" PRIuPTR
137136
" bytes (%.2f%% savings)",
138137
algo_name, before_size, after_size, 100 * savings_ratio);
139138
}
140-
tmp.Swap(payload);
139+
*message->payload() = std::move(*compressed);
141140
flags |= GRPC_WRITE_INTERNAL_COMPRESS;
142141
if (call_tracer != nullptr) {
143142
call_tracer->RecordSendCompressedMessage(*message);
@@ -148,7 +147,7 @@ MessageHandle ChannelCompression::CompressMessage(
148147
GRPC_CHECK(grpc_compression_algorithm_name(algorithm, &algo_name));
149148
LOG(INFO) << "Algorithm '" << algo_name
150149
<< "' enabled but decided not to compress. Input size: "
151-
<< payload->Length();
150+
<< message->payload()->Length();
152151
}
153152
}
154153
return message;
@@ -180,15 +179,17 @@ absl::StatusOr<MessageHandle> ChannelCompression::DecompressMessage(
180179
return std::move(message);
181180
}
182181
// Try to decompress the payload.
183-
SliceBuffer decompressed_slices;
184-
if (grpc_msg_decompress(args.algorithm, message->payload()->c_slice_buffer(),
185-
decompressed_slices.c_slice_buffer()) == 0) {
186-
return absl::InternalError(
187-
absl::StrCat("Unexpected error decompressing data for algorithm ",
188-
CompressionAlgorithmAsString(args.algorithm)));
182+
std::optional<uint32_t> max_output_size = IsMessageSizeRefactoringEnabled()
183+
? args.max_recv_message_length
184+
: std::nullopt;
185+
absl::StatusOr<SliceBuffer> decompressed_slices =
186+
MessageDecompress(args.algorithm, *message->payload(), max_output_size);
187+
if (!decompressed_slices.ok()) {
188+
return decompressed_slices.status();
189189
}
190-
// Swap the decompressed slices into the message.
191-
message->payload()->Swap(&decompressed_slices);
190+
191+
// Move the decompressed slices into the message.
192+
*message->payload() = std::move(*decompressed_slices);
192193
message->mutable_flags() &= ~GRPC_WRITE_INTERNAL_COMPRESS;
193194
message->mutable_flags() |= GRPC_WRITE_INTERNAL_TEST_ONLY_WAS_COMPRESSED;
194195
if (call_tracer != nullptr) {
@@ -213,18 +214,10 @@ grpc_compression_algorithm ChannelCompression::HandleOutgoingMetadata(
213214
ChannelCompression::DecompressArgs ChannelCompression::HandleIncomingMetadata(
214215
const grpc_metadata_batch& incoming_metadata) {
215216
// Configure max receive size.
216-
auto max_recv_message_length = max_recv_size_;
217-
const MessageSizeParsedConfig* limits =
218-
MessageSizeParsedConfig::GetFromCallContext(
219-
GetContext<Arena>(), message_size_service_config_parser_index_);
220-
if (limits != nullptr && limits->max_recv_size().has_value() &&
221-
(!max_recv_message_length.has_value() ||
222-
*limits->max_recv_size() < *max_recv_message_length)) {
223-
max_recv_message_length = limits->max_recv_size();
224-
}
225-
return DecompressArgs{incoming_metadata.get(GrpcEncodingMetadata())
226-
.value_or(GRPC_COMPRESS_NONE),
227-
max_recv_message_length};
217+
return DecompressArgs{
218+
incoming_metadata.get(GrpcEncodingMetadata())
219+
.value_or(GRPC_COMPRESS_NONE),
220+
GetMaxRecvSizeFromCallContext(GetContext<Arena>(), max_recv_size_)};
228221
}
229222

230223
void ClientCompressionFilter::Call::OnClientInitialMetadata(

‎src/core/ext/filters/http/message_compress/compression_filter.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ class ChannelCompression {
107107
private:
108108
// Max receive message length, if set.
109109
std::optional<uint32_t> max_recv_size_;
110-
size_t message_size_service_config_parser_index_;
111110
// The default, channel-level, compression algorithm.
112111
grpc_compression_algorithm default_compression_algorithm_;
113112
// Enabled compression algorithms.

‎src/core/ext/transport/chttp2/transport/chttp2_transport.cc‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
#include "src/core/telemetry/stats_data.h"
107107
#include "src/core/telemetry/tcp_tracer.h"
108108
#include "src/core/transport/auth_context.h"
109+
#include "src/core/transport/message_size_service_config.h"
109110
#include "src/core/util/bitset.h"
110111
#include "src/core/util/crash.h"
111112
#include "src/core/util/debug_location.h"
@@ -816,6 +817,7 @@ grpc_chttp2_transport::grpc_chttp2_transport(
816817
Ref(), &init_keepalive_ping_locked),
817818
absl::OkStatus());
818819

820+
// TODO(tjagtap) : [PH2][P0] : Do this for PH2.
819821
if (flow_control.bdp_probe()) {
820822
bdp_ping_blocked = true;
821823
grpc_chttp2_act_on_flowctl_action(flow_control.PeriodicUpdate(), this,
@@ -859,6 +861,9 @@ grpc_chttp2_transport::grpc_chttp2_transport(
859861
epte->SetSocketNode(channelz_socket);
860862
}
861863
}
864+
865+
max_recv_message_length =
866+
grpc_core::GetMaxRecvSizeFromChannelArgs(channel_args);
862867
}
863868

864869
static void destroy_transport_locked(void* tp, grpc_error_handle /*error*/) {
@@ -879,6 +884,10 @@ static void close_transport_locked(grpc_chttp2_transport* t,
879884
grpc_error_handle error) {
880885
end_all_the_calls(t, error);
881886
cancel_pings(t, error);
887+
if (t->transport_framing_endpoint_extension != nullptr) {
888+
t->transport_framing_endpoint_extension->SetSendFrameCallback(nullptr);
889+
t->transport_framing_endpoint_extension = nullptr;
890+
}
882891
if (t->closed_with_error.ok()) {
883892
if (!grpc_error_has_clear_grpc_status(error)) {
884893
error =
@@ -1507,7 +1516,8 @@ static grpc_closure* add_closure_barrier(grpc_closure* closure) {
15071516
return closure;
15081517
}
15091518

1510-
static void null_then_sched_closure(grpc_closure** closure) {
1519+
static void null_then_sched_closure_with_error(grpc_closure** closure,
1520+
grpc_error_handle error) {
15111521
grpc_closure* c = *closure;
15121522
*closure = nullptr;
15131523
// null_then_schedule_closure might be run during a start_batch which might
@@ -1516,7 +1526,11 @@ static void null_then_sched_closure(grpc_closure** closure) {
15161526
// completion, have the application see it, and make a new operation on the
15171527
// call which recycles the batch BEFORE the call to start_batch completes,
15181528
// forcing a race.
1519-
grpc_core::ExecCtx::Run(DEBUG_LOCATION, c, absl::OkStatus());
1529+
grpc_core::ExecCtx::Run(DEBUG_LOCATION, c, error);
1530+
}
1531+
1532+
static void null_then_sched_closure(grpc_closure** closure) {
1533+
null_then_sched_closure_with_error(closure, absl::OkStatus());
15201534
}
15211535

15221536
void grpc_chttp2_complete_closure_step(grpc_chttp2_transport* t,
@@ -2343,6 +2357,8 @@ void grpc_chttp2_maybe_complete_recv_initial_metadata(grpc_chttp2_transport* t,
23432357
t->registered_method_matcher_cb(t->accept_stream_cb_user_data,
23442358
s->recv_initial_metadata);
23452359
}
2360+
s->max_recv_message_length =
2361+
GetMaxRecvSizeFromCallContext(s->arena, t->max_recv_message_length);
23462362
null_then_sched_closure(&s->recv_initial_metadata_ready);
23472363
}
23482364
}
@@ -2415,7 +2431,36 @@ void grpc_chttp2_maybe_complete_recv_message(grpc_chttp2_transport* t,
24152431
*s->call_failed_before_recv_message =
24162432
(s->published_metadata[1] != GRPC_METADATA_PUBLISHED_AT_CLOSE);
24172433
}
2418-
null_then_sched_closure(&s->recv_message_ready);
2434+
if (s->seen_error && s->message_size_limit_exceeded) {
2435+
null_then_sched_closure_with_error(&s->recv_message_ready, error);
2436+
} else {
2437+
null_then_sched_closure(&s->recv_message_ready);
2438+
}
2439+
} else if (s->seen_error && s->message_size_limit_exceeded) {
2440+
null_then_sched_closure_with_error(&s->recv_message_ready, error);
2441+
}
2442+
2443+
if (grpc_core::IsMessageSizeRefactoringEnabled() && s->seen_error &&
2444+
s->message_size_limit_exceeded) {
2445+
s->message_size_limit_exceeded = false;
2446+
// Explicitly cancel the stream to fail the RPC immediately.
2447+
// We delay the cancellation until the recv_message_ready closure is
2448+
// executed to prevent re-entrancy issues. When the HTTP/2 frame deframer
2449+
// in `grpc_deframe_unprocessed_incoming_frames`
2450+
// returns a size violation error, invoking `grpc_chttp2_cancel_stream`
2451+
// immediately triggers `grpc_chttp2_mark_stream_closed`. This in turn
2452+
// synchronously re-enters `grpc_chttp2_maybe_complete_recv_message` while
2453+
// the original deframing loop is still active on the call stack.
2454+
//
2455+
// Because the re-entrant invocation observes that the incoming frame
2456+
// storage has been cleared but `s->recv_message_ready` is still non-null,
2457+
// it preemptively schedules the closure as a standard/non-error stream
2458+
// termination (i.e., via `null_then_sched_closure(...,
2459+
// absl::OkStatus())`). By the time control returns to the initial (outer)
2460+
// deframing loop to execute `null_then_sched_closure_with_error`, the
2461+
// closure pointer is already null, causing the original error context to
2462+
// be discarded.
2463+
grpc_chttp2_cancel_stream(t, s, error, true, nullptr);
24192464
}
24202465
}();
24212466

‎src/core/ext/transport/chttp2/transport/frame_data.cc‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ grpc_core::Poll<grpc_error_handle> grpc_deframe_unprocessed_incoming_frames(
123123
(static_cast<uint32_t>(header[3]) << 8) |
124124
static_cast<uint32_t>(header[4]);
125125

126+
if (grpc_core::IsMessageSizeRefactoringEnabled()) {
127+
if (s->max_recv_message_length.has_value() &&
128+
length > *(s->max_recv_message_length)) {
129+
error = GRPC_ERROR_CREATE(
130+
absl::StrFormat("%s: Received message larger than max (%d vs. %u)",
131+
s->t->is_client ? "CLIENT" : "SERVER", length,
132+
*(s->max_recv_message_length)));
133+
error = grpc_error_set_int(error, grpc_core::StatusIntProperty::kStreamId,
134+
static_cast<intptr_t>(s->id));
135+
// Attach the explicit gRPC status code to fail the RPC correctly.
136+
error =
137+
grpc_error_set_int(error, grpc_core::StatusIntProperty::kRpcStatus,
138+
GRPC_STATUS_RESOURCE_EXHAUSTED);
139+
s->message_size_limit_exceeded = true;
140+
return error;
141+
}
142+
}
143+
126144
if (slices->length < length + GRPC_HEADER_SIZE_IN_BYTES) {
127145
if (min_progress_size != nullptr) {
128146
*min_progress_size = length + GRPC_HEADER_SIZE_IN_BYTES - slices->length;

‎src/core/ext/transport/chttp2/transport/frame_goaway.cc‎

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@
3131
#include "absl/strings/string_view.h"
3232

3333
void grpc_chttp2_goaway_parser_init(grpc_chttp2_goaway_parser* p) {
34-
p->debug_data = nullptr;
34+
if (!grpc_core::IsPh2Perf01Enabled()) {
35+
p->debug_data = nullptr;
36+
}
3537
}
3638

3739
void grpc_chttp2_goaway_parser_destroy(grpc_chttp2_goaway_parser* p) {
38-
gpr_free(p->debug_data);
40+
if (!grpc_core::IsPh2Perf01Enabled()) {
41+
gpr_free(p->debug_data);
42+
}
3943
}
4044

4145
grpc_error_handle grpc_chttp2_goaway_parser_begin_frame(
@@ -45,10 +49,14 @@ grpc_error_handle grpc_chttp2_goaway_parser_begin_frame(
4549
absl::StrFormat("goaway frame too short (%d bytes)", length));
4650
}
4751

48-
gpr_free(p->debug_data);
4952
p->debug_length = length - 8;
50-
p->debug_data = static_cast<char*>(gpr_malloc(p->debug_length));
51-
p->debug_pos = 0;
53+
if (grpc_core::IsPh2Perf01Enabled()) {
54+
p->debug_slice_buffer.Clear();
55+
} else {
56+
gpr_free(p->debug_data);
57+
p->debug_data = static_cast<char*>(gpr_malloc(p->debug_length));
58+
p->debug_pos = 0;
59+
}
5260
p->state = GRPC_CHTTP2_GOAWAY_LSI0;
5361
return absl::OkStatus();
5462
}
@@ -130,24 +138,48 @@ grpc_error_handle grpc_chttp2_goaway_parser_parse(void* parser,
130138
++cur;
131139
[[fallthrough]];
132140
case GRPC_CHTTP2_GOAWAY_DEBUG:
133-
if (end != cur) {
134-
memcpy(p->debug_data + p->debug_pos, cur,
135-
static_cast<size_t>(end - cur));
136-
}
137-
GRPC_CHECK((size_t)(end - cur) < UINT32_MAX - p->debug_pos);
138-
p->debug_pos += static_cast<uint32_t>(end - cur);
139-
p->state = GRPC_CHTTP2_GOAWAY_DEBUG;
140-
if (is_last) {
141-
t->http2_ztrace_collector.Append([p]() {
142-
return grpc_core::H2GoAwayTrace<true>{
143-
p->last_stream_id, p->error_code,
144-
std::string(absl::string_view(p->debug_data, p->debug_length))};
145-
});
146-
grpc_chttp2_add_incoming_goaway(
147-
t, p->error_code, p->last_stream_id,
148-
absl::string_view(p->debug_data, p->debug_length));
149-
gpr_free(p->debug_data);
150-
p->debug_data = nullptr;
141+
if (grpc_core::IsPh2Perf01Enabled()) {
142+
if (end != cur) {
143+
p->debug_slice_buffer.AppendIndexed(
144+
grpc_core::Slice::FromCopiedBuffer(
145+
cur, static_cast<size_t>(end - cur)));
146+
}
147+
GRPC_CHECK(p->debug_slice_buffer.Length() <= p->debug_length);
148+
p->state = GRPC_CHTTP2_GOAWAY_DEBUG;
149+
if (is_last) {
150+
std::string debug_str = p->debug_slice_buffer.JoinIntoString();
151+
152+
grpc_chttp2_add_incoming_goaway(t, p->error_code, p->last_stream_id,
153+
absl::string_view(debug_str));
154+
155+
t->http2_ztrace_collector.Append(
156+
[last_stream_id = p->last_stream_id, error_code = p->error_code,
157+
debug_str = std::move(debug_str)]() mutable {
158+
return grpc_core::H2GoAwayTrace<true>{
159+
last_stream_id, error_code, std::move(debug_str)};
160+
});
161+
p->debug_slice_buffer.Clear();
162+
}
163+
} else {
164+
if (end != cur) {
165+
memcpy(p->debug_data + p->debug_pos, cur,
166+
static_cast<size_t>(end - cur));
167+
}
168+
GRPC_CHECK((size_t)(end - cur) < UINT32_MAX - p->debug_pos);
169+
p->debug_pos += static_cast<uint32_t>(end - cur);
170+
p->state = GRPC_CHTTP2_GOAWAY_DEBUG;
171+
if (is_last) {
172+
t->http2_ztrace_collector.Append([p]() {
173+
return grpc_core::H2GoAwayTrace<true>{
174+
p->last_stream_id, p->error_code,
175+
std::string(absl::string_view(p->debug_data, p->debug_length))};
176+
});
177+
grpc_chttp2_add_incoming_goaway(
178+
t, p->error_code, p->last_stream_id,
179+
absl::string_view(p->debug_data, p->debug_length));
180+
gpr_free(p->debug_data);
181+
p->debug_data = nullptr;
182+
}
151183
}
152184
return absl::OkStatus();
153185
}

‎src/core/ext/transport/chttp2/transport/frame_goaway.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct grpc_chttp2_goaway_parser {
4444
uint32_t last_stream_id;
4545
uint32_t error_code;
4646
char* debug_data;
47+
grpc_core::SliceBuffer debug_slice_buffer;
4748
uint32_t debug_length;
4849
uint32_t debug_pos;
4950
};

‎src/core/ext/transport/chttp2/transport/internal.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ struct grpc_chttp2_transport final : public grpc_core::FilterStackTransport,
629629

630630
GPR_NO_UNIQUE_ADDRESS grpc_core::latent_see::Flow write_flow;
631631

632+
std::optional<uint32_t> max_recv_message_length;
633+
632634
// Current mitigation engine, retrieved once per connection.
633635
grpc_core::RefCountedPtr<grpc_core::MitigationEngine> mitigation_engine;
634636
};
@@ -777,6 +779,9 @@ struct grpc_chttp2_stream {
777779
// The last time a stream window update was received.
778780
grpc_core::Timestamp last_window_update_time =
779781
grpc_core::Timestamp::InfPast();
782+
783+
bool message_size_limit_exceeded = false;
784+
std::optional<uint32_t> max_recv_message_length;
780785
};
781786

782787
/// Transport writing call flow:

0 commit comments

Comments
 (0)