Skip to content

[Exporter.Prometheus] Fix stack overflow - #7524

Merged
martincostello merged 4 commits into
open-telemetry:mainfrom
martincostello:fix-stack-overflow-in-prometheus-exporter
Jul 16, 2026
Merged

martincostello merged 4 commits into
open-telemetry:mainfrom
martincostello:fix-stack-overflow-in-prometheus-exporter

Conversation

@martincostello

@martincostello martincostello commented Jul 15, 2026 •

Copy link
Copy Markdown
Member

Changes

Fix stack overflow caused by recursion if EnterCollect() failed and then completed synchronously introduced by refactoring in #7371.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

Fix stack overflow caused by recursion if `EnterCollect()` failed and then completed synchronously introduced by refactoring in open-telemetry#7371.
@martincostello martincostello added this to the v1.17.0 milestone Jul 15, 2026
@github-actions github-actions Bot added the pkg:OpenTelemetry.Exporter.Prometheus.HttpListener Issues related to OpenTelemetry.Exporter.Prometheus.HttpListener NuGet package label Jul 15, 2026
@codecov

codecov Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.16667% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.23%. Comparing base (fbe866c) to head (29fd776).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...ner/Internal/Shared/PrometheusCollectionManager.cs 80.85% 9 Missing ⚠️
...r/Internal/Shared/PrometheusExporterEventSource.cs 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #7524   +/-   ##
=======================================
  Coverage   90.23%   90.23%           
=======================================
  Files         290      290           
  Lines       15936    15967   +31     
=======================================
+ Hits        14380    14408   +28     
- Misses       1556     1559    +3     
Flag Coverage Δ
unittests-Project-Experimental 90.27% <79.16%> (-0.04%) ⬇️
unittests-Project-Stable 90.26% <79.16%> (-0.03%) ⬇️
unittests-UnstableCoreLibraries-Experimental 51.10% <79.16%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...r/Internal/Shared/PrometheusExporterEventSource.cs 48.27% <0.00%> (-1.73%) ⬇️
...ner/Internal/Shared/PrometheusCollectionManager.cs 90.68% <80.85%> (-1.70%) ⬇️

... and 4 files with indirect coverage changes

@martincostello
martincostello marked this pull request as ready for review July 15, 2026 14:40
Copilot AI review requested due to automatic review settings July 15, 2026 14:40
@martincostello
martincostello requested a review from a team as a code owner July 15, 2026 14:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a stack overflow in the Prometheus exporter scrape path by eliminating recursive retry behavior when a shared collection completes synchronously but does not produce a response for the requested protocol. It introduces bounded retry loops and adds a stress-style regression test to validate the retry path under contention.

Changes:

  • Refactors PrometheusCollectionManager.EnterCollect / WaitForCollectionResponseAsync to use bounded loops instead of recursion when retrying after a shared collection doesn’t satisfy a protocol.
  • Adds a MaxCollectAttempts cap and a CollectStep result type to make retry behavior explicit and prevent infinite retry/starvation.
  • Adds a regression test that repeatedly exercises the retry path under concurrent scraping to ensure it does not overflow the stack.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/PrometheusCollectionManager.cs Reworks collection entry/retry logic to avoid recursive retries (stack growth) and adds bounded retry limits.
test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs Adds a contention-based regression test to ensure retry behavior doesn’t overflow the stack.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Address Copilot review feedback.

if (!resolved)
{
// The attempt budget was exhausted while contending with concurrent

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should a warning be emitted in this case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These are both just constructs to avoid using a while (true) in case there was ever a logic bug that meant the loop never terminated, but it shouldn't happen.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a log message for completeness just in case.

}

if (protocolWasRegistered)
// The retry budget was exhausted while contending with concurrent scrapes;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Similar situation - worth emitting a warning here? Or is this a typical situation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a log message for completeness just in case.

Add an error log if the collection cannot acquire a lock to get a response within the retry budget for some reason.
Add entries for this PR.
@github-actions github-actions Bot added the pkg:OpenTelemetry.Exporter.Prometheus.AspNetCore Issues related to OpenTelemetry.Exporter.Prometheus.AspNetCore NuGet package label Jul 16, 2026
@martincostello
martincostello enabled auto-merge July 16, 2026 07:55
@martincostello
martincostello added this pull request to the merge queue Jul 16, 2026
Merged via the queue into open-telemetry:main with commit 37db49d Jul 16, 2026
76 checks passed
@martincostello
martincostello deleted the fix-stack-overflow-in-prometheus-exporter branch July 16, 2026 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg:OpenTelemetry.Exporter.Prometheus.AspNetCore Issues related to OpenTelemetry.Exporter.Prometheus.AspNetCore NuGet package pkg:OpenTelemetry.Exporter.Prometheus.HttpListener Issues related to OpenTelemetry.Exporter.Prometheus.HttpListener NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants