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

Avoid Unhandled Exception on .NET 461 if the Registry Access threw an exception. #1101

Merged
merged 32 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f992746
ignore registry errors (would be nice to log it)
lucas-zimerman Jul 1, 2021
61b623c
remove changes.
lucas-zimerman Jul 1, 2021
ef99719
wip
lucas-zimerman Jul 2, 2021
903f92b
WIP
lucas-zimerman Jul 2, 2021
0b9fb5a
update changelog.
lucas-zimerman Jul 2, 2021
0002382
Merge branch 'main' into feat/opt-out-netfxinstalaltions
lucas-zimerman Jul 5, 2021
5140c39
Moved error handling flow for Registry reading
lucas-zimerman Jul 5, 2021
df81d98
const params
lucas-zimerman Jul 5, 2021
85d84dd
replaced tryopenregistry by Registry.LocalMachine.TryOpenSubKey
lucas-zimerman Jul 5, 2021
0cd35c6
Update src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs
lucas-zimerman Jul 5, 2021
2b46fca
wip
lucas-zimerman Jul 5, 2021
3b93b36
Changed exception flow + fixed changelog.
lucas-zimerman Jul 6, 2021
21590e3
removed unused return
lucas-zimerman Jul 6, 2021
7795ca3
fixed changelog and exception flow.
lucas-zimerman Jul 7, 2021
772063d
Revert manual test
lucas-zimerman Jul 7, 2021
a4efea1
Merge branch 'main' into feat/opt-out-netfxinstalaltions
lucas-zimerman Jul 7, 2021
bd31d7d
removed spaces.
lucas-zimerman Jul 7, 2021
4d345b4
Apply suggestions from code review
lucas-zimerman Jul 7, 2021
1590d4e
Merge branch 'feat/opt-out-netfxinstalaltions' of https://github.com/…
lucas-zimerman Jul 8, 2021
7eeed1b
Update src/Sentry/SentryOptions.cs
lucas-zimerman Jul 8, 2021
ae3d816
remove white line
lucas-zimerman Jul 8, 2021
04659b2
Merge branch 'feat/opt-out-netfxinstalaltions' of https://github.com/…
lucas-zimerman Jul 8, 2021
c1337a7
rollback hub try/catch on Register
lucas-zimerman Jul 8, 2021
3f9e65a
Merge branch 'main' into feat/opt-out-netfxinstalaltions
lucas-zimerman Jul 8, 2021
8930405
exception log on NetFxInstallationIntegration.
lucas-zimerman Jul 8, 2021
0f32c44
Merge branch 'feat/opt-out-netfxinstalaltions' of https://github.com/…
lucas-zimerman Jul 8, 2021
b07e2d0
remove unused using.
lucas-zimerman Jul 8, 2021
03c4929
removed PR from changelog.
lucas-zimerman Jul 8, 2021
8928c6e
Apply suggestions from code review
lucas-zimerman Jul 10, 2021
1e6cce5
Merge branch 'main' into feat/opt-out-netfxinstalaltions
bruno-garcia Jul 10, 2021
a15349b
fix StackTrageMode return
lucas-zimerman Jul 10, 2021
58a0cd7
Merge branch 'main' into feat/opt-out-netfxinstalaltions
bruno-garcia Jul 10, 2021
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Avoid Unhandled Exception on .NET 461 if the Registry Access threw an exception ([#1101](https://github.com/getsentry/sentry-dotnet/pull/1101))

## 3.6.1

### Fixes
Expand Down
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>
9 changes: 8 additions & 1 deletion src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ internal Hub(ISentryClient client, ISystemClock clock, ISessionManager sessionMa
foreach (var integration in _integrations)
{
options.DiagnosticLogger?.LogDebug("Registering integration: '{0}'.", integration.GetType().Name);
integration.Register(this, options);
try
{
integration.Register(this, options);
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception ex)
{
options.DiagnosticLogger?.LogError("Failed to Register integration: '{0}'.", ex, integration.GetType().Name);
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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
1 change: 1 addition & 0 deletions src/Sentry/PlatformAbstractions/RegistryKeyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NET461
using System;
using Microsoft.Win32;

namespace Sentry.PlatformAbstractions
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)
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
{
_runtime = RuntimeInfo.GetRuntime();
RuntimeInfo.SetAdditionalParameters(_runtime);
}
return _runtime;
}
}
/// <summary>
/// The name of the runtime
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion src/Sentry/PlatformAbstractions/RuntimeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ 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)
{
if (rawRuntimeDescription == null)
Expand Down
34 changes: 27 additions & 7 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,40 @@ public double TracesSampleRate
/// </remarks>
public Func<TransactionSamplingContext, double?>? TracesSampler { get; set; }

private StackTraceMode? _stackTraceMode = null;

/// <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 { } stackTraceMode)
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
{
return stackTraceMode;
}
try
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
{
// 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 StackTraceMode to {0} ", ex, StackTraceMode.Enhanced);
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
}
return _stackTraceMode.Value;
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
}
set => _stackTraceMode = value;
}

/// <summary>
/// Maximum allowed file size of attachments, in bytes.
Expand Down Expand Up @@ -513,12 +539,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