Skip to content

Commit d6dd817

Browse files
.
1 parent 0046933 commit d6dd817

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/Sentry/Internal/CacheDirectoryCoordinator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public CacheDirectoryCoordinator(string cacheDir, IDiagnosticLogger? logger, IFi
3636
}
3737
}
3838

39-
public bool TryAcquire(TimeSpan timeout)
39+
public bool TryAcquire()
4040
{
4141
if (_acquired)
4242
{

src/Sentry/Internal/Http/CachingTransport.cs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private CachingTransport(ITransport innerTransport, SentryOptions options, bool
8282
options.TryGetIsolatedCacheDirectoryPath() ??
8383
throw new InvalidOperationException("Cache directory or DSN is not set.");
8484
_cacheCoordinator = new CacheDirectoryCoordinator(_isolatedCacheDirectoryPath, options.DiagnosticLogger, options.FileSystem);
85-
if (!_cacheCoordinator.TryAcquire(TimeSpan.FromMilliseconds(100)))
85+
if (!_cacheCoordinator.TryAcquire())
8686
{
8787
throw new InvalidOperationException("Cache directory already locked.");
8888
}
@@ -107,7 +107,7 @@ private void Initialize(bool startWorker)
107107
_worker = Task.Run(CachedTransportBackgroundTaskAsync);
108108

109109
// Start a recycler in the background so as not to block initialisation
110-
_recycler = Task.Run(SalvageAbandonedCacheSessions);
110+
_recycler = Task.Run(() => SalvageAbandonedCacheSessions(_workerCts.Token), _workerCts.Token);
111111
}
112112
else
113113
{
@@ -187,7 +187,7 @@ private async Task CachedTransportBackgroundTaskAsync()
187187
_options.LogDebug("CachingTransport worker stopped.");
188188
}
189189

190-
private void SalvageAbandonedCacheSessions()
190+
private void SalvageAbandonedCacheSessions(CancellationToken cancellationToken)
191191
{
192192
if (_options.GetBaseCacheDirectoryPath() is not { } baseCacheDir)
193193
{
@@ -197,22 +197,34 @@ private void SalvageAbandonedCacheSessions()
197197
const string searchPattern = $"{CacheDirectoryHelper.IsolatedCacheDirectoryPrefix}*";
198198
foreach (var dir in _fileSystem.EnumerateDirectories(baseCacheDir, searchPattern))
199199
{
200+
if (cancellationToken.IsCancellationRequested)
201+
{
202+
return; // graceful exit on cancellation
203+
}
204+
200205
if (string.Equals(dir, _isolatedCacheDirectoryPath, StringComparison.OrdinalIgnoreCase))
201206
{
202207
continue; // Ignore the current cache directory
203208
}
204209

205210
_options.LogDebug("found abandoned cache: {0}", dir);
206211
using var coordinator = new CacheDirectoryCoordinator(dir, _options.DiagnosticLogger, _options.FileSystem);
207-
if (coordinator.TryAcquire(TimeSpan.FromMilliseconds(100)))
212+
if (!coordinator.TryAcquire())
208213
{
209-
_options.LogDebug("Salvaging abandoned cache: {0}", dir);
210-
foreach (var filePath in _fileSystem.EnumerateFiles(dir, "*.envelope", SearchOption.AllDirectories))
214+
continue;
215+
}
216+
217+
_options.LogDebug("Salvaging abandoned cache: {0}", dir);
218+
foreach (var filePath in _fileSystem.EnumerateFiles(dir, "*.envelope", SearchOption.AllDirectories))
219+
{
220+
if (cancellationToken.IsCancellationRequested)
211221
{
212-
var destinationPath = Path.Combine(_isolatedCacheDirectoryPath, Path.GetFileName(filePath));
213-
_options.LogDebug("Moving abandoned file back to cache: {0} to {1}.", filePath, destinationPath);
214-
MoveFileWithRetries(filePath, destinationPath, "abandoned");
222+
return; // graceful exit on cancellation
215223
}
224+
225+
var destinationPath = Path.Combine(_isolatedCacheDirectoryPath, Path.GetFileName(filePath));
226+
_options.LogDebug("Moving abandoned file back to cache: {0} to {1}.", filePath, destinationPath);
227+
MoveFileWithRetries(filePath, destinationPath, "abandoned");
216228
}
217229
}
218230
}
@@ -582,6 +594,20 @@ public Task StopWorkerAsync()
582594
return _worker;
583595
}
584596

597+
private Task StopRecyclerAsync()
598+
{
599+
if (_recycler?.IsCompleted != false)
600+
{
601+
// already stopped
602+
return Task.CompletedTask;
603+
}
604+
605+
// Stop worker and wait until it finishes
606+
_options.LogDebug("Stopping CachingTransport worker.");
607+
_workerCts.Cancel();
608+
return _recycler;
609+
}
610+
585611
public Task FlushAsync(CancellationToken cancellationToken = default)
586612
{
587613
_options.LogDebug("CachingTransport received request to flush the cache.");
@@ -600,6 +626,15 @@ public async ValueTask DisposeAsync()
600626
_options.LogError(ex, "Error stopping worker during dispose.");
601627
}
602628

629+
try
630+
{
631+
await StopRecyclerAsync().ConfigureAwait(false);
632+
}
633+
catch (Exception ex)
634+
{
635+
_options.LogError(ex, "Error in recycler during dispose.");
636+
}
637+
603638
_workerSignal.Dispose();
604639
_workerCts.Dispose();
605640
_worker.Dispose();

0 commit comments

Comments
 (0)