Skip to content

Commit 43cc4c1

Browse files
authored
Fix data race on incoming MsQuicStream abort event (#69483)
* Fix data race on MsQuicStream abort * fixup! Fix data race on MsQuicStream abort
1 parent 9260c24 commit 43cc4c1

File tree

1 file changed

+5
-2
lines changed
  • src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic

1 file changed

+5
-2
lines changed

src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/MsQuicStream.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ private CancellationTokenRegistration SetupWriteStartState(bool emptyBuffer, Can
329329
{
330330
throw new InvalidOperationException(SR.net_quic_writing_notallowed);
331331
}
332-
if (_state.SendState == SendState.Aborted)
332+
// Use Volatile.Read to ensure we read the actual SendErrorCode set by the racing callback thread.
333+
if ((SendState)Volatile.Read(ref Unsafe.As<SendState, int>(ref _state.SendState)) == SendState.Aborted)
333334
{
334335
if (_state.SendErrorCode != -1)
335336
{
@@ -1112,8 +1113,10 @@ private static int HandleEventPeerRecvAborted(State state, ref QUIC_STREAM_EVENT
11121113
shouldShutdownWriteComplete = true;
11131114
}
11141115

1115-
state.SendState = SendState.Aborted;
11161116
state.SendErrorCode = (long)streamEvent.PEER_RECEIVE_ABORTED.ErrorCode;
1117+
// make sure the SendErrorCode above is commited to memory before we assign the state. This
1118+
// ensures that the code is read correctly in SetupWriteStartState when checking without lock
1119+
Volatile.Write(ref Unsafe.As<SendState, int>(ref state.SendState), (int)SendState.Aborted);
11171120
}
11181121

11191122
if (shouldSendComplete)

0 commit comments

Comments
 (0)