Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting Thread.Name before Start() not working on macOS #106464

Closed
NinoFloris opened this issue Aug 15, 2024 · 1 comment · Fixed by #114249
Closed

Setting Thread.Name before Start() not working on macOS #106464

NinoFloris opened this issue Aug 15, 2024 · 1 comment · Fixed by #114249
Assignees
Milestone

Comments

@NinoFloris
Copy link
Contributor

NinoFloris commented Aug 15, 2024

Description

According to thread.cpp on macOS Thread.Name changes can only be done from the calling thread, which is fine. However this means that the fairly common pattern of newing up a thread with a descriptive name doesn't work as expected.

Observing certain dotnet threads vs rust etc. is more tedious as the name is entirely absent, requiring manual stack inspection and making a guess. A good example is the I/O threads:

var thread = new Thread(static s => ((SocketAsyncEngine)s!).EventLoop())
{
IsBackground = true,
Name = ".NET Sockets"
};
which show up unnamed when doing a simple process sampling.

The workaround is to move the name initialization into the start callback, but it seems the proper fix would be for the macOS PAL to set the name (if any) before calling into the start callback.

Reproduction Steps

var mres = new ManualResetEventSlim();
var thread = new Thread(() =>
{
    // This works
    // Thread.CurrentThread.Name = "Test thread name";
    mres.Wait();
}) { Name = "Test thread name " }; // this doesn't
thread.Start();
while (!Debugger.IsAttached)
    Thread.Sleep(1000);

Run this code in a console app and check the thread names. Easiest way to do so (unfortunately) is to open Activity Monitor -> select your process -> click the (...) ∨ button near the CPU | Memory | etc tab bar in the top, and click "Sample Process" to get a list of threads and stacks.

Expected behavior

new Thread { Name = "Test thread name" } correctly shows "Test thread name" in the sampled threads.

Actual behavior

new Thread { Name = "Test thread name" } does not show the name in the sampled threads, instead it has to be set inside the thread before it takes effect.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Aug 15, 2024
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

@mangod9 mangod9 removed the untriaged New issue has not been triaged by the area owner label Aug 28, 2024
@mangod9 mangod9 added this to the Future milestone Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants