fix: cache race conditions and time underflow bugs - #58
Open
kimjune01 wants to merge 2 commits into
Open
Conversation
Issue: Veirt#49 reported that detached cache tasks can stack during slow disk I/O. The previous implementation spawned unbounded tokio tasks for each cache write, which could accumulate faster than the filesystem could process them. Solution: Introduce a bounded mpsc channel (32 tasks) with a single writer worker that serializes all cache writes. When the buffer is full, try_send drops writes instead of blocking, providing backpressure. Changes: - Add CacheWriteTask enum for location/weather/geocode writes - Add static CACHE_WRITER with OnceLock for single worker initialization - Refactor save_* functions to use try_send instead of spawn - Extract write_* async functions for actual I/O operations - Add test demonstrating rapid cache writes complete without hang The bounded worker prevents memory exhaustion under sustained slow I/O while maintaining non-blocking behavior for callers.
Gemini bug hunt found 3 critical/high severity bugs:
1. Cache thrashing: Multiple locations overwrote single cache files.
Fix: Use location-specific filenames (weather_{location}_{provider}.json,
geocode_{location}_{language}.json) to support multiple cached locations.
2. Read/write race: Concurrent reads could see truncated/corrupted JSON during writes.
Fix: Atomic write-then-rename pattern using temp files to prevent partial reads.
3. Time underflow panic: Clock adjustments backward caused arithmetic underflow.
Fix: Use saturating_sub() instead of plain subtraction for cache expiration checks.
All three fixes maintain backward compatibility — old single-file caches still work,
new multi-file caches enable proper multi-location support.
Deferred (low severity, design decisions):
- Silent error discarding (would need logging framework)
- Wasted create_dir_all calls (performance, not correctness)
- Redundant write collapsing (enhancement, not bug)
- Graceful shutdown (requires broader coordination)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Gemini bug hunt found 3 critical/high severity bugs:
Cache thrashing: Multiple locations overwrote single cache files.
Fix: Use location-specific filenames (weather_{location}{provider}.json,
geocode{location}_{language}.json) to support multiple cached locations.
Read/write race: Concurrent reads could see truncated/corrupted JSON during writes.
Fix: Atomic write-then-rename pattern using temp files to prevent partial reads.
Time underflow panic: Clock adjustments backward caused arithmetic underflow.
Fix: Use saturating_sub() instead of plain subtraction for cache expiration checks.
All three fixes maintain backward compatibility — old single-file caches still work,
new multi-file caches enable proper multi-location support.
Deferred (low severity, design decisions):
Test plan