Skip to content
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

Fix Deadlock Inside Metrics Code #105259

Merged
merged 1 commit into from
Jul 22, 2024

Conversation

tarekgh
Copy link
Member

@tarekgh tarekgh commented Jul 22, 2024

This is a regression from the change in #104680.

Thanks to @rokonec who caught it and suggested the fix too.

@tarekgh
Copy link
Member Author

tarekgh commented Jul 22, 2024

@stephentoub @jkotas could you please help review this fix? Thanks!

CC @noahfalk @stevejgordon

@tarekgh tarekgh marked this pull request as ready for review July 22, 2024 17:52
@jakobbotsch
Copy link
Member

Do you think this will fix #105189?

@tarekgh
Copy link
Member Author

tarekgh commented Jul 22, 2024

Do you think this will fix #105189?

Very possible. I'll close this one when I merge to see if this will show up again.

// 2. Instrument.Publish is called and enters the SyncObject lock.
// 3. Within the lock block, MeterListener is called, triggering its static constructor.
// 4. The static constructor creates runtime metrics instruments, causing re-entry to Instrument.Publish and leading to a deadlock.
RuntimeHelpers.RunClassConstructor(typeof(MeterListener).TypeHandle);
Copy link
Member

Choose a reason for hiding this comment

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

I've been thinking about it and this seems OK - I didn't come up with another place where the MeterListener static constructor would be a problem. In the future if we did discover any more it might be worthwhile to move the runtime metrics initialization out of the static constructor and instead do it as a lazy initialization within the MeterListener instance constructor.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we can consider moving the initialization later out of the static constructor.

@tarekgh tarekgh merged commit 75a48e9 into dotnet:main Jul 22, 2024
82 of 84 checks passed
jkotas added a commit to jkotas/runtime that referenced this pull request Jul 26, 2024
- Trigger the RuntimeMetrics initialization only when actually needed in the MeterListener constructor.
- Delete the lock-ordering workaround and wrong comment introduced in dotnet#105259. Trigger the RuntimeMetrics initialization only when actually needed should make the lock-ordering workarond unnecessary.
jkotas added a commit to jkotas/runtime that referenced this pull request Jul 26, 2024
- Trigger the RuntimeMetrics initialization only when actually needed in the MeterListener constructor.
- Delete the lock-ordering workaround and wrong comment introduced in dotnet#105259. Trigger the RuntimeMetrics initialization only when actually needed should make the lock-ordering workarond unnecessary.
tarekgh pushed a commit that referenced this pull request Jul 26, 2024
* Simplify initialization of RuntimeMetrics

- Trigger the RuntimeMetrics initialization only when actually needed in the MeterListener constructor.
- Delete the lock-ordering workaround and wrong comment introduced in #105259. Trigger the RuntimeMetrics initialization only when actually needed should make the lock-ordering workarond unnecessary.

* Delete unnecessary static fields

* Update src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/RuntimeMetrics.cs
@tarekgh tarekgh deleted the FixDeadlockInsideMetricsCode branch August 6, 2024 20:03
@github-actions github-actions bot locked and limited conversation to collaborators Sep 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants