Skip to content

[#699] Hold the metrics cache lock while the history worker scrapes - #700

Open
lookslikeameya wants to merge 1 commit into
pgexporter:mainfrom
lookslikeameya:history-connection-race
Open

lookslikeameya wants to merge 1 commit into
pgexporter:mainfrom
lookslikeameya:history-connection-race

Conversation

@lookslikeameya

Copy link
Copy Markdown
Contributor

closes #699

The history timer worker scraped without the lock that /metrics holds, so the two could use the shared servers[] connection at the same time and reset each other's fd. The worker now takes the same lock just for the scrape, and writes the snapshot after releasing it.

A /metrics request that arrives during a history scrape now waits for it, the same way two /metrics requests already wait for each other. If the lock isn't free within blocking_timeout, the worker skips that snapshot.

Tested on macOS (arm64) with the setup from #699 (history_interval = 1s, back-to-back /metrics scrapes for 60s):

  • Before: 15/110 and 4/100 scrapes reported pgexporter_postgresql_active 0
  • After: 0/82, 0/71, 0/72, no errors in the log
  • With pid logging added locally, no process saw another's fd in 62 history scrapes running alongside 121 /metrics scrapes

@lookslikeameya

Copy link
Copy Markdown
Contributor Author

@jesperpedersen PTAL. The Debug test failures also happen on main (#691).

@lookslikeameya
lookslikeameya force-pushed the history-connection-race branch 2 times, most recently from b92659f to 5c2a30a Compare December 1, 2026 15:03
@Pranav-error

Copy link
Copy Markdown
Collaborator

#692 is merged now. It fixes the Debug crash in test_connection_bind_unix_socket_permissions (#691), which is what fails both build-linux Debug jobs here. Please rebase on main.

@lookslikeameya
lookslikeameya force-pushed the history-connection-race branch from 5c2a30a to a43489f Compare December 2, 2026 11:12
@lookslikeameya

Copy link
Copy Markdown
Contributor Author

@Pranav-error Thanks, rebased on main (801d41c). All CI jobs pass now. PTAL

@Pranav-error Pranav-error left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reproduced #699 on 801d41c with history_interval = 1s and /metrics in a loop for 60s (macOS, PG 14, ASan): 2/205 scrapes had pgexporter_postgresql_active 0 and ~2 KB bodies. On a43489f: 0/200, and no history scrape overlapped a /metrics scrape. When I reverted only the local_history.c call, both symptoms came back: an active 0 scrape, and in a second run pg_monitor role check failed FATAL x3.

One thing to look at: metrics_page() jumps to error: without releasing cache->lock, so a client that drops mid-response leaves the lock held (#325, #725). With this PR the history worker waits on that lock too. After 5 aborted clients, the next /metrics on a43489f failed after 30s, and the log showed history: failed to scrape metrics 5 times. On 801d41c the same steps logged 0, because the worker doesn't take the lock there. Can #725 go in first?

There is no test. The race is timing-dependent, so I didn't ask for one. Not run: test/check.sh suite and Linux; CI is green.

@lookslikeameya

lookslikeameya commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

@Pranav-error Thanks for testing! Agreed, #725 should go in first since #700 makes the history worker depend on that lock. Once #725 is merged I'll rebase.

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.

History timer worker and /metrics scrape share the server connection, so a scrape can report the server as down

2 participants