-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix ObjectDisposedException race condition in TimestampedFileLogger #53241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,7 +176,18 @@ protected override void WriteMessage(string message) | |
| { | ||
| if (!_messageQueue.IsAddingCompleted) | ||
| { | ||
| _messageQueue.Add(message); | ||
| try | ||
| { | ||
| _messageQueue.Add(message); | ||
| } | ||
| catch (ObjectDisposedException) | ||
| { | ||
| // The logger was disposed between the IsAddingCompleted check and Add. | ||
| } | ||
| catch (InvalidOperationException) | ||
| { | ||
| // CompleteAdding was called between the IsAddingCompleted check and Add. | ||
| } | ||
|
Comment on lines
+179
to
+190
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7092,4 +7092,4 @@ | |
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IsAddingCompletedcheck is still a TOCTOU gate (state can change immediately after the check). SinceAddis now wrapped to handle disposal/completion races, consider removing theIsAddingCompletedcheck (or including it inside the try/catch) and just attempt theAdd, dropping messages onInvalidOperationException/ObjectDisposedException. This reduces duplicated branching and eliminates an extra race window.