-
-
Notifications
You must be signed in to change notification settings - Fork 1
Fix resumable-transport DDoS: terminate degenerate pullBundleChunk requests #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hahn-kev-bot
wants to merge
1
commit into
master
Choose a base branch
from
claude/youthful-bardeen-a5b13f
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+417
β48
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| namespace HgResume.Api; | ||
|
|
||
| /// <summary> | ||
| /// Periodically reaps abandoned transaction artifacts from <see cref="ApiConfig.CachePath"/>. | ||
| /// | ||
| /// Only <c>finishPull/PushBundle</c> cleaned these up, so a client that looped (or simply went away) | ||
| /// left its <c>.bundle/.metadata/.async_run/.incoming</c> files behind forever. The July 2026 incident | ||
| /// left <b>1,258</b> leaked <c>.async_run</c> locks. The PHP deployment would have needed a cron job; | ||
| /// the .NET host is long-running, so a <see cref="BackgroundService"/> fits without any image | ||
| /// process-model change. | ||
| /// </summary> | ||
| public sealed class CacheGarbageCollector : BackgroundService | ||
| { | ||
| // Transaction artifacts BundleHelper/AsyncRunner write into the cache dir. Repo-reset backups live | ||
| // under their own TempRepoFolder and are reaped by RepoManageService, so we don't touch those. | ||
| private static readonly string[] Extensions = { ".bundle", ".metadata", ".async_run", ".incoming" }; | ||
|
|
||
| private readonly ApiConfig _config; | ||
| private readonly ILogger<CacheGarbageCollector> _logger; | ||
|
|
||
| public CacheGarbageCollector(ApiConfig config, ILogger<CacheGarbageCollector> logger) | ||
| { | ||
| _config = config; | ||
| _logger = logger; | ||
| } | ||
|
|
||
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
| { | ||
| // Run once at startup (reap anything a prior process left behind), then on the configured cadence. | ||
| while (!stoppingToken.IsCancellationRequested) | ||
| { | ||
| try | ||
| { | ||
| Collect(); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _logger.LogWarning(e, "Cache GC pass failed"); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| await Task.Delay(_config.CacheGcInterval, stoppingToken); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Deletes cache artifacts whose last-write time is older than <see cref="ApiConfig.CacheTtl"/>.</summary> | ||
| public int Collect() | ||
| { | ||
| if (!Directory.Exists(_config.CachePath)) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| DateTime cutoff = DateTime.UtcNow - _config.CacheTtl; | ||
| int removed = 0; | ||
| foreach (string path in Directory.EnumerateFiles(_config.CachePath)) | ||
| { | ||
| if (!Extensions.Contains(Path.GetExtension(path))) | ||
| { | ||
| continue; | ||
| } | ||
| try | ||
| { | ||
| if (File.GetLastWriteTimeUtc(path) < cutoff) | ||
| { | ||
| File.Delete(path); | ||
| removed++; | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| // A file deleted or rewritten by a concurrent request between stat and delete is fine; | ||
| // just skip it this pass. | ||
| _logger.LogDebug(e, "Cache GC could not reap {Path}", path); | ||
| } | ||
| } | ||
|
|
||
| if (removed > 0) | ||
| { | ||
| _logger.LogInformation("Cache GC reaped {Count} stale cache file(s) from {Path}", | ||
| removed, _config.CachePath); | ||
| } | ||
| return removed; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style comment only. This function is an example of why I dislike the "opening and closing braces on their own line" style that seems to have become standard in C#. I would much rather see this code written as:
5 lines instead of 17, and taking up much less vertical space on-screen. With functions taking up less vertical space, it's easier to read them in the context of surrounding code, rather than scrolling up and down to see surrounding context.
Very much a style nitpick. But this is one of the things that bugs me every time I see it, and this
whileloop is one of the best examples I've ever seen where readability would have been much improved by having fewer lines of code.