check handle before shutdown in quic connection Dispose (#56047)
authorTomas Weinfurt <tweinfurt@yahoo.com>
Wed, 21 Jul 2021 03:10:23 +0000 (20:10 -0700)
committerGitHub <noreply@github.com>
Wed, 21 Jul 2021 03:10:23 +0000 (20:10 -0700)
* check handle before shutdown in quic connection Dispose

* add comment

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

index ef88126..6fb6588 100644 (file)
@@ -712,10 +712,14 @@ namespace System.Net.Quic.Implementations.MsQuic
             if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(_state, $"{TraceId()} Stream disposing {disposing}");
 
             // If we haven't already shutdown gracefully (via a successful CloseAsync call), then force an abortive shutdown.
-            MsQuicApi.Api.ConnectionShutdownDelegate(
-                _state.Handle,
-                QUIC_CONNECTION_SHUTDOWN_FLAGS.SILENT,
-                0);
+            if (_state.Handle != null)
+            {
+                // Handle can be null if outbound constructor failed and we are called from finalizer.
+                MsQuicApi.Api.ConnectionShutdownDelegate(
+                    _state.Handle,
+                    QUIC_CONNECTION_SHUTDOWN_FLAGS.SILENT,
+                    0);
+            }
 
             bool releaseHandles = false;
             lock (_state)