Bug 44741 - all my mono processes appear as "Main" on top and cant find using status myapp.exe
Summary: all my mono processes appear as "Main" on top and cant find using status myap...
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: 4.6.0 (C8)
Hardware: PC Linux
: Normal normal
Target Milestone: 4.8.0 (C9)
Assignee: Alex Rønne Petersen
Depends on:
Reported: 2016-09-26 14:47 UTC by Albert
Modified: 2017-02-17 16:53 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Albert 2016-09-26 14:47:37 UTC
After updating to mono 4.6 all my mono processes appear as "Main" on top:

mono 4.4:

 3686 root      20   0 1197m  73m  14m S  0.0  1.2   0:03.54 myapp.exe

mono 4.6:

27104 root      20   0 1201m  77m  14m S  0.0  1.0   0:03.40 Main

Also I cant use status to find my process and start/stop it on init scripts:

mono 4.4:

[root@localhost~]# status myapp.exe
myapp.exe (pid 3686) is running...

mono 4.6:

[root@localhost~]# status myapp.exe
myapp.exe is stopped
Comment 1 Jo Shields 2016-09-29 13:20:00 UTC
How are you launching your app?
Comment 2 Albert 2016-09-29 13:56:53 UTC
I am using "mono myapp.exe" or "./myapp.exe" and both get the same result on mono 4.6.

ps shows the right process:

# ps -aux | grep mono 

root      9647  6.2  2.0 1230436 79260 pts/0   Sl+  09:49   0:02 /usr/bin/mono /root/myapp/myapp.exe

top doesnt:

[root@localhost ~]# top -p 9647 -b
top - 09:50:13 up 9 min,  2 users,  load average: 0.12, 0.17, 0.09
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  4.2%us,  1.6%sy,  0.5%ni, 92.0%id,  1.7%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3924632k total,  1483956k used,  2440676k free,    21592k buffers
Swap:  2097148k total,        0k used,  2097148k free,  1099872k cached

 9809 root      20   0 1201m  77m  14m S  0.0  2.0   0:02.12 Main

Any idea? I cant upgrade my systems to this version as it breaks init scripts and also we are using monitoring solution that get metrics from processes and now is reporting my application  as "Main" instead of the right process name myapp.exe.
Comment 3 Albert 2016-10-13 23:24:41 UTC
Jo, any update on this? Do you know if this will be fixed in upcoming releases?

Comment 4 Jo Shields 2016-10-14 09:24:52 UTC
Sorry, this fell off my radar. I'll spend some time today investigating. I can confirm I see the same behaviour, though.
Comment 5 Jo Shields 2016-10-14 12:58:10 UTC
OK, tracked the change down to a899fef659765a57d3a8c601bef699ef67b2b335

Will discuss with AlexRP.
Comment 6 Jo Shields 2016-10-14 15:39:17 UTC
It's definitely intentional - each Mono thread has an explicitly set name, and top uses thread names rather than argv[0] where it exists. My understanding is the change to "Main" as a default is to make debugging nicer.
Comment 7 Albert 2016-10-14 15:50:12 UTC
Thanks Joe, but for end user this is confusing. 

If you are running different mono applications on the same server all of them will be listed as "Main" and you cant differentiate them to see wich one is consuming more resources or know what process to kill from top.

I have same problem with a remote monitoring software that get metrics from server processes and now all my mono applications will be reported as Main.

It is ok to show tread name if they have set, but if application hasnt set a thread name i think is better to show argv[0] than just "Main".

Meantime, how can i change the default thread name of my console application? I am not using or starting new threads just default Main method.
Comment 8 Alex Rønne Petersen 2016-10-14 16:13:31 UTC
The fix here should be to only set the managed name for the main thread, not the native one. I'll have a look.

As a workaround, you can do something like this from your main thread:

> using System;
> using System.IO;
> using System.Threading;
> static class Program {
>     static void Main () {
>         Thread.CurrentThread.Name = Path.GetFileName (Environment.GetCommandLineArgs () [0]);
>         // ...
>     }
> }

This will overwrite the thread name Mono sets at startup and should make it show up as `myapp.exe` in `top`.
Comment 10 Alex Rønne Petersen 2016-10-14 21:18:44 UTC
Fixed in master and mono-4.8.0-branch.