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

Don't run sentry-dotnet with multiple app instances at the same time (desktop) #643

Merged
merged 6 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- [diff](https://github.com/getsentry/sentry-dotnet/compare/3.14.1...3.15.0)
- Windows - include sentry.dll & .pdb in debug symbol upload ([#641](https://github.com/getsentry/sentry-unity/pull/641))

### Fixes

- Windows - avoid sentry-dotnet issue when multiple app instances try to access the same cache directory ([#643](https://github.com/getsentry/sentry-unity/pull/643))

## 0.12.0

### Features
Expand Down
54 changes: 46 additions & 8 deletions src/Sentry.Unity/SentryUnity.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.IO;
using System.ComponentModel;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using UnityEngine;

namespace Sentry.Unity
{
Expand All @@ -10,6 +12,8 @@ namespace Sentry.Unity
/// </summary>
public static class SentryUnity
{
private static FileStream? _lockFile;

/// <summary>
/// Initializes Sentry Unity SDK while configuring the options.
/// </summary>
Expand All @@ -24,19 +28,53 @@ public static void Init(Action<SentryUnityOptions> sentryUnityOptionsConfigure)
/// <summary>
/// Initializes Sentry Unity SDK while providing an options object.
/// </summary>
/// <param name="sentryUnityOptions">The options object.</param>
/// <param name="options">The options object.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public static void Init(SentryUnityOptions sentryUnityOptions)
public static void Init(SentryUnityOptions options)
{
SentryOptionsUtility.TryAttachLogger(sentryUnityOptions);
if (sentryUnityOptions.ShouldInitializeSdk())
SentryOptionsUtility.TryAttachLogger(options);
if (options.ShouldInitializeSdk())
{
sentryUnityOptions.DiagnosticLogger?.LogDebug(sentryUnityOptions.ToString());
var sentryDotNet = SentrySdk.Init(sentryUnityOptions);
// On Standalone, we disable cache dir in case multiple app instances run over the same path.
// Note: we cannot use a named Mutex, because Unit doesn't support it. Instead, we create a file with `FileShare.None`.
// https://forum.unity.com/threads/unsupported-internal-call-for-il2cpp-mutex-createmutex_internal-named-mutexes-are-not-supported.387334/
if (ApplicationAdapter.Instance.Platform == RuntimePlatform.WindowsPlayer && options.CacheDirectoryPath != null)
vaind marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
_lockFile = new FileStream(Path.Combine(options.CacheDirectoryPath, "sentry-unity.lock"), FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None);
}
catch (Exception ex)
{
options.DiagnosticLogger?.LogWarning("An exception was thrown while trying to " +
"acquire a lockfile on the config directory: .NET event cache will be disabled.", ex);
options.CacheDirectoryPath = null;
options.AutoSessionTracking = false;
}
}

var sentryDotNet = SentrySdk.Init(options);
ApplicationAdapter.Instance.Quitting += () =>
{
sentryUnityOptions.DiagnosticLogger?.LogDebug("Closing the sentry-dotnet SDK");
sentryDotNet.Dispose();
options.DiagnosticLogger?.LogDebug("Closing the sentry-dotnet SDK");
try
{
sentryDotNet.Dispose();
}
finally
{
try
{
// We don't really need to close, Windows would release the lock anyway, but let's be nice.
_lockFile?.Close();
}
catch (Exception ex)
{
options.DiagnosticLogger?.Log(SentryLevel.Warning,
"Exception while releasing the lockfile on the config directory.", ex);
}
}
};
}
}
Expand Down