Skip to content

Commit f4aadf6

Browse files
committed
Prevent cleanup timer callback from running during/after Dispose()
This only occurred if not using the `IAsyncDisposable.DisposeAsync()` interface method to dispose of the `SqliteCache` object. Closes #24
1 parent 9c16c7d commit f4aadf6

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

SqliteCache/SqliteCache.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ public void Dispose()
6464
_logger.LogTrace("Disposing SQLite cache database at {SqliteCacheDbPath}", _config.CachePath);
6565

6666
// Dispose the timer first, because it might still access other things until it's been disposed!
67-
_cleanupTimer?.Dispose();
67+
if (_cleanupTimer is not null)
68+
{
69+
// Timer.Dispose(WaitHandle) ends up delegating to (the internal)
70+
// TimerQueueTimer.Dispose(WaitHandle), which calls (the private)
71+
// EventWaitHandle.Set(SafeWaitHandle) method, which is just a wrapper around Kernel32's
72+
// SetEvent() -- all of which is to say, we can't use a ManualResetEventSlim here.
73+
using var resetEvent = new ManualResetEvent(false);
74+
_cleanupTimer.Dispose(resetEvent.WaitHandle);
75+
}
6876
Commands.Dispose();
6977

7078
_logger.LogTrace("Closing connection to SQLite database at {SqliteCacheDbPath}", _config.CachePath);

0 commit comments

Comments
 (0)