Skip to content

Commit 1586391

Browse files
rcj1Rachel Jarvinoahfalk
authored
Part 1: adding better diagnostic messages for too-long TMPDIR on Linux (#5517)
Closes dotnet/runtime#111165 This PR adds better diagnostic messages in the scenario of too-long TMPDIR values on Linux that cause the diagnostic socket to be absent or incorrectly named. --------- Co-authored-by: Rachel Jarvi <[email protected]> Co-authored-by: Noah Falk <[email protected]>
1 parent 220408e commit 1586391

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsIpc/IpcTransport.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,13 @@ internal class PidIpcEndpoint : IpcEndpoint
239239
{
240240
public static string IpcRootPath { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"\\.\pipe\" : Path.GetTempPath();
241241
public static string DiagnosticsPortPattern { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"^dotnet-diagnostic-(\d+)$" : @"^dotnet-diagnostic-(\d+)-(\d+)-socket$";
242-
242+
// Format strings as private const members
243+
private const string _defaultAddressFormatWindows = "dotnet-diagnostic-{0}";
244+
private const string _dsrouterAddressFormatWindows = "dotnet-diagnostic-dsrouter-{0}";
245+
private const string _defaultAddressFormatNonWindows = "dotnet-diagnostic-{0}-{1}-socket";
246+
private const string _dsrouterAddressFormatNonWindows = "dotnet-diagnostic-dsrouter-{0}-{1}-socket";
243247
private int _pid;
244248
private IpcEndpointConfig _config;
245-
246249
/// <summary>
247250
/// Creates a reference to a .NET process's IPC Transport
248251
/// using the default rules for a given pid
@@ -289,11 +292,11 @@ private static bool TryGetDefaultAddress(int pid, out string defaultAddress)
289292

290293
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
291294
{
292-
defaultAddress = $"dotnet-diagnostic-{pid}";
295+
defaultAddress = string.Format(_defaultAddressFormatWindows, pid);
293296

294297
try
295298
{
296-
string dsrouterAddress = Directory.GetFiles(IpcRootPath, $"dotnet-diagnostic-dsrouter-{pid}").FirstOrDefault();
299+
string dsrouterAddress = Directory.GetFiles(IpcRootPath, string.Format(_dsrouterAddressFormatWindows, pid)).FirstOrDefault();
297300
if (!string.IsNullOrEmpty(dsrouterAddress))
298301
{
299302
defaultAddress = dsrouterAddress;
@@ -305,11 +308,11 @@ private static bool TryGetDefaultAddress(int pid, out string defaultAddress)
305308
{
306309
try
307310
{
308-
defaultAddress = Directory.GetFiles(IpcRootPath, $"dotnet-diagnostic-{pid}-*-socket") // Try best match.
311+
defaultAddress = Directory.GetFiles(IpcRootPath, string.Format(_defaultAddressFormatNonWindows, pid, "*")) // Try best match.
309312
.OrderByDescending(f => new FileInfo(f).LastWriteTime)
310313
.FirstOrDefault();
311314

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

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

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

0 commit comments

Comments
 (0)