make sure we close OS handle if socket is finalized without disposing (dotnet/corefx...
authorTomas Weinfurt <tweinfurt@yahoo.com>
Sun, 16 Jun 2019 04:40:37 +0000 (21:40 -0700)
committerGitHub <noreply@github.com>
Sun, 16 Jun 2019 04:40:37 +0000 (21:40 -0700)
* socket_finalize_37044

* update test

Commit migrated from https://github.com/dotnet/corefx/commit/23faf5d032feef556f5399b46a92a6e4aef7d84a

src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs
src/libraries/System.Net.Sockets/tests/FunctionalTests/DisposedSocketTests.netcoreapp.cs

index acb5405..1f12774 100644 (file)
@@ -4461,16 +4461,11 @@ namespace System.Net.Sockets
 
         protected virtual void Dispose(bool disposing)
         {
-            if (!disposing)
-            {
-                return;
-            }
-
             if (NetEventSource.IsEnabled)
             {
                 try
                 {
-                    NetEventSource.Info(this, $"disposing:true CleanedUp:{CleanedUp}");
+                    NetEventSource.Info(this, $"disposing:{disposing} CleanedUp:{CleanedUp}");
                     NetEventSource.Enter(this);
                 }
                 catch (Exception exception) when (!ExceptionCheck.IsFatal(exception)) { }
@@ -4490,11 +4485,11 @@ namespace System.Net.Sockets
             try
             {
                 int timeout = _closeTimeout;
-                if (timeout == 0)
+                if (timeout == 0 || !disposing)
                 {
                     // Abortive.
                     if (NetEventSource.IsEnabled) NetEventSource.Info(this, "Calling _handle.Dispose()");
-                    _handle.Dispose();
+                    _handle?.Dispose();
                 }
                 else
                 {
index c893e79..a67682f 100644 (file)
@@ -331,6 +331,7 @@ namespace System.Net.Sockets
                             if (context != null)
                             {
                                 context.HandleEvents(_buffer[i].Events);
+                                context = null;
                             }
                         }
                     }
index 24e39eb..cfc85d2 100644 (file)
@@ -12,7 +12,6 @@ namespace System.Net.Sockets.Tests
 {
     public partial class DisposedSocket
     {
-        [ActiveIssue(37044, TestPlatforms.AnyUnix)]
         [Theory]
         [InlineData(false)]
         [InlineData(true)]
@@ -22,6 +21,7 @@ namespace System.Net.Sockets.Tests
             RetryHelper.Execute(() =>
             {
                 GC.Collect();
+                GC.WaitForPendingFinalizers();
                 Assert.Equal(0, handles.Count(h => h.IsAlive));
             });
         }