Skip to content

Part 1: adding better diagnostic messages for too-long TMPDIR on Linux #5517

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

Merged
merged 11 commits into from
Jul 15, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,13 @@ internal class PidIpcEndpoint : IpcEndpoint
{
public static string IpcRootPath { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"\\.\pipe\" : Path.GetTempPath();
public static string DiagnosticsPortPattern { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"^dotnet-diagnostic-(\d+)$" : @"^dotnet-diagnostic-(\d+)-(\d+)-socket$";

// Format strings as private const members
private const string _defaultAddressFormatWindows = "dotnet-diagnostic-{0}";
private const string _dsrouterAddressFormatWindows = "dotnet-diagnostic-dsrouter-{0}";
private const string _defaultAddressFormatNonWindows = "dotnet-diagnostic-{0}-{1}-socket";
private const string _dsrouterAddressFormatNonWindows = "dotnet-diagnostic-dsrouter-{0}-{1}-socket";
private int _pid;
private IpcEndpointConfig _config;

/// <summary>
/// Creates a reference to a .NET process's IPC Transport
/// using the default rules for a given pid
Expand Down Expand Up @@ -271,11 +274,11 @@ private static bool TryGetDefaultAddress(int pid, out string defaultAddress)

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
defaultAddress = $"dotnet-diagnostic-{pid}";
defaultAddress = string.Format(_defaultAddressFormatWindows, pid);

try
{
string dsrouterAddress = Directory.GetFiles(IpcRootPath, $"dotnet-diagnostic-dsrouter-{pid}").FirstOrDefault();
string dsrouterAddress = Directory.GetFiles(IpcRootPath, string.Format(_dsrouterAddressFormatWindows, pid)).FirstOrDefault();
if (!string.IsNullOrEmpty(dsrouterAddress))
{
defaultAddress = dsrouterAddress;
Expand All @@ -287,11 +290,11 @@ private static bool TryGetDefaultAddress(int pid, out string defaultAddress)
{
try
{
defaultAddress = Directory.GetFiles(IpcRootPath, $"dotnet-diagnostic-{pid}-*-socket") // Try best match.
defaultAddress = Directory.GetFiles(IpcRootPath, string.Format(_defaultAddressFormatNonWindows, pid, "*")) // Try best match.
.OrderByDescending(f => new FileInfo(f).LastWriteTime)
.FirstOrDefault();

string dsrouterAddress = Directory.GetFiles(IpcRootPath, $"dotnet-diagnostic-dsrouter-{pid}-*-socket") // Try best match.
string dsrouterAddress = Directory.GetFiles(IpcRootPath, string.Format(_dsrouterAddressFormatNonWindows, pid, "*")) // Try best match.
.OrderByDescending(f => new FileInfo(f).LastWriteTime)
.FirstOrDefault();

Expand Down Expand Up @@ -332,8 +335,15 @@ public static string GetDefaultAddress(int pid)
string msg = $"Unable to connect to Process {pid}.";
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
int total_length = IpcRootPath.Length + string.Format(_defaultAddressFormatNonWindows, pid, "##########").Length;
if (total_length > 108) // This isn't perfect as we don't know the disambiguation key length. However it should catch most cases.
{
msg += "The total length of the diagnostic socket path may exceed 108 characters. " +
"Try setting the TMPDIR environment variable to a shorter path";
}
msg += $" Please verify that {IpcRootPath} is writable by the current user. "
+ "If the target process has environment variable TMPDIR set, please set TMPDIR to the same directory. "
+ "Please also ensure that the target process has {TMPDIR}/dotnet-diagnostic-{pid}-{disambiguation_key}-socket shorter than 108 characters. "
+ "Please see https://aka.ms/dotnet-diagnostics-port for more information";
}
throw new ServerNotAvailableException(msg);
Expand All @@ -349,7 +359,7 @@ public static bool IsDefaultAddressDSRouter(int pid, string address)
address = address.Substring(IpcRootPath.Length);
}

string dsrouterAddress = $"dotnet-diagnostic-dsrouter-{pid}";
string dsrouterAddress = string.Format(_dsrouterAddressFormatWindows, pid);
return address.StartsWith(dsrouterAddress, StringComparison.OrdinalIgnoreCase);
}

Expand Down