Skip to content

Commit 2b46720

Browse files
[release/6.0-rc1] Improve tracing of HTTP/2 PINGs (#57998)
* more HTTP/2 PING logging * test unexpected PING response Co-authored-by: Anton Firszov <antonfir@gmail.com>
1 parent 2170911 commit 2b46720

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,8 @@ private void ProcessPingFrame(FrameHeader frameHeader)
915915
ReadOnlySpan<byte> pingContent = _incomingBuffer.ActiveSpan.Slice(0, FrameHeader.PingLength);
916916
long pingContentLong = BinaryPrimitives.ReadInt64BigEndian(pingContent);
917917

918+
if (NetEventSource.Log.IsEnabled()) Trace($"Received PING frame, content:{pingContentLong} ack: {frameHeader.AckFlag}");
919+
918920
if (frameHeader.AckFlag)
919921
{
920922
ProcessPingAck(pingContentLong);

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2StreamWindowManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ internal void OnDataOrHeadersReceived(Http2Connection connection)
213213

214214
// Send a PING
215215
_pingCounter--;
216+
if (NetEventSource.Log.IsEnabled()) connection.Trace($"[FlowControl] Sending RTT PING with payload {_pingCounter}");
216217
connection.LogExceptions(connection.SendPingAsync(_pingCounter, isAck: false));
217218
_pingSentTimestamp = now;
218219
_state = State.PingSent;
@@ -223,6 +224,7 @@ internal void OnPingAckReceived(long payload, Http2Connection connection)
223224
{
224225
if (_state != State.PingSent && _state != State.TerminatingMayReceivePingAck)
225226
{
227+
if (NetEventSource.Log.IsEnabled()) connection.Trace($"[FlowControl] Unexpected PING ACK in state {_state}");
226228
ThrowProtocolError();
227229
}
228230

@@ -236,7 +238,10 @@ internal void OnPingAckReceived(long payload, Http2Connection connection)
236238
Debug.Assert(payload < 0);
237239

238240
if (_pingCounter != payload)
241+
{
242+
if (NetEventSource.Log.IsEnabled()) connection.Trace($"[FlowControl] Unexpected RTT PING ACK payload {payload}, should be {_pingCounter}.");
239243
ThrowProtocolError();
244+
}
240245

241246
RefreshRtt(connection);
242247
_state = State.Waiting;

src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.Http2FlowControl.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public async Task InitialHttp2StreamWindowSize_SentInSettingsFrame()
5151
Assert.Equal(WindowSize, (int)entry.Value);
5252
}
5353

54-
[Fact]
55-
public Task InvalidRttPingResponse_RequestShouldFail()
54+
[Theory]
55+
[InlineData(0)] // Invalid PING payload
56+
[InlineData(1)] // Unexpected PING response
57+
public Task BadRttPingResponse_RequestShouldFail(int mode)
5658
{
5759
return Http2LoopbackServer.CreateClientAndServerAsync(async uri =>
5860
{
@@ -67,13 +69,24 @@ public Task InvalidRttPingResponse_RequestShouldFail()
6769
(int streamId, _) = await connection.ReadAndParseRequestHeaderAsync();
6870
await connection.SendDefaultResponseHeadersAsync(streamId);
6971
PingFrame pingFrame = await connection.ReadPingAsync(); // expect an RTT PING
70-
await connection.SendPingAckAsync(-6666); // send an invalid PING response
72+
73+
if (mode == 0)
74+
{
75+
// Invalid PING payload
76+
await connection.SendPingAckAsync(-6666); // send an invalid PING response
77+
}
78+
else
79+
{
80+
// Unexpected PING response
81+
await connection.SendPingAckAsync(pingFrame.Data); // send an valid PING response
82+
await connection.SendPingAckAsync(pingFrame.Data - 1); // send a second unexpected PING response
83+
}
84+
7185
await connection.SendResponseDataAsync(streamId, new byte[] { 1, 2, 3 }, true); // otherwise fine response
7286
},
7387
NoAutoPingResponseHttp2Options);
7488
}
7589

76-
7790
[OuterLoop("Runs long")]
7891
[Fact]
7992
public async Task HighBandwidthDelayProduct_ClientStreamReceiveWindowWindowScalesUp()

0 commit comments

Comments
 (0)