Skip to content

Fix: replace lock(this) with static per-file-path lock#18

Open
HashidaTKS wants to merge 1 commit into
masterfrom
fix/static-file-lock
Open

Fix: replace lock(this) with static per-file-path lock#18
HashidaTKS wants to merge 1 commit into
masterfrom
fix/static-file-lock

Conversation

@HashidaTKS
Copy link
Copy Markdown
Owner

問題点

BaseRepository および XmlSerializerWrapperlock(this) を使用していたため、同一ファイルに対して複数のインスタンスが同時アクセスした場合に排他制御が機能しなかった。

具体的には RaceDataManager.csAutoPurchaserMainTask.csnew HoldingInformationRepository() が複数回呼ばれており、UIスレッドとバックグラウンドタスクが同時に同じXMLファイルを読み書きするとファイル破損やデシリアライズ失敗が発生しうる状態だった。

改善内容

  • XmlSerializerWrapper にファイルパスをキーとした static Dictionary<string, object> _fileLocks を追加
  • lock(this)lock(GetFileLock()) に置き換え、同一ファイルパスへのアクセスはインスタンスをまたいで排他制御されるよう修正
  • BaseRepository 側の冗長な lock(this) を削除(XmlSerializerWrapper 側で保護されるため)

Test plan

  • 同一ファイルに対して複数の Repository インスタンスから同時に Store / ReadAll を呼んだ際にデータ破損が起きないことを確認
  • 異なるファイルパスを持つ Repository インスタンスはそれぞれ独立してロックされることを確認

🤖 Generated with Claude Code

…a corruption

lock(this) only protects against concurrent access from the same instance.
When multiple instances access the same file (as seen in RaceDataManager and
AutoPurchaserMainTask), there is no mutual exclusion, risking file corruption.

Introduce a static Dictionary<string, object> keyed by file path in
XmlSerializerWrapper so that all instances targeting the same file share
one lock object. Remove the now-redundant lock(this) in BaseRepository.

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