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

[release/6.0] Fix QUIC ConnectionState NRE in HandleEventConnectionClose #57742

Merged
merged 2 commits into from
Aug 23, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public void Cleanup()
// inbound.
internal MsQuicStream(MsQuicConnection.State connectionState, SafeMsQuicStreamHandle streamHandle, QUIC_STREAM_OPEN_FLAGS flags)
{
if (!connectionState.TryAddStream(this))
{
throw new ObjectDisposedException(nameof(QuicConnection));
}
// this assignment should be done before SetCallbackHandlerDelegate to prevent NRE in HandleEventConnectionClose
// but after TryAddStream to prevent unnecessary RemoveStream in finalizer
_state.ConnectionState = connectionState;

_state.Handle = streamHandle;
_canRead = true;
_canWrite = !flags.HasFlag(QUIC_STREAM_OPEN_FLAGS.UNIDIRECTIONAL);
Expand All @@ -117,14 +125,6 @@ internal MsQuicStream(MsQuicConnection.State connectionState, SafeMsQuicStreamHa
throw;
}

if (!connectionState.TryAddStream(this))
{
_state.StateGCHandle.Free();
throw new ObjectDisposedException(nameof(QuicConnection));
}

_state.ConnectionState = connectionState;

_state.TraceId = MsQuicTraceHelper.GetTraceId(_state.Handle);
if (NetEventSource.Log.IsEnabled())
{
Expand All @@ -140,6 +140,14 @@ internal MsQuicStream(MsQuicConnection.State connectionState, QUIC_STREAM_OPEN_F
{
Debug.Assert(connectionState.Handle != null);

if (!connectionState.TryAddStream(this))
{
throw new ObjectDisposedException(nameof(QuicConnection));
}
// this assignment should be done before StreamOpenDelegate to prevent NRE in HandleEventConnectionClose
// but after TryAddStream to prevent unnecessary RemoveStream in finalizer
_state.ConnectionState = connectionState;

_canRead = !flags.HasFlag(QUIC_STREAM_OPEN_FLAGS.UNIDIRECTIONAL);
_canWrite = true;

Expand Down Expand Up @@ -170,15 +178,6 @@ internal MsQuicStream(MsQuicConnection.State connectionState, QUIC_STREAM_OPEN_F
throw;
}

if (!connectionState.TryAddStream(this))
{
_state.Handle?.Dispose();
_state.StateGCHandle.Free();
throw new ObjectDisposedException(nameof(QuicConnection));
}

_state.ConnectionState = connectionState;

_state.TraceId = MsQuicTraceHelper.GetTraceId(_state.Handle);
if (NetEventSource.Log.IsEnabled())
{
Expand Down