Skip to content
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

Fix exception thrown from MsQuicStream writes when connection is aborted by peer. #67651

Merged
merged 2 commits into from
Apr 8, 2022
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -1264,9 +1264,9 @@ private static uint HandleEventPeerSendShutdown(State state)
private static uint HandleEventSendComplete(State state, ref StreamEvent evt)
{
StreamEventDataSendComplete sendCompleteEvent = evt.Data.SendComplete;
bool canceled = sendCompleteEvent.Canceled != 0;

bool complete = false;
Exception? abortException = null;

lock (state)
{
Expand All @@ -1276,24 +1276,33 @@ private static uint HandleEventSendComplete(State state, ref StreamEvent evt)
complete = true;
}

if (canceled)
if (sendCompleteEvent.Canceled != 0)
{
state.SendState = SendState.Aborted;

//
// There are multiple reasons the send could have been cancelled:
// - Connection was aborted (either by transport or peer) => error-code already provided on the connection-level event
// - Stream's receive side was aborted by peer => already handled by HandleEventPeerRecvAborted
// and we will not set the exception due to complete == false
// - Stream's send side was aborted locally => no connection-level abort code and we return QuicOperationAbortException
//
abortException = ThrowHelper.GetConnectionAbortedException(state.ConnectionState.AbortErrorCode);
}
}

if (complete)
{
CleanupSendState(state);

if (!canceled)
if (abortException == null)
{
state.SendResettableCompletionSource.Complete(MsQuicStatusCodes.Success);
}
else
{
state.SendResettableCompletionSource.CompleteException(
ExceptionDispatchInfo.SetCurrentStackTrace(new OperationCanceledException("Write was canceled")));
ExceptionDispatchInfo.SetCurrentStackTrace(abortException));
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason to create the exception within the lock?
Could this whole change be reduced to just:

Suggested change
ExceptionDispatchInfo.SetCurrentStackTrace(abortException));
ExceptionDispatchInfo.SetCurrentStackTrace(ThrowHelper.GetConnectionAbortedException(state.ConnectionState.AbortErrorCode)));

Copy link
Member Author

Choose a reason for hiding this comment

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

I originally had code dealing with stream write aborts as well, until I realized that they are handled in HandleEventPeerRecvAborted and will never get hit here. So if accessing state.ConnectionState.AbortErrorCode is fine outside of the lock, then I can simplify this.

}
}

Expand Down