Skip to content

fix: cache race conditions and time underflow bugs - #58

Open
kimjune01 wants to merge 2 commits into
Veirt:mainfrom
kimjune01:fix/cache-task-stacking
Open

fix: cache race conditions and time underflow bugs#58
kimjune01 wants to merge 2 commits into
Veirt:mainfrom
kimjune01:fix/cache-task-stacking

Conversation

@kimjune01

Copy link
Copy Markdown

Summary

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)

Test plan

  • Tests pass
  • Manual verification completed

kimjune01 added 2 commits May 11, 2026 20:33
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)
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.

1 participant