Skip to content

Commit b57a099

Browse files
tmdsdanmoseleylambdageek
authored
Ensure Process.ProcessName doesn't change when setting Thread.Name on Linux (#34064)
Fixes #33673 This issue is a side-effect of adding support for setting thread names on Linux in dotnet/coreclr#27182. Co-authored-by: Dan Moseley <[email protected]> Co-authored-by: Aleksey Kliger (λgeek) <[email protected]>
1 parent 8271d53 commit b57a099

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/coreclr/src/pal/src/thread/thread.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,13 @@ CorUnix::InternalSetThreadDescription(
16411641

16421642
pTargetThread->Lock(pThread);
16431643

1644+
// Ignore requests to set the main thread name because
1645+
// it causes the value returned by Process.ProcessName to change.
1646+
if ((pid_t)pTargetThread->GetThreadId() == getpid())
1647+
{
1648+
goto InternalSetThreadDescriptionExit;
1649+
}
1650+
16441651
/* translate the wide char lpThreadDescription string to multibyte string */
16451652
nameSize = WideCharToMultiByte(CP_ACP, 0, lpThreadDescription, -1, NULL, 0, NULL, NULL);
16461653

src/libraries/System.Threading.Thread/tests/ThreadTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,21 @@ public static void NameTest()
651651
});
652652
}
653653

654+
[Fact]
655+
[ActiveIssue ("https://github.com/dotnet/runtime/issues/35908", TestRuntimes.Mono)]
656+
public static void ThreadNameDoesNotAffectProcessName()
657+
{
658+
// On Linux, changing the main thread name affects ProcessName.
659+
// To avoid that, .NET ignores requests to change the main thread name.
660+
RemoteExecutor.Invoke(() =>
661+
{
662+
const string ThreadName = "my-thread";
663+
Thread.CurrentThread.Name = ThreadName;
664+
Assert.Equal(ThreadName, Thread.CurrentThread.Name);
665+
Assert.NotEqual(ThreadName, Process.GetCurrentProcess().ProcessName);
666+
}).Dispose();
667+
}
668+
654669
[Fact]
655670
public static void PriorityTest()
656671
{

0 commit comments

Comments
 (0)