Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Prevent race condition in LoggingService #11284

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

Conversation

YuliiaKovalova
Copy link
Member

@YuliiaKovalova YuliiaKovalova commented Jan 14, 2025

Fixes: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/2320135/

Context

Reset() method invocation sometimes causes exception System.IO.IOException: 'The handle is invalid.'.
Very likely it happens on application shutdown.
The assumption is:

  • ShutdownComponent starts and gets the _lockObject
  • It cleans up/disposes the events
  • Meanwhile our logging thread is trying to use those same events without checking _lockObject in context of StartLoggingEventProcessing()

Changes Made

  1. All local vars to prevent a race to set fields null and then use them
  2. Add catch if fields got disposed while Logging still was going on.

image

Testing

N/A

@YuliiaKovalova YuliiaKovalova marked this pull request as ready for review January 15, 2025 10:27
Copy link
Member

@rainersigwald rainersigwald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I understand how this helps. Isn't there still a race between thread 1 seeing that it's non-null and calling into it and thread 2 calling Dispose()?

@YuliiaKovalova YuliiaKovalova marked this pull request as draft January 24, 2025 09:36
@YuliiaKovalova YuliiaKovalova force-pushed the dev/ykovalova/better_handle_event_reset branch from 95adccd to 3611f9a Compare January 24, 2025 10:08
@YuliiaKovalova YuliiaKovalova force-pushed the dev/ykovalova/better_handle_event_reset branch from 50d0c8c to 74a1a74 Compare January 27, 2025 09:18
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Copy link
Member

@JanKrivanek JanKrivanek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss this one

@YuliiaKovalova YuliiaKovalova force-pushed the dev/ykovalova/better_handle_event_reset branch from 0322d36 to 2cae31a Compare February 6, 2025 14:47
@YuliiaKovalova YuliiaKovalova force-pushed the dev/ykovalova/better_handle_event_reset branch from 84bb22b to b87fe20 Compare February 6, 2025 16:41
@YuliiaKovalova YuliiaKovalova marked this pull request as ready for review February 10, 2025 10:56
Copy link
Member

@JanProvaznik JanProvaznik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It improves the situation, but please update the description. I wouldn't say it's a double check pattern, it 1. prevents a race to set a field null and then use it and 2. catches a dispose race.

Copy link
Member

@JanKrivanek JanKrivanek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@YuliiaKovalova
Copy link
Member Author

The solution isn't ideal, but it allows to avoid "System.IO.IOException: 'The handle is invalid.'" exception.
if it's not enough and causes other crashes, we will reconsider it.

@YuliiaKovalova YuliiaKovalova changed the title Apply double check pattern to avoid race condition in LoggingService Prevent race condition in LoggingService Feb 11, 2025
@YuliiaKovalova YuliiaKovalova merged commit e3404ae into dotnet:main Feb 11, 2025
10 checks passed
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.

4 participants