Fix: eliminate TOCTOU race condition in AutoPurchaserMainTask.Stop()#37
Open
HashidaTKS wants to merge 1 commit intomasterfrom
Open
Fix: eliminate TOCTOU race condition in AutoPurchaserMainTask.Stop()#37HashidaTKS wants to merge 1 commit intomasterfrom
HashidaTKS wants to merge 1 commit intomasterfrom
Conversation
Stop() checked Running (CancellationTokenSource != null) then called CancellationTokenSource.Cancel(). The background task sets CancellationTokenSource = null in its finally block, so between the null check and .Cancel() another thread could null out the field, causing NullReferenceException. Capture the field reference into a local variable first and use the null-conditional operator (?.) to call Cancel atomically on that snapshot, eliminating the race window. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
問題点
AutoPurchaserMainTask.Stop()に TOCTOU(Time-of-check-time-of-use)競合状態があった(line 86-91)。バックグラウンドタスクの
finallyブロックでCancellationTokenSource = nullが設定されるため、①と②の間に別スレッドがnullを書き込むとNullReferenceExceptionが発生する。改善内容
CancellationTokenSourceをローカル変数ctsにキャプチャしてから使用cts?.Cancel()で null 安全に Cancel を呼び出すRunningの別途チェックが不要になるTest plan
Stop()を呼び出してもNullReferenceExceptionが発生しないことを確認Stop()を呼び出しても安全に動作することを確認🤖 Generated with Claude Code