Skip to content

Resend Close Frames #5107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/core/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,8 @@ MsQuicStreamSend(
Oper->Type = QUIC_OPER_TYPE_API_CALL;
Oper->API_CALL.Context = &Connection->BackupApiContext;
Oper->API_CALL.Context->Type = QUIC_API_TYPE_CONN_SHUTDOWN;
Oper->API_CALL.Context->CONN_SHUTDOWN.Flags = QUIC_CONNECTION_SHUTDOWN_FLAG_SILENT;
Oper->API_CALL.Context->CONN_SHUTDOWN.Flags =
QUIC_CONNECTION_SHUTDOWN_FLAG_SILENT | QUIC_CONNECTION_SHUTDOWN_FLAG_STATUS;
Oper->API_CALL.Context->CONN_SHUTDOWN.ErrorCode = (QUIC_VAR_INT)QUIC_STATUS_OUT_OF_MEMORY;
Oper->API_CALL.Context->CONN_SHUTDOWN.RegistrationShutdown = FALSE;
Oper->API_CALL.Context->CONN_SHUTDOWN.TransportShutdown = TRUE;
Expand Down Expand Up @@ -1359,7 +1360,8 @@ MsQuicStreamReceiveComplete(
Oper->Type = QUIC_OPER_TYPE_API_CALL;
Oper->API_CALL.Context = &Connection->BackupApiContext;
Oper->API_CALL.Context->Type = QUIC_API_TYPE_CONN_SHUTDOWN;
Oper->API_CALL.Context->CONN_SHUTDOWN.Flags = QUIC_CONNECTION_SHUTDOWN_FLAG_SILENT;
Oper->API_CALL.Context->CONN_SHUTDOWN.Flags =
QUIC_CONNECTION_SHUTDOWN_FLAG_SILENT | QUIC_CONNECTION_SHUTDOWN_FLAG_STATUS;
Oper->API_CALL.Context->CONN_SHUTDOWN.ErrorCode = (QUIC_VAR_INT)QUIC_STATUS_INVALID_STATE;
Oper->API_CALL.Context->CONN_SHUTDOWN.RegistrationShutdown = FALSE;
Oper->API_CALL.Context->CONN_SHUTDOWN.TransportShutdown = TRUE;
Expand Down
7 changes: 7 additions & 0 deletions src/core/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

--*/

//
// Private flag of the QUIC_CONNECTION_SHUTDOWN_FLAGS enum used for indicating
// the type of error code being passed in. Not exposed to apps because they
Copy link
Contributor

Choose a reason for hiding this comment

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

nti:

Suggested change
// the type of error code being passed in. Not exposed to apps because they
// a QUIC_STATUS is being passed (and not a QUIC_ERROR).
// Not exposed to apps because they

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The "not a QUIC_ERROR" isn't actually correct either 😄 I think I'd rather just leave it as is for now and not write a massive comment explaining the various types of errors in MsQuic

// should not be using different types of errors directly.
//
#define QUIC_CONNECTION_SHUTDOWN_FLAG_STATUS ((QUIC_CONNECTION_SHUTDOWN_FLAGS)0x8000)

_IRQL_requires_max_(PASSIVE_LEVEL)
QUIC_STATUS
QUIC_API
Expand Down
3 changes: 2 additions & 1 deletion src/core/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,8 @@ QuicBindingCreateConnection(
Oper->Type = QUIC_OPER_TYPE_API_CALL;
Oper->API_CALL.Context = &NewConnection->BackupApiContext;
Oper->API_CALL.Context->Type = QUIC_API_TYPE_CONN_SHUTDOWN;
Oper->API_CALL.Context->CONN_SHUTDOWN.Flags = QUIC_CONNECTION_SHUTDOWN_FLAG_SILENT;
Oper->API_CALL.Context->CONN_SHUTDOWN.Flags =
QUIC_CONNECTION_SHUTDOWN_FLAG_SILENT | QUIC_CONNECTION_SHUTDOWN_FLAG_STATUS;
Oper->API_CALL.Context->CONN_SHUTDOWN.ErrorCode = (QUIC_VAR_INT)QUIC_STATUS_INTERNAL_ERROR;
Oper->API_CALL.Context->CONN_SHUTDOWN.RegistrationShutdown = FALSE;
Oper->API_CALL.Context->CONN_SHUTDOWN.TransportShutdown = TRUE;
Expand Down
24 changes: 24 additions & 0 deletions src/core/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ QuicConnShutdown(
(!Connection->State.Started && QuicConnIsClient(Connection))) {
CloseFlags |= QUIC_CLOSE_SILENT;
}
if (Flags & QUIC_CONNECTION_SHUTDOWN_FLAG_STATUS) {
CloseFlags |= QUIC_CLOSE_QUIC_STATUS;
}

QuicConnCloseLocally(Connection, CloseFlags, ErrorCode, NULL);
}
Expand Down Expand Up @@ -703,6 +706,13 @@ QuicConnQueueOper(
CXPLAT_DBG_ASSERT(QuicConnIsServer(Connection));
CXPLAT_DBG_ASSERT(Connection->SourceCids.Next != NULL || CxPlatIsRandomMemoryFailureEnabled());
}
if (Oper->Type == QUIC_OPER_TYPE_API_CALL) {
if (Oper->API_CALL.Context->Type == QUIC_API_TYPE_CONN_SHUTDOWN) {
CXPLAT_DBG_ASSERT(
(Oper->API_CALL.Context->CONN_SHUTDOWN.ErrorCode <= QUIC_VAR_INT_MAX) ||
(Oper->API_CALL.Context->CONN_SHUTDOWN.Flags & QUIC_CONNECTION_SHUTDOWN_FLAG_STATUS));
}
}
#endif
if (QuicOperationEnqueue(&Connection->OperQ, Connection->Partition, Oper)) {
//
Expand Down Expand Up @@ -1591,6 +1601,7 @@ QuicConnTryClose(
Connection->CloseErrorCode = QUIC_ERROR_INTERNAL_ERROR;
} else {
Connection->CloseStatus = QuicErrorCodeToStatus(ErrorCode);
CXPLAT_DBG_ASSERT(ErrorCode <= QUIC_VAR_INT_MAX);
Connection->CloseErrorCode = ErrorCode;
if (QuicErrorIsProtocolError(ErrorCode)) {
QuicPerfCounterIncrement(
Expand Down Expand Up @@ -4411,6 +4422,19 @@ QuicConnRecvFrames(
uint16_t PayloadLength = Packet->PayloadLength;
uint64_t RecvTime = CxPlatTimeUs64();

//
// In closing state, respond to any packet with a new close frame (rate-limited).
//
if (Closed && !Connection->State.ShutdownComplete) {
if (RecvTime - Connection->LastCloseResponseTimeUs >= QUIC_CLOSING_RESPONSE_MIN_INTERVAL) {
QuicSendSetSendFlag(
&Connection->Send,
Connection->State.AppClosed ?
QUIC_CONN_SEND_FLAG_APPLICATION_CLOSE :
QUIC_CONN_SEND_FLAG_CONNECTION_CLOSE);
}
}

if (QuicConnIsClient(Connection) &&
!Connection->State.GotFirstServerResponse) {
Connection->State.GotFirstServerResponse = TRUE;
Expand Down
6 changes: 6 additions & 0 deletions src/core/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,12 @@ typedef struct QUIC_CONNECTION {
//
uint64_t EarliestExpirationTime;

//
// Timestamp (us) of when we last queued up a connection close (or
// application close) response to be sent.
//
uint64_t LastCloseResponseTimeUs;

//
// Receive packet queue.
//
Expand Down
6 changes: 6 additions & 0 deletions src/core/quicdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ CXPLAT_STATIC_ASSERT(
//
#define QUIC_DEFAULT_HANDSHAKE_IDLE_TIMEOUT 10000

//
// Minimum interval (in microseconds) between CONNECTION_CLOSE responses in
// closing state.
//
#define QUIC_CLOSING_RESPONSE_MIN_INTERVAL 5000

//
// The default value for keep alives being enabled or not.
//
Expand Down
1 change: 1 addition & 0 deletions src/core/send.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ QuicSendSetSendFlag(
}

if (IsCloseFrame) {
Connection->LastCloseResponseTimeUs = CxPlatTimeUs64();
QuicSendClear(Send);
}

Expand Down
Loading