From 81acdda860b1b07824eaf8e95a047e5e557dc449 Mon Sep 17 00:00:00 2001 From: Tomas Weinfurt Date: Sat, 15 Jun 2019 21:40:37 -0700 Subject: [PATCH] make sure we close OS handle if socket is finalized without disposing (dotnet/corefx#38499) * socket_finalize_37044 * update test Commit migrated from https://github.com/dotnet/corefx/commit/23faf5d032feef556f5399b46a92a6e4aef7d84a --- .../System.Net.Sockets/src/System/Net/Sockets/Socket.cs | 11 +++-------- .../src/System/Net/Sockets/SocketAsyncEngine.Unix.cs | 1 + .../tests/FunctionalTests/DisposedSocketTests.netcoreapp.cs | 2 +- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index acb5405..1f12774 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -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 { diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs index c893e79..a67682f 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs @@ -331,6 +331,7 @@ namespace System.Net.Sockets if (context != null) { context.HandleEvents(_buffer[i].Events); + context = null; } } } diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/DisposedSocketTests.netcoreapp.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/DisposedSocketTests.netcoreapp.cs index 24e39eb..cfc85d2 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/DisposedSocketTests.netcoreapp.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/DisposedSocketTests.netcoreapp.cs @@ -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)); }); } -- 2.7.4