BUG/MAJOR: promex: fix crash on deleted server
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 22 Feb 2024 16:18:15 +0000 (17:18 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 22 Feb 2024 17:39:35 +0000 (18:39 +0100)
commitef337f8b2d52ddb699e3511c3fecd2dd6dbdc207
treed5a2eafc8199bb41f9e72ceb7c1b736fa206d000
parent260dbb8a6a2e5bb9941b74847dbb0a7c9734aca7
BUG/MAJOR: promex: fix crash on deleted server

Promex applet is used to dump many metrics. Some of them are related to
a server instance. The applet can be interrupted in the middle of a
dump, for example waiting for output buffer space. In this case, its
context is save to resume dump on the correct instance.

A crash can occur if dump is interrupted during servers loop. If the
server instance is deleted during two scheduling of the promex applet,
its context will still referenced the deleted server on resume.

To fix this, use server refcount to prevent its deletion during parsing.
As <ctx.sv> is manipulated in a lot of places, use a static helper
function to automatically drop previous server refcount and increment
the new one.

To complete this change, also implement a release callback to ensure
server refcount is decremented in case of early applet abort.

A corresponding fix was first introduced in 3.0 dev branch. However, due
to massive differences, a new patch has been designed on top of the
current 2.9 tree. For reference, here is the 3.0 reference.

  f81c00cd447e21b07ee355fd5c2fb2a4787ea4a0
  BUG/MAJOR: promex: fix crash on deleted server

This must be backported to all stable releases.
addons/promex/service-prometheus.c