Skip to content

Commit

Permalink
Avoid Unhandled Exception on .NET 461 if the Registry Access threw an…
Browse files Browse the repository at this point in the history
… exception. (#1101)

* ignore registry errors (would be nice to log it)

* remove changes.

* wip

* WIP

* update changelog.

* Moved error handling flow for Registry reading

* const params

* replaced tryopenregistry by Registry.LocalMachine.TryOpenSubKey

* Update src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs

Co-authored-by: Alexey Golub <tyrrrrrr@gmail.com>

* wip

* Changed exception flow + fixed changelog.

* removed unused return

* fixed changelog and exception flow.

* Revert manual test

* removed spaces.

* Apply suggestions from code review

Co-authored-by: Alexey Golub <tyrrrrrr@gmail.com>

* Update src/Sentry/SentryOptions.cs

Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>

* remove white line

* rollback hub try/catch on Register

* exception log on NetFxInstallationIntegration.

* remove unused using.

* removed PR from changelog.

* Apply suggestions from code review

Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>

* fix StackTrageMode return

Co-authored-by: Alexey Golub <tyrrrrrr@gmail.com>
Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>
  • Loading branch information
3 people authored Jul 10, 2021
1 parent 733245e commit b496520
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
<OutputType>Exe</OutputType>
<!--Version is detected by default as the application release and sent to sentry-->
<Version>9.8.7</Version>
<TargetFramework>net5.0</TargetFramework>
<TargetFrameworks>net5.0;netcoreapp2.1;net461</TargetFrameworks>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../src/Sentry/Sentry.csproj" />
</ItemGroup>

</Project>
13 changes: 11 additions & 2 deletions src/Sentry/Integrations/NetFxInstallationsIntegration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if NET461
using System;
using Sentry.Extensibility;
using Sentry.PlatformAbstractions;

namespace Sentry.Integrations
Expand All @@ -7,9 +9,16 @@ internal class NetFxInstallationsIntegration : ISdkIntegration
{
public void Register(IHub hub, SentryOptions options)
{
if (!Runtime.Current.IsMono())
try
{
options.AddEventProcessor(new NetFxInstallationsEventProcessor(options));
if (!Runtime.Current.IsMono())
{
options.AddEventProcessor(new NetFxInstallationsEventProcessor(options));
}
}
catch (Exception ex)
{
options.DiagnosticLogger?.LogError("Failed to register NetFxInstallations.", ex);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Sentry.PlatformAbstractions
/// </summary>
public static partial class FrameworkInfo
{
internal const string NetFxNdpRegistryKey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\";
internal const string NetFxNdpFullRegistryKey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
/// <summary>
/// Get the latest Framework installation for the specified CLR
/// </summary>
Expand Down Expand Up @@ -85,8 +87,7 @@ public static partial class FrameworkInfo
/// <returns>Enumeration of installations</returns>
public static IEnumerable<FrameworkInstallation> GetInstallations()
{
using var ndpKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, string.Empty)
.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\");
using var ndpKey = Registry.LocalMachine.OpenSubKey(NetFxNdpRegistryKey, false);
if (ndpKey == null)
{
yield break;
Expand Down Expand Up @@ -160,15 +161,14 @@ private static FrameworkInstallation GetFromV4(RegistryKey subKey, string subKey
},
Version = version,
ServicePack = subKey.GetInt("SP"),
Release = hasRelease ? release : null as int?
Release = hasRelease ? release : null
};
}

// https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#to-find-net-framework-versions-by-querying-the-registry-in-code-net-framework-45-and-later
internal static int? Get45PlusLatestInstallationFromRegistry()
{
using var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\");
using var ndpKey = Registry.LocalMachine.OpenSubKey(NetFxNdpFullRegistryKey, false);
return ndpKey?.GetInt("Release");
}

Expand Down
13 changes: 12 additions & 1 deletion src/Sentry/PlatformAbstractions/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ public class Runtime
/// <value>
/// The current runtime.
/// </value>
public static Runtime Current => _runtime ??= RuntimeInfo.GetRuntime();
public static Runtime Current
{
get
{
if (_runtime is null)
{
_runtime = RuntimeInfo.GetRuntime();
RuntimeInfo.SetAdditionalParameters(_runtime);
}
return _runtime;
}
}
/// <summary>
/// The name of the runtime
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion src/Sentry/PlatformAbstractions/RuntimeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ internal static Runtime GetRuntime()
runtime ??= GetFromMonoRuntime();

runtime ??= GetFromEnvironmentVariable();
return runtime;
}

internal static void SetAdditionalParameters(Runtime runtime)
{
#if NET461
SetNetFxReleaseAndVersion(runtime);
#else
SetNetCoreVersion(runtime);
#endif
return runtime;
}

internal static Runtime? Parse(string rawRuntimeDescription, string? name = null)
Expand Down
36 changes: 29 additions & 7 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,42 @@ public double TracesSampleRate
/// </remarks>
public Func<TransactionSamplingContext, double?>? TracesSampler { get; set; }

private StackTraceMode? _stackTraceMode;

/// <summary>
/// ATTENTION: This option will change how issues are grouped in Sentry!
/// </summary>
/// <remarks>
/// Sentry groups events by stack traces. If you change this mode and you have thousands of groups,
/// you'll get thousands of new groups. So use this setting with care.
/// </remarks>
public StackTraceMode StackTraceMode { get; set; }
public StackTraceMode StackTraceMode
{
get
{
if (_stackTraceMode is not null)
{
return _stackTraceMode.Value;
}

try
{
// from 3.0.0 uses Enhanced (Ben.Demystifier) by default which is a breaking change
// unless you are using .NET Native which isn't compatible with Ben.Demystifier.
_stackTraceMode = Runtime.Current.Name == ".NET Native"
? StackTraceMode.Original
: StackTraceMode.Enhanced;
}
catch (Exception ex)
{
_stackTraceMode = StackTraceMode.Enhanced;
DiagnosticLogger?.LogError("Failed to get runtime, setting {0} to {1} ", ex, nameof(StackTraceMode), _stackTraceMode);
}

return _stackTraceMode.Value;
}
set => _stackTraceMode = value;
}

/// <summary>
/// Maximum allowed file size of attachments, in bytes.
Expand Down Expand Up @@ -513,12 +541,6 @@ public double TracesSampleRate
/// </summary>
public SentryOptions()
{
// from 3.0.0 uses Enhanced (Ben.Demystifier) by default which is a breaking change
// unless you are using .NET Native which isn't compatible with Ben.Demystifier.
StackTraceMode = Runtime.Current.Name == ".NET Native"
? StackTraceMode.Original
: StackTraceMode.Enhanced;

EventProcessorsProviders = new Func<IEnumerable<ISentryEventProcessor>>[] {
() => EventProcessors ?? Enumerable.Empty<ISentryEventProcessor>()
};
Expand Down

0 comments on commit b496520

Please sign in to comment.