From a40327fe5e6a1933c01cdcf19038c2202c989e52 Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Tue, 24 Mar 2020 15:18:06 +0100 Subject: [PATCH 01/10] Add ThreadNameDoesNotAffectProcessName test --- .../System.Threading.Thread/tests/ThreadTests.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs index 017341ba856f2..3038c65423a71 100644 --- a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs +++ b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs @@ -647,6 +647,19 @@ public static void NameTest() }); } + [Fact] + 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.NotEqual(threadName, Process.GetCurrentProcess().ProcessName); + }).Dispose(); + } + [Fact] public static void PriorityTest() { From 19901e4db3fce25d66956705baf2a53526c6decc Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Thu, 26 Mar 2020 16:02:31 +0100 Subject: [PATCH 02/10] Assert Main Thread Name get returns set value. --- src/libraries/System.Threading.Thread/tests/ThreadTests.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs index 3038c65423a71..7e95dde288377 100644 --- a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs +++ b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs @@ -654,9 +654,10 @@ public static void ThreadNameDoesNotAffectProcessName() // To avoid that, .NET ignores requests to change the main thread name. RemoteExecutor.Invoke(() => { - const string threadName = "my-thread"; - Thread.CurrentThread.Name = threadName; - Assert.NotEqual(threadName, Process.GetCurrentProcess().ProcessName); + const string ThreadName = "my-thread"; + Thread.CurrentThread.Name = ThreadName; + Assert.Equal(ThreadName, Thread.CurrentThread.Name); + Assert.NotEqual(ThreadName, Process.GetCurrentProcess().ProcessName); }).Dispose(); } From 5dbbb48f27370354fddb189c7be4833d86d2a21d Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Fri, 27 Mar 2020 16:30:48 +0100 Subject: [PATCH 03/10] Ignore requests to set main thread name. --- src/coreclr/src/pal/src/thread/thread.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/coreclr/src/pal/src/thread/thread.cpp b/src/coreclr/src/pal/src/thread/thread.cpp index 1473601c51252..39240fd807ba6 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 (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); From eced0aba7fede4279a97f8bb6b738a2cde45d852 Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Mon, 30 Mar 2020 14:50:37 +0200 Subject: [PATCH 04/10] Fix GCC compilation --- src/coreclr/src/pal/src/thread/thread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/src/pal/src/thread/thread.cpp b/src/coreclr/src/pal/src/thread/thread.cpp index 39240fd807ba6..413e42b8c1067 100644 --- a/src/coreclr/src/pal/src/thread/thread.cpp +++ b/src/coreclr/src/pal/src/thread/thread.cpp @@ -1643,7 +1643,7 @@ CorUnix::InternalSetThreadDescription( // Ignore requests to set the main thread name because // it causes the value returned by Process.ProcessName to change. - if (pTargetThread->GetThreadId() == getpid()) + if ((pid_t)pTargetThread->GetThreadId() == getpid()) { goto InternalSetThreadDescriptionExit; } From 376bbe57adbef5fbe265359f016b26c4928e17a2 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 4 May 2020 18:18:21 -0700 Subject: [PATCH 05/10] fix mono --- src/mono/mono/utils/mono-threads-posix.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/mono/mono/utils/mono-threads-posix.c b/src/mono/mono/utils/mono-threads-posix.c index 33afc458a7928..62cdec0e1d69a 100644 --- a/src/mono/mono/utils/mono-threads-posix.c +++ b/src/mono/mono/utils/mono-threads-posix.c @@ -282,6 +282,13 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name) if (tid != mono_native_thread_id_get ()) return; + /* + * Ignore requests to set the main thread name because + * it causes the value returned by Process.ProcessName to change. + */ + if (tid == getpid()) + return; + if (!name) { pthread_setname_np (""); } else { From b5d9209728ffef53b7d5e7eda5a2257677cd3b06 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 4 May 2020 19:16:47 -0700 Subject: [PATCH 06/10] Revert "fix mono" This reverts commit 376bbe57adbef5fbe265359f016b26c4928e17a2. --- src/mono/mono/utils/mono-threads-posix.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/mono/mono/utils/mono-threads-posix.c b/src/mono/mono/utils/mono-threads-posix.c index 62cdec0e1d69a..33afc458a7928 100644 --- a/src/mono/mono/utils/mono-threads-posix.c +++ b/src/mono/mono/utils/mono-threads-posix.c @@ -282,13 +282,6 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name) if (tid != mono_native_thread_id_get ()) return; - /* - * Ignore requests to set the main thread name because - * it causes the value returned by Process.ProcessName to change. - */ - if (tid == getpid()) - return; - if (!name) { pthread_setname_np (""); } else { From cb4c518fdebbeac68ad8b08e2b0fce5b39979cb7 Mon Sep 17 00:00:00 2001 From: danmosemsft Date: Tue, 5 May 2020 13:17:46 -0700 Subject: [PATCH 07/10] attempt 2 --- src/mono/mono/utils/mono-threads-posix.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/mono/mono/utils/mono-threads-posix.c b/src/mono/mono/utils/mono-threads-posix.c index 33afc458a7928..9d6429e26ce12 100644 --- a/src/mono/mono/utils/mono-threads-posix.c +++ b/src/mono/mono/utils/mono-threads-posix.c @@ -282,6 +282,15 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name) if (tid != mono_native_thread_id_get ()) return; +#if defined(__linux__) + /* + * Ignore requests to set the main thread name because + * it causes the value returned by Process.ProcessName to change. + */ + if (tid == (guint64)getpid ()) + return; +#endif + if (!name) { pthread_setname_np (""); } else { From 3bc54484e282cfe9f5cf0ffc5da32c3d7ac8de31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksey=20Kliger=20=28=CE=BBgeek=29?= Date: Wed, 6 May 2020 14:20:52 -0400 Subject: [PATCH 08/10] [mono] (Linux) check native os thread id when setting thread name Changing the name of the main thread changes the name of the entire process, which we don't want. --- src/mono/mono/utils/mono-threads-posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/utils/mono-threads-posix.c b/src/mono/mono/utils/mono-threads-posix.c index 9d6429e26ce12..f8f21ca677008 100644 --- a/src/mono/mono/utils/mono-threads-posix.c +++ b/src/mono/mono/utils/mono-threads-posix.c @@ -287,7 +287,7 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name) * Ignore requests to set the main thread name because * it causes the value returned by Process.ProcessName to change. */ - if (tid == (guint64)getpid ()) + if (mono_native_thread_os_id_get () == (guint64)getpid ()) return; #endif From 034ce1ebbb057c893f7423a475a86bf03113efbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksey=20Kliger=20=28=CE=BBgeek=29?= Date: Wed, 6 May 2020 14:52:15 -0400 Subject: [PATCH 09/10] Disable ThreadNameDoesNotAffectProcessName on Mono until https://github.com/dotnet/runtime/issues/35908 is fixed --- src/libraries/System.Threading.Thread/tests/ThreadTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs index 7e95dde288377..f6e0e1e60a47e 100644 --- a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs +++ b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs @@ -648,6 +648,7 @@ 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. From d82c806067f2652c57ffc3a96632f1652b692794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksey=20Kliger=20=28=CE=BBgeek=29?= Date: Wed, 6 May 2020 14:52:30 -0400 Subject: [PATCH 10/10] Revert mono change --- src/mono/mono/utils/mono-threads-posix.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/mono/mono/utils/mono-threads-posix.c b/src/mono/mono/utils/mono-threads-posix.c index f8f21ca677008..33afc458a7928 100644 --- a/src/mono/mono/utils/mono-threads-posix.c +++ b/src/mono/mono/utils/mono-threads-posix.c @@ -282,15 +282,6 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name) if (tid != mono_native_thread_id_get ()) return; -#if defined(__linux__) - /* - * Ignore requests to set the main thread name because - * it causes the value returned by Process.ProcessName to change. - */ - if (mono_native_thread_os_id_get () == (guint64)getpid ()) - return; -#endif - if (!name) { pthread_setname_np (""); } else {