Skip to content

root: clear stale Prometheus multiprocess files on startup - #27772

Open
andrein wants to merge 1 commit into
goauthentik:mainfrom
andrein:metrics-clear-multiproc-dir
Open

andrein wants to merge 1 commit into
goauthentik:mainfrom
andrein:metrics-clear-multiproc-dir

Conversation

@andrein

@andrein andrein commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Details

What does this PR change?

bootstrap() deletes the .db files in the Prometheus multiprocess directory before the server, worker or all-in-one start (and before migrations). Only .db files are removed, so a PROMETHEUS_MULTIPROC_DIR pointing at a shared path loses nothing else.

Why is this change needed?

prometheus_client requires the multiprocess directory to be wiped between runs: https://prometheus.github.io/client_python/multiprocess/

authentik creates the directory but never wipes it. When it survives a restart (a container restart inside the same pod keeps /dev/shm), the new processes reuse the old per-worker files, and every labelset the previous processes wrote keeps being exported, including values no live process will ever overwrite.

How was this tested?

  • Unit test in src/boot.rs: .db files are removed, other files and directories are kept.
  • Built the binary from this branch on a dev image of main, deleted a certificate-key pair (with the metric from crypto: export certificate expiry as a Prometheus metric #27771) without scraping, restarted the server container: before, the deleted pair kept its old expiry; after, it is gone.
  • CI on my fork: lints and unit tests pass. oidc_rp-initiated failed with upload_image failed - HTTP 400 File size exceeds the 500KB limit, which also fails on main.

Side effect: Python counters and histograms start from zero after every restart, which Prometheus treats as a normal counter reset.

Linked issues

refs #27771


AI disclosure: written with Claude Code (Claude Opus 5.5), which drafted the code, test and this description; I reviewed, ran and tested the change myself.

Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)
  • I have read the AI usage policy.

prometheus_client requires the multiprocess directory to be wiped
between runs. Without it, a restart that keeps the directory (a
container restart within the same pod, where /dev/shm survives) lets
new processes reuse the old per-PID files, and labelsets the old
processes wrote keep being exported.

https://prometheus.github.io/client_python/multiprocess/

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@andrein
andrein requested a review from a team as a code owner November 11, 2026 05:35
@netlify

netlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-storybook ready!

Name Link
🔨 Latest commit e02b136
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/6af3fea3b4157cf777a0b627
😎 Deploy Preview https://deploy-preview-27772--authentik-storybook.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-integrations ready!

Name Link
🔨 Latest commit e02b136
🔍 Latest deploy log https://app.netlify.com/projects/authentik-integrations/deploys/6af3fea395f04a77e6f93b50
😎 Deploy Preview https://deploy-preview-27772--authentik-integrations.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit e02b136
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6af3fea36b417a5474294422
😎 Deploy Preview https://deploy-preview-27772--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@andrein

andrein commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

@BeryJu thanks for merging #27771! This one is its companion and I think it's still needed:

The NaN for deleted pairs only works while the process that wrote the old labelset is alive. authentik never wipes PROMETHEUS_MULTIPROC_DIR, and prometheus_client requires it to be wiped between runs. When the directory survives a restart (e.g. a container restart within the same pod, since /dev/shm lives as long as the pod), the new processes reopen the old .db files (gunicorn names them by worker number, so the names repeat), and every labelset written before the restart keeps being exported. Nothing alive ever overwrites them.

Reproduced with a dev image of main plus #27771:

  1. generate a certificate-key pair, scrape /metrics: expiry is exported
  2. delete the pair, restart the server container without scraping in between
  3. scrape again: the deleted pair still reports its old expiry, indefinitely

With this PR the .db files are removed in bootstrap() before migrations and the server/worker start, and the deleted pair is gone after the restart. This isn't specific to the certificate metric: any labelset exported before a restart lingers the same way.

This branch has not been deployed

No deployments
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