Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 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
ada094b
Review feedback
jamescrosswell Nov 5, 2025
efaa83e
Merge remote-tracking branch 'origin/version6' into isolated-cache
jamescrosswell Nov 5, 2025
3730654
Added auto migration from v5
jamescrosswell Nov 7, 2025
81a06f9
Format code
getsentry-bot Nov 7, 2025
af4821a
Fix tests for windows
jamescrosswell Nov 7, 2025
8f72bf3
Merge branch 'isolated-cache' of https://github.com/getsentry/sentry-…
jamescrosswell Nov 7, 2025
1003333
Merge branch 'version6' into isolated-cache
jamescrosswell Nov 11, 2025
2be400d
Review feedback
jamescrosswell Nov 12, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Features

- Implemented 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))
- The SDK now makes use of the new SessionEndStatus `Unhandled` when capturing an unhandled but non-terminal exception, i.e. through the UnobservedTaskExceptionIntegration ([#4633](https://github.com/getsentry/sentry-dotnet/pull/4633), [#4653](https://github.com/getsentry/sentry-dotnet/pull/4653))
- The SDK now provides a `IsSessionActive` to allow checking the session state ([#4662](https://github.com/getsentry/sentry-dotnet/pull/4662))
- The SDK now makes use of the new SessionEndStatus `Unhandled` when capturing an unhandled but non-terminal exception, i.e. through the UnobservedTaskExceptionIntegration ([#4633](https://github.com/getsentry/sentry-dotnet/pull/4633))
Expand Down
5 changes: 1 addition & 4 deletions src/Sentry/GlobalSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public GlobalSessionManager(
_clock = clock ?? SystemClock.Clock;
_persistedSessionProvider = persistedSessionProvider
?? (filePath => Json.Load(_options.FileSystem, filePath, PersistedSessionUpdate.FromJson));

// 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.GetIsolatedCacheDirectoryPath();
}

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

namespace Sentry.Internal;

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

private Stream? _lockStream;
private readonly string _lockFilePath;

private volatile bool _acquired;
private volatile bool _disposed;

public CacheDirectoryCoordinator(string cacheDir, IDiagnosticLogger? logger, IFileSystem fileSystem)
{
_logger = logger;
_fileSystem = fileSystem;
// Note this creates a lock file in the cache directory's parent directory... not in the cache directory itself
_lockFilePath = $"{cacheDir}.lock";
}

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

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

if (_disposed)
{
return false;
}

try
{
var baseDir = Path.GetDirectoryName(_lockFilePath);
if (!string.IsNullOrWhiteSpace(baseDir))
{
_fileSystem.CreateDirectory(baseDir);
}
_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()
{
if (_disposed)
{
return;
}

lock (_lock)
{
_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.ToString(),
options.InitCounter.Count.ToString());
#endif
return stringBuilder.ToString();
}

internal static string? GetIsolatedCacheDirectoryPath(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