Skip to content

Commit

Permalink
Catch expected exception when reading info out of a PDB (#76182)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Dec 3, 2024
2 parents 6fe8938 + 9879a92 commit c988fa3
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/Features/Core/Portable/FeaturesResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3189,4 +3189,7 @@ Zero-width positive lookbehind assertions are typically used at the beginning of
<data name="interface_name" xml:space="preserve">
<value>&lt;interface name&gt;</value>
</data>
<data name="Unable_to_create_0" xml:space="preserve">
<value>Unable to create '{0}'</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.DecompiledSource;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.PdbSourceDocument;
Expand Down Expand Up @@ -171,34 +170,26 @@ internal sealed class DecompilationMetadataAsSourceFileProvider(IImplementationA
var timeout = TimeSpan.FromSeconds(5);
var firstAttempt = true;
var skipWritingFile = false;
while (!Directory.Exists(directoryToCreate))

while (!IOUtilities.PerformIO(() => Directory.Exists(directoryToCreate)))
{
if (stopwatch.Elapsed > TimeSpan.FromSeconds(5))
if (stopwatch.Elapsed > timeout)
{
// If we still can't create the folder after 5 seconds, assume we will not be able to create it.
skipWritingFile = true;
break;
}

try
if (firstAttempt)
{
if (firstAttempt)
{
firstAttempt = false;
}
else
{
await Task.Delay(DelayTimeSpan.Short, cancellationToken).ConfigureAwait(false);
}

Directory.CreateDirectory(directoryToCreate);
firstAttempt = false;
}
catch (DirectoryNotFoundException)
{
}
catch (UnauthorizedAccessException)
else
{
await Task.Delay(DelayTimeSpan.Short, cancellationToken).ConfigureAwait(false);
}

IOUtilities.PerformIO(() => Directory.CreateDirectory(directoryToCreate));
}

if (!skipWritingFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
Expand Down Expand Up @@ -178,9 +179,11 @@ internal sealed class PdbSourceDocumentMetadataAsSourceFileProvider(

ImmutableDictionary<string, string> pdbCompilationOptions;
ImmutableArray<SourceDocument> sourceDocuments;
// We know we have a DLL, call and see if we can find metadata readers for it, and for the PDB (whereever it may be)
using (var documentDebugInfoReader = await _pdbFileLocatorService.GetDocumentDebugInfoReaderAsync(dllPath, options.AlwaysUseDefaultSymbolServers, telemetryMessage, cancellationToken).ConfigureAwait(false))

try
{
// We know we have a DLL, call and see if we can find metadata readers for it, and for the PDB (wherever it may be)
using var documentDebugInfoReader = await _pdbFileLocatorService.GetDocumentDebugInfoReaderAsync(dllPath, options.AlwaysUseDefaultSymbolServers, telemetryMessage, cancellationToken).ConfigureAwait(false);
if (documentDebugInfoReader is null)
return null;

Expand All @@ -191,11 +194,17 @@ internal sealed class PdbSourceDocumentMetadataAsSourceFileProvider(

// Try to find some actual document information from the PDB
sourceDocuments = documentDebugInfoReader.FindSourceDocuments(handle);
if (sourceDocuments.Length == 0)
{
_logger?.Log(FeaturesResources.No_source_document_info_found_in_PDB);
return null;
}
}
catch (BadImageFormatException ex) when (FatalError.ReportAndCatch(ex))
{
_logger?.Log(ex.Message);
return null;
}

if (sourceDocuments.Length == 0)
{
_logger?.Log(FeaturesResources.No_source_document_info_found_in_PDB);
return null;
}

Encoding? defaultEncoding = null;
Expand Down Expand Up @@ -227,14 +236,18 @@ internal sealed class PdbSourceDocumentMetadataAsSourceFileProvider(
}

var tempFilePath = Path.Combine(tempPath, projectId.Id.ToString());

// Create the directory. It's possible a parallel deletion is happening in another process, so we may have
// to retry this a few times.
var loopCount = 0;
while (!Directory.Exists(tempFilePath))
{
// Protect against infinite loops.
if (loopCount++ > 10)
{
_logger?.Log(FeaturesResources.Unable_to_create_0, tempFilePath);
return null;
}

IOUtilities.PerformIO(() => Directory.CreateDirectory(tempFilePath));
}
Expand Down
5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c988fa3

Please sign in to comment.