From 10dfd9b58815b114f16477a8aa1f49bbb0b2c056 Mon Sep 17 00:00:00 2001 From: Gordon Ross Date: Sat, 20 Jul 2024 15:55:40 -0400 Subject: [PATCH] Fix MaxWorkingSet_GetNotStarted_ThrowsInvalidOperationException Fixes issue #105422 On MacOS, FreeBSD, SunOS (the ports that share ProcessBSD.c) the get/set WorkingSet methods only work on the current process. Skip the parts of tests that operate on other processes. Remove now redundant MacOS-speicifc tests. --- .../src/System/Diagnostics/Process.BSD.cs | 4 ++++ .../src/System/Diagnostics/Process.Linux.cs | 3 ++- .../tests/ProcessTests.cs | 22 +++---------------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.BSD.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.BSD.cs index eeac5579c180c0..97c0b27c730982 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.BSD.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.BSD.cs @@ -57,6 +57,8 @@ private static IntPtr ProcessorAffinityCore /// private void GetWorkingSetLimits(out IntPtr minWorkingSet, out IntPtr maxWorkingSet) { + EnsureState(State.HaveNonExitedId); + // We can only do this for the current process on OS X if (_processId != Environment.ProcessId) throw new PlatformNotSupportedException(SR.OsxExternalProcessWorkingSetNotSupported); @@ -86,6 +88,8 @@ private void GetWorkingSetLimits(out IntPtr minWorkingSet, out IntPtr maxWorking /// The resulting maximum working set limit after any changes applied. private void SetWorkingSetLimitsCore(IntPtr? newMin, IntPtr? newMax, out IntPtr resultingMin, out IntPtr resultingMax) { + EnsureState(State.HaveNonExitedId); + // We can only do this for the current process on OS X if (_processId != Environment.ProcessId) throw new PlatformNotSupportedException(SR.OsxExternalProcessWorkingSetNotSupported); diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Linux.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Linux.cs index cf5ceb58e831cc..3c707d7473bcd9 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Linux.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Linux.cs @@ -246,8 +246,9 @@ private void GetWorkingSetLimits(out IntPtr minWorkingSet, out IntPtr maxWorking /// The resulting minimum working set limit after any changes applied. /// The resulting maximum working set limit after any changes applied. #pragma warning disable IDE0060 - private static void SetWorkingSetLimitsCore(IntPtr? newMin, IntPtr? newMax, out IntPtr resultingMin, out IntPtr resultingMax) + private void SetWorkingSetLimitsCore(IntPtr? newMin, IntPtr? newMax, out IntPtr resultingMin, out IntPtr resultingMax) { + EnsureState(State.HaveNonExitedId); // RLIMIT_RSS with setrlimit not supported on Linux > 2.4.30. throw new PlatformNotSupportedException(SR.MinimumWorkingSetNotSupported); } diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs index 6907a43b630400..56d3a68bacb660 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs @@ -603,20 +603,12 @@ public void TestMaxWorkingSet() } [Fact] - [SkipOnPlatform(TestPlatforms.OSX | TestPlatforms.FreeBSD | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Getting MaxWorkingSet is not supported on OSX, BSD, iOS, MacCatalyst, and tvOS.")] + [SkipOnPlatform(TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Getting MaxWorkingSet is not supported on iOS, MacCatalyst, and tvOS.")] public void MaxWorkingSet_GetNotStarted_ThrowsInvalidOperationException() { var process = new Process(); Assert.Throws(() => process.MaxWorkingSet); - } - - [Fact] - [PlatformSpecific(TestPlatforms.OSX | TestPlatforms.FreeBSD)] - public void MaxValueWorkingSet_GetSetMacos_ThrowsPlatformSupportedException() - { - var process = new Process(); - Assert.Throws(() => process.MaxWorkingSet); - Assert.Throws(() => process.MaxWorkingSet = (IntPtr)1); + Assert.Throws(() => process.MaxWorkingSet = (IntPtr)1); } [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] @@ -659,21 +651,13 @@ public void TestMinWorkingSet() } [Fact] - [SkipOnPlatform(TestPlatforms.OSX | TestPlatforms.FreeBSD | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Getting MinWorkingSet is not supported on OSX, BSD, iOS, MacCatalyst, and tvOS.")] + [SkipOnPlatform(TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Getting MinWorkingSet is not supported on iOS, MacCatalyst, and tvOS.")] public void MinWorkingSet_GetNotStarted_ThrowsInvalidOperationException() { var process = new Process(); Assert.Throws(() => process.MinWorkingSet); } - [Fact] - [PlatformSpecific(TestPlatforms.OSX | TestPlatforms.FreeBSD)] - public void MinWorkingSet_GetMacos_ThrowsPlatformSupportedException() - { - var process = new Process(); - Assert.Throws(() => process.MinWorkingSet); - } - [Fact] public void TestModules() {