diff --git a/src/coreclr/src/pal/src/thread/thread.cpp b/src/coreclr/src/pal/src/thread/thread.cpp index a6a14c20f9ba8..36a523e28d120 100644 --- a/src/coreclr/src/pal/src/thread/thread.cpp +++ b/src/coreclr/src/pal/src/thread/thread.cpp @@ -1641,6 +1641,13 @@ CorUnix::InternalSetThreadDescription( pTargetThread->Lock(pThread); + // Ignore requests to set the main thread name because + // it causes the value returned by Process.ProcessName to change. + if ((pid_t)pTargetThread->GetThreadId() == getpid()) + { + goto InternalSetThreadDescriptionExit; + } + /* translate the wide char lpThreadDescription string to multibyte string */ nameSize = WideCharToMultiByte(CP_ACP, 0, lpThreadDescription, -1, NULL, 0, NULL, NULL); diff --git a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs index 846602b863b3b..797629521a807 100644 --- a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs +++ b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs @@ -651,6 +651,21 @@ public static void NameTest() }); } + [Fact] + [ActiveIssue ("https://github.com/dotnet/runtime/issues/35908", TestRuntimes.Mono)] + public static void ThreadNameDoesNotAffectProcessName() + { + // On Linux, changing the main thread name affects ProcessName. + // To avoid that, .NET ignores requests to change the main thread name. + RemoteExecutor.Invoke(() => + { + const string ThreadName = "my-thread"; + Thread.CurrentThread.Name = ThreadName; + Assert.Equal(ThreadName, Thread.CurrentThread.Name); + Assert.NotEqual(ThreadName, Process.GetCurrentProcess().ProcessName); + }).Dispose(); + } + [Fact] public static void PriorityTest() {