Skip to content

Commit

Permalink
Ensure Process.ProcessName doesn't change when setting Thread.Name on…
Browse files Browse the repository at this point in the history
… 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 <danmose@microsoft.com>
Co-authored-by: Aleksey Kliger (λgeek) <akliger@gmail.com>
  • Loading branch information
3 people authored May 12, 2020
1 parent 8271d53 commit b57a099
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/coreclr/src/pal/src/thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 15 additions & 0 deletions src/libraries/System.Threading.Thread/tests/ThreadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit b57a099

Please sign in to comment.