You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Diagnostics.Process.GetProcesses() returns truncated 15 char string on macOS #52860
When using Process.GetProcesses() on macOS in dotnet core it returns process names as strings max 15 chars. It truncates the process names to be 15 char long.
To whomever grabs this. The sysctrl interface in macOS does not support giving longer paths. You need the libproc interface instead. Luckily, the interop is already implemented in .NET Runtime.
That means, to fix this issue, it is a simple matter of rewriting the line here, to use the libproc interop.
For anyone with this problem, a workaround is to call Process.GetCurrentProcess().MainModule.FileName instead. Something like this:
string?fileName=currentProcess.MainModule?.FileName;if(string.IsNullOrEmpty(fileName))thrownewInvalidOperationException("Unable to obtain process name");char[]split={Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar};intidx=fileName.LastIndexOfAny(split);stringprocessName=fileName.Substring(idx+1);
Is there anything blocking the transition to using the main module's FileName property in the implementation of Process.Name and Process.GetProcessesByName?
Given that even the ps command extracts the long name from the main module's path I believe it's the right thing to do here as well.
That would work only for current process right @sonodima? (e.g. it would not work for all processes in the system)
Even that would be good improvement.
For anyone with this problem, a workaround is to call Process.GetCurrentProcess().MainModule.FileName instead. Something like this:
string?fileName=currentProcess.MainModule?.FileName;if(string.IsNullOrEmpty(fileName))thrownewInvalidOperationException("Unable to obtain process name");char[]split={Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar};intidx=fileName.LastIndexOfAny(split);stringprocessName=fileName.Substring(idx+1);
This produces the same result: Path.GetFileName(Environment.ProcessPath) (.NET 6+).
That would work only for current process right @sonodima? (e.g. it would not work for all processes in the system) Even that would be good improvement.
This should work on all the processes. I wrote a similar implementation some time ago in another project's process abstraction, and using the filename was still better behaviour than using the truncated name.
Moreover, in these tests using the path appears to work well even on system processes which would otherwise require sudo permissions.
In theory for GetProcessesByName we would need to modify the CreateProcessInfo function in ProcessManager.OSX.cs and use Path.GetFileName(GetProcPath(pid)) instead of temp.pbsd.pbi_comm
Reacted by Tomas Weinfurt
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
wfurt commentedon May 19, 2021
I think that comes from MAXCOMLEN limit on kinfo_proc. I think we would need to find different way than
Genbox commentedon Jul 6, 2022
To whomever grabs this. The sysctrl interface in macOS does not support giving longer paths. You need the libproc interface instead. Luckily, the interop is already implemented in .NET Runtime.
That means, to fix this issue, it is a simple matter of rewriting the line here, to use the libproc interop.
Genbox commentedon Jul 6, 2022
For anyone with this problem, a workaround is to call
Process.GetCurrentProcess().MainModule.FileNameinstead. Something like this:sonodima commentedon Dec 22, 2023
Is there anything blocking the transition to using the main module's
FileNameproperty in the implementation ofProcess.NameandProcess.GetProcessesByName?Given that even the ps command extracts the long name from the main module's path I believe it's the right thing to do here as well.
danmoseley commentedon Dec 22, 2023
@sonodima I think this issue is available if you want to give it a try.
wfurt commentedon Dec 22, 2023
That would work only for current process right @sonodima? (e.g. it would not work for all processes in the system)
Even that would be good improvement.
am11 commentedon Dec 22, 2023
This produces the same result:
Path.GetFileName(Environment.ProcessPath)(.NET 6+).sonodima commentedon Dec 22, 2023
This should work on all the processes. I wrote a similar implementation some time ago in another project's process abstraction, and using the filename was still better behaviour than using the truncated name.
Moreover, in these tests using the path appears to work well even on system processes which would otherwise require sudo permissions.
In theory for
GetProcessesByNamewe would need to modify theCreateProcessInfofunction inProcessManager.OSX.csand usePath.GetFileName(GetProcPath(pid))instead oftemp.pbsd.pbi_comm