Skip to content

Add cancellation support to cache cleanup task - #29

Merged
Sarmkadan merged 1 commit into
mainfrom
fix/cache-cleanup-cancellation
May 21, 2026
Merged

Add cancellation support to cache cleanup task#29
Sarmkadan merged 1 commit into
mainfrom
fix/cache-cleanup-cancellation

Conversation

@Sarmkadan

Copy link
Copy Markdown
Owner

Summary

The InMemoryCacheService background cleanup task ran in an infinite loop with no cancellation support, preventing graceful shutdown.

Changes

  • InMemoryCacheService now implements IDisposable
  • Added CancellationTokenSource that's passed to the cleanup task
  • The cleanup loop checks IsCancellationRequested and the Task.Delay call honors the token
  • OperationCanceledException is caught and handled cleanly during shutdown

Testing

  • Verified the cleanup task stops within the delay interval when Dispose() is called
  • Existing cache get/set/remove operations are unaffected

Closes #26

The background cleanup task used while(true) with no way to stop it
during application shutdown. Now uses CancellationTokenSource that
gets cancelled on Dispose(), allowing graceful shutdown.

Closes #26

@Sarmkadan Sarmkadan left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Clean fix. The only thing I'd add for completeness is making sure the DI container is configured to treat this as a singleton so Dispose gets called at the right time. But that's a configuration concern, not a code issue.

@Sarmkadan
Sarmkadan merged commit e064e5b into main May 21, 2026
2 of 5 checks passed
Sarmkadan added a commit that referenced this pull request Jul 11, 2026
The background cleanup task used while(true) with no way to stop it
during application shutdown. Now uses CancellationTokenSource that
gets cancelled on Dispose(), allowing graceful shutdown.

Closes #26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InMemoryCacheService cleanup task runs forever without cancellation support

1 participant