Skip to content

Fix: protect static cache in RaceDataManager with lock#21

Open
HashidaTKS wants to merge 1 commit into
masterfrom
fix/static-cache-thread-safety
Open

Fix: protect static cache in RaceDataManager with lock#21
HashidaTKS wants to merge 1 commit into
masterfrom
fix/static-cache-thread-safety

Conversation

@HashidaTKS

Copy link
Copy Markdown
Owner

問題点

RaceDataManager.CachedHoldingInformation は静的フィールドだが、UIスレッドとバックグラウンドの Task.Run() から同時にアクセス・更新される可能性があり、lockvolatile もなかった。

これにより複数スレッドが同時にキャッシュを参照・更新した場合に競合状態(race condition)が発生し、古いキャッシュを参照し続けたり、読み書きが中途半端な状態のオブジェクトを参照するリスクがあった。

改善内容

  • private static readonly object _cacheLock を追加
  • CachedHoldingInformation の読み書きをすべて lock (_cacheLock) で保護
  • CachedHoldingInformationvolatile を追加してキャッシュの可視性を保証
  • ResetCache() も同じロックで保護

Test plan

  • 複数スレッドから同時に GetRaceDataOfDay を呼び出してもデータ競合が発生しないことを確認
  • ResetCache() がスレッドセーフに機能することを確認

🤖 Generated with Claude Code

…ace conditions

CachedHoldingInformation is a static field accessed from both the UI thread
and background Task.Run threads without synchronization, creating a data race.
Add a dedicated static lock object (_cacheLock) and mark the field volatile
so that reads and writes to the cache are properly serialized across threads.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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