Skip to content

fix(metrics): stop double-freeing native metrics and family object#2

Open
Rakdos8 wants to merge 1 commit into
carbonengine:mainfrom
Rakdos8:fix/metric-double-free
Open

fix(metrics): stop double-freeing native metrics and family object#2
Rakdos8 wants to merge 1 commit into
carbonengine:mainfrom
Rakdos8:fix/metric-double-free

Conversation

@Rakdos8
Copy link
Copy Markdown

@Rakdos8 Rakdos8 commented May 15, 2026

Summary

Removes two heap-corruption defects on the normal metric dealloc path
(counter, gauge, histogram, summary).

Problem

  1. Each metric PyObject held its native metric in a std::unique_ptr, but
    the same pointer is owned by MetricFactory's unique_ptr maps — so
    destroying the PyObject double-freed the native metric.
  2. *_dealloc ran delete self->family on a PyObject* (another Python
    object, or self), corrupting the heap on every dealloc.

Fix

Hold the native metric as a non-owning raw pointer (the factory is the
owner) and remove the bogus delete. The family reference is still
correctly released via Py_DecRef. Applied identically to all four
metric types.

Type

Security / stability — double-free & heap corruption (Critical).

Testing

Manual review; ownership now single-rooted in MetricFactory, dealloc no
longer frees memory it does not own.

Each metric PyObject held its native metric in a std::unique_ptr, but
that same pointer is owned by MetricFactory's counters/gauges/histograms/
summaries maps (std::unique_ptr), so destroying the PyObject double-freed
it. The dealloc also ran 'delete self->family' on a PyObject* (either
another Python object or self), corrupting the heap on every dealloc.

Hold the native metric as a non-owning raw pointer (the factory owns it)
and drop the bogus delete; family refcount is still released via
Py_DecRef. Applied to counter, gauge, histogram and summary.
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.

1 participant