From: Tomas Weinfurt Date: Sun, 16 Jun 2019 04:40:37 +0000 (-0700) Subject: make sure we close OS handle if socket is finalized without disposing (dotnet/corefx... X-Git-Tag: submit/tizen/20210909.063632~11031^2~1273 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=81acdda860b1b07824eaf8e95a047e5e557dc449;p=platform%2Fupstream%2Fdotnet%2Fruntime.git 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 --- 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)); }); }