Skip to content

fix(metrics): clear state in publishStoredMetrics even when throwOnEmptyMetrics throws#5213

Open
svozza wants to merge 3 commits intomainfrom
fix/metrics-state-leak-on-throw
Open

fix(metrics): clear state in publishStoredMetrics even when throwOnEmptyMetrics throws#5213
svozza wants to merge 3 commits intomainfrom
fix/metrics-state-leak-on-throw

Conversation

@svozza
Copy link
Copy Markdown
Contributor

@svozza svozza commented Apr 26, 2026

Summary

Changes

When serializeMetrics() throws due to throwOnEmptyMetrics, the cleanup calls (clearMetrics, clearDimensions, clearMetadata) in publishStoredMetrics() are never reached. This causes dimensions and metadata to leak into subsequent publish calls.

  • Wrap the serialize/emit block in try/finally to guarantee state cleanup even when an exception is thrown
  • Add test verifying dimensions and metadata don't leak after a failed publish
  • Add test for the disabled=true code path in publishStoredMetrics, removing the /* v8 ignore else */ coverage pragma

Issue number: closes #5207


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

…ptyMetrics throws

When serializeMetrics() threw due to throwOnEmptyMetrics, the cleanup
calls (clearMetrics, clearDimensions, clearMetadata) were never reached,
causing dimensions and metadata to leak into subsequent publishes.

Wrap the serialize/emit block in try/finally to guarantee cleanup.
Also add tests for the disabled code path, removing the v8 ignore comment.

Closes #5207
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M PR between 30-99 LOC label Apr 26, 2026
@svozza svozza closed this Apr 26, 2026
@svozza svozza reopened this Apr 26, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Apr 26, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Apr 26, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Apr 26, 2026
@sonarqubecloud
Copy link
Copy Markdown

@svozza svozza requested a review from dreamorosi April 27, 2026 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M PR between 30-99 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: State leaks when throwOnEmptyMetrics throws — dimensions and metadata carry over to next publish

1 participant