Skip to content
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

Catch expected exception when reading info out of a PDB #76182

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateDirectory

It doesn't look like IOUtilities.IsNormalIOException includes DirectoryNotFoundException

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lies.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, cyrus pointed out to me it does.

}

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.

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

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

Loading