Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
83a3c75
Implement process and instance isolation
jamescrosswell Sep 5, 2025
5e2c12e
Experimenting with named mutexes and semaphores
jamescrosswell Sep 5, 2025
7382cac
Format code
getsentry-bot Sep 5, 2025
ec60ca1
Update CachingTransport.cs
jamescrosswell Sep 7, 2025
dcec5e0
Update CacheDirectoryCoordinator.cs
jamescrosswell Sep 7, 2025
220884b
Switched to a file lock
jamescrosswell Sep 8, 2025
180ff53
Update Sentry.csproj
jamescrosswell Sep 8, 2025
03c998c
Format code
getsentry-bot Sep 8, 2025
71ed31b
Fixed tests
jamescrosswell Sep 8, 2025
d5dc55a
Update CachingTransportTests.cs
jamescrosswell Sep 8, 2025
3ae4d96
Merge branch 'isolated-cache' of https://github.com/getsentry/sentry-…
jamescrosswell Sep 8, 2025
0046933
Update CHANGELOG.md
jamescrosswell Sep 8, 2025
d6dd817
.
jamescrosswell Sep 8, 2025
90ede6b
Fixed mobile tests
jamescrosswell Sep 8, 2025
c26c5bd
Format code
getsentry-bot Sep 8, 2025
39be526
Windows tests
getsentry-bot Sep 9, 2025
c85161f
Create CacheDirectoryCoordinatorTests.cs
jamescrosswell Sep 9, 2025
536d9e2
Update SentryOptionsTests.cs
jamescrosswell Sep 9, 2025
477361b
Merge branch 'isolated-cache' of https://github.com/getsentry/sentry-…
jamescrosswell Sep 9, 2025
ee30f23
Added tests for SalvageAbandonedCacheSessions
jamescrosswell Sep 9, 2025
cd0848d
Update CacheDirectoryCoordinatorTests.cs
jamescrosswell Sep 9, 2025
b763698
Merge branch 'main' into isolated-cache
jamescrosswell Sep 11, 2025
a7b0656
Update CHANGELOG.md
jamescrosswell Sep 11, 2025
78f1a3a
Merge branch 'main' into isolated-cache
jamescrosswell Sep 19, 2025
29fa252
Update CHANGELOG.md
jamescrosswell Sep 19, 2025
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 @@ -2,6 +2,10 @@

## Unreleased

### Features

- Implement process and instance isolation so that multiple instances of the Sentry SDK can be instantiated inside the same process when using the Caching Transport ([#4498](https://github.com/getsentry/sentry-dotnet/pull/4498))

### Fixes

- Fail when building Blazor WASM with Profiling. We don't support profiling in Blazor WebAssembly projects. ([#4512](https://github.com/getsentry/sentry-dotnet/pull/4512))
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/GlobalSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public GlobalSessionManager(

// TODO: session file should really be process-isolated, but we
// don't have a proper mechanism for that right now.
_persistenceDirectoryPath = options.TryGetDsnSpecificCacheDirectoryPath();
_persistenceDirectoryPath = options.TryGetIsolatedCacheDirectoryPath();
}

// Take pause timestamp directly instead of referencing _lastPauseTimestamp to avoid
Expand Down
153 changes: 153 additions & 0 deletions src/Sentry/Internal/CacheDirectoryCoordinator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using Sentry.Extensibility;
using Sentry.Internal.Extensions;

namespace Sentry.Internal;

internal class CacheDirectoryCoordinator : IDisposable
{
private readonly IDiagnosticLogger? _logger;
private readonly IFileSystem _fileSystem;
private readonly object _gate = new();

private Stream? _lockStream;
private readonly string _lockFilePath;

private bool _acquired;
private bool _disposed;
Comment on lines +15 to +16
Copy link

Choose a reason for hiding this comment

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

The double-checked locking pattern is correctly implemented, but the _acquired field should be marked as volatile to ensure proper memory visibility across threads. This prevents potential race conditions where one thread might not see the updated value immediately.

Suggested change
private bool _acquired;
private bool _disposed;
private volatile bool _acquired;

Did we get this right? 👍 / 👎 to inform future reviews.


public CacheDirectoryCoordinator(string cacheDir, IDiagnosticLogger? logger, IFileSystem fileSystem)
{
_logger = logger;
_fileSystem = fileSystem;
_lockFilePath = $"{cacheDir}.lock";

try
{
var baseDir = Path.GetDirectoryName(_lockFilePath);
if (!string.IsNullOrWhiteSpace(baseDir))
{
// Not normally necessary, but just in case
fileSystem.CreateDirectory(baseDir);
}
}
catch (Exception ex)
{
_logger?.LogError("Failed to ensure lock directory exists for cache coordinator.", ex);
}
}

public bool TryAcquire()
{
if (_acquired)
{
return true;
}

lock (_gate)
{
if (_acquired)
{
return true;
}

if (_disposed)
{
return false;
}

try
{
_acquired = _fileSystem.TryCreateLockFile(_lockFilePath, out _lockStream);
return _acquired;
}
catch (Exception ex)
{
_logger?.LogDebug("Unable to acquire cache directory lock", ex);
}
finally
{
if (!_acquired && _lockStream is not null)
{
try
{ _lockStream.Dispose(); }
catch
{
// Ignore
}
_lockStream = null;
}
}

return false;
}
}

public void Dispose()
{
lock (_gate)
{
if (_disposed)
{
return;
}

_disposed = true;

if (_acquired)
{
try
{
_lockStream?.Close();
}
catch (Exception ex)
{
_logger?.LogError("Error releasing the cache directory file lock.", ex);
}
}

try
{
_lockStream?.Dispose();
}
catch (Exception ex)
{
_logger?.LogError("Error disposing cache lock stream.", ex);
}
_lockStream = null;
}
}
}

internal static class CacheDirectoryHelper
{
public const string IsolatedCacheDirectoryPrefix = "isolated_";

internal static string? GetBaseCacheDirectoryPath(this SentryOptions options) =>
string.IsNullOrWhiteSpace(options.CacheDirectoryPath)
? null
: Path.Combine(options.CacheDirectoryPath, "Sentry");

internal static string? GetIsolatedFolderName(this SentryOptions options)
{
var stringBuilder = new StringBuilder(IsolatedCacheDirectoryPrefix);
#if IOS || ANDROID
// On iOS or Android the app is already sandboxed, so there's no risk of sending data to another Sentry's DSN.
// However, users may still initiate the SDK multiple times within the process, so we need an InitCounter
stringBuilder.Append(options.InitCounter.Count);
#else
if (string.IsNullOrWhiteSpace(options.Dsn))
{
return null;
}
var processId = options.ProcessIdResolver.Invoke() ?? 0;
stringBuilder.AppendJoin('_', options.Dsn.GetHashString(), processId, options.InitCounter.Count);
#endif
return stringBuilder.ToString();
}

internal static string? TryGetIsolatedCacheDirectoryPath(this SentryOptions options) =>
GetBaseCacheDirectoryPath(options) is not { } baseCacheDir
|| GetIsolatedFolderName(options) is not { } isolatedFolderName
? null
: Path.Combine(baseCacheDir, isolatedFolderName);
}
4 changes: 4 additions & 0 deletions src/Sentry/Internal/FileSystemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespace Sentry.Internal;

internal abstract class FileSystemBase : IFileSystem
{
public IEnumerable<string> EnumerateDirectories(string path, string searchPattern) =>
Directory.EnumerateDirectories(path, searchPattern);

public IEnumerable<string> EnumerateFiles(string path) => Directory.EnumerateFiles(path);

public IEnumerable<string> EnumerateFiles(string path, string searchPattern) =>
Expand All @@ -23,6 +26,7 @@ public IEnumerable<string> EnumerateFiles(string path, string searchPattern, Sea
public abstract bool CreateDirectory(string path);
public abstract bool DeleteDirectory(string path, bool recursive = false);
public abstract bool CreateFileForWriting(string path, out Stream fileStream);
public abstract bool TryCreateLockFile(string path, out Stream fileStream);
public abstract bool WriteAllTextToFile(string path, string contents);
public abstract bool MoveFile(string sourceFileName, string destFileName, bool overwrite = false);
public abstract bool DeleteFile(string path);
Expand Down
Loading
Loading