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 1 commit
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
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

### Fixes

- Avoid Unhandled Exception on .NET 461 if the Registry Access threw an exception([#1101](https://github.com/getsentry/sentry-dotnet/pull/1101))
- `IHub.ResumeSession()`: don't start a new session if pause wasn't called or if there is no active session ([#1089](https://github.com/getsentry/sentry-dotnet/pull/1089))
- Fixed incorrect order when getting the last active span ([#1094](https://github.com/getsentry/sentry-dotnet/pull/1094))
- Fix logger call in BackgroundWorker that caused a formatting exception in runtime ([#1092](https://github.com/getsentry/sentry-dotnet/pull/1092))
Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.Console.Basic/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Sentry;

using (SentrySdk.Init("https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537"))
using (SentrySdk.Init(o=> { o.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537"; o.Debug = true; o.DiagnosticLevel = SentryLevel.Debug; }))
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
{
// The following exception is captured and sent to Sentry
throw null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
<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
14 changes: 9 additions & 5 deletions src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#if NET461
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Win32;

namespace Sentry.PlatformAbstractions
Expand All @@ -23,7 +22,7 @@ public static partial class FrameworkInfo
/// CLR 4 => .NET 4.0, 4.5.x, 4.6.x, 4.7.x
/// </remarks>
/// <param name="clrVersion">The CLR version: 1, 2 or 4</param>
/// <returns>The framework installation or null if none is found, used for fine-tuning the Latest info.</returns>
/// <returns>The framework installation or null if none is found.</returns>
public static FrameworkInstallation? GetLatest(int clrVersion)
{
// CLR versions
Expand Down Expand Up @@ -87,8 +86,10 @@ public static partial class FrameworkInfo
/// <seealso href="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-1-4"/>
/// <returns>Enumeration of installations</returns>
public static IEnumerable<FrameworkInstallation> GetInstallations()
{
using var ndpKey = Registry.LocalMachine.TryOpenLocalSubKey(NetFxNdpRegistryKey);
{
int i = 0; if (i == 0)
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
throw new Exception("Test");
using var ndpKey = Registry.LocalMachine.OpenSubKey(NetFxNdpRegistryKey, false);
if (ndpKey == null)
{
yield break;
Expand Down Expand Up @@ -169,7 +170,10 @@ private static FrameworkInstallation GetFromV4(RegistryKey subKey, string subKey
// 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 = Registry.LocalMachine.TryOpenLocalSubKey(NetFxNdpFullRegistryKey);
int i = 0;
if (i == 0)
throw new Exception("Test");
using var ndpKey = Registry.LocalMachine.OpenSubKey(NetFxNdpFullRegistryKey, false);
return ndpKey?.GetInt("Release");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading;
using Microsoft.Win32;
using Sentry.Extensibility;

namespace Sentry.PlatformAbstractions
Expand All @@ -25,13 +23,7 @@ internal class NetFxInstallationsEventProcessor : ISentryEventProcessor
internal static Dictionary<string, string> GetInstallationsDictionary()
{
var versionsDictionary = new Dictionary<string, string>();
//We need access to the Registry to be able to collect the Installations.
//Without it, this Event processor should be disabled.
if (Registry.LocalMachine.GetLastException() is { } exception)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}
var installations = FrameworkInfo.GetInstallations();
var installations = FrameworkInfo.GetInstallations().ToArray();
foreach (var profile in installations.Select(p => p.Profile).Distinct())
{
versionsDictionary.Add($"{NetFxInstallationsKey} {profile}",
Expand Down
18 changes: 0 additions & 18 deletions src/Sentry/PlatformAbstractions/RegistryKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@ internal static class RegistryKeyExtensions

public static int? GetInt(this RegistryKey key, string value)
=> (int?)key.GetValue(value);

private static Exception? _lastException { get; set; }

internal static Exception? GetLastException(this RegistryKey _)
=> _lastException;

internal static RegistryKey? TryOpenLocalSubKey(this RegistryKey key, string path)
{
try
{
return _lastException != null ? null : key.OpenSubKey(path, false);
}
catch (Exception ex)
{
_lastException = ex;
return null;
}
}
}
}
#endif
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
11 changes: 8 additions & 3 deletions 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 Expand Up @@ -60,14 +65,14 @@ internal static void SetNetFxReleaseAndVersion(Runtime runtime)
var latest = FrameworkInfo.GetLatest(Environment.Version.Major);

runtime.FrameworkInstallation = latest;
if (latest?.Version?.Major < 4)
if (latest.Version?.Major < 4)
{
// prior to 4, user-friendly versions are always 2 digit: 1.0, 1.1, 2.0, 3.0, 3.5
runtime.Version = latest.ServicePack == null
? $"{latest.Version.Major}.{latest.Version.Minor}"
: $"{latest.Version.Major}.{latest.Version.Minor} SP {latest.ServicePack}";
}
else if (latest != null)
else
{
runtime.Version = latest.Version?.ToString();
}
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