@@ -7,33 +7,20 @@ internal class CacheDirectoryCoordinator : IDisposable
77{
88 private readonly IDiagnosticLogger ? _logger ;
99 private readonly IFileSystem _fileSystem ;
10- private readonly object _gate = new ( ) ;
10+ private readonly Lock _lock = new ( ) ;
1111
1212 private Stream ? _lockStream ;
1313 private readonly string _lockFilePath ;
1414
15- private bool _acquired ;
16- private bool _disposed ;
15+ private volatile bool _acquired ;
16+ private volatile bool _disposed ;
1717
1818 public CacheDirectoryCoordinator ( string cacheDir , IDiagnosticLogger ? logger , IFileSystem fileSystem )
1919 {
2020 _logger = logger ;
2121 _fileSystem = fileSystem ;
22+ // Note this creates a lock file in the cache directory's parent directory... not in the cache directory itself
2223 _lockFilePath = $ "{ cacheDir } .lock";
23-
24- try
25- {
26- var baseDir = Path . GetDirectoryName ( _lockFilePath ) ;
27- if ( ! string . IsNullOrWhiteSpace ( baseDir ) )
28- {
29- // Not normally necessary, but just in case
30- fileSystem . CreateDirectory ( baseDir ) ;
31- }
32- }
33- catch ( Exception ex )
34- {
35- _logger ? . LogError ( "Failed to ensure lock directory exists for cache coordinator." , ex ) ;
36- }
3724 }
3825
3926 public bool TryAcquire ( )
@@ -43,7 +30,7 @@ public bool TryAcquire()
4330 return true ;
4431 }
4532
46- lock ( _gate )
33+ lock ( _lock )
4734 {
4835 if ( _acquired )
4936 {
@@ -57,6 +44,11 @@ public bool TryAcquire()
5744
5845 try
5946 {
47+ var baseDir = Path . GetDirectoryName ( _lockFilePath ) ;
48+ if ( ! string . IsNullOrWhiteSpace ( baseDir ) )
49+ {
50+ _fileSystem . CreateDirectory ( baseDir ) ;
51+ }
6052 _acquired = _fileSystem . TryCreateLockFile ( _lockFilePath , out _lockStream ) ;
6153 return _acquired ;
6254 }
@@ -84,13 +76,13 @@ public bool TryAcquire()
8476
8577 public void Dispose ( )
8678 {
87- lock ( _gate )
79+ if ( _disposed )
8880 {
89- if ( _disposed )
90- {
91- return ;
92- }
81+ return ;
82+ }
9383
84+ lock ( _lock )
85+ {
9486 _disposed = true ;
9587
9688 if ( _acquired )
@@ -140,12 +132,13 @@ internal static class CacheDirectoryHelper
140132 return null ;
141133 }
142134 var processId = options . ProcessIdResolver . Invoke ( ) ?? 0 ;
143- stringBuilder . AppendJoin ( '_' , options . Dsn . GetHashString ( ) , processId , options . InitCounter . Count ) ;
135+ stringBuilder . AppendJoin ( '_' , options . Dsn . GetHashString ( ) , processId . ToString ( ) ,
136+ options . InitCounter . Count . ToString ( ) ) ;
144137#endif
145138 return stringBuilder . ToString ( ) ;
146139 }
147140
148- internal static string ? TryGetIsolatedCacheDirectoryPath ( this SentryOptions options ) =>
141+ internal static string ? GetIsolatedCacheDirectoryPath ( this SentryOptions options ) =>
149142 GetBaseCacheDirectoryPath ( options ) is not { } baseCacheDir
150143 || GetIsolatedFolderName ( options ) is not { } isolatedFolderName
151144 ? null
0 commit comments