Skip to content

Comments

[-] resolve monitoredSources race condition, fixes #1213#1223

Open
Mazen050 wants to merge 6 commits intocybertec-postgresql:masterfrom
Mazen050:bugfix/datarace-monitoredsources
Open

[-] resolve monitoredSources race condition, fixes #1213#1223
Mazen050 wants to merge 6 commits intocybertec-postgresql:masterfrom
Mazen050:bugfix/datarace-monitoredsources

Conversation

@Mazen050
Copy link
Contributor

Fix a data race in Reaper caused by concurrent access to the shared monitoredSources slice.

LoadSources() updates this slice while background goroutines (Reap() loop and WriteMonitoredSources()) read it concurrently, which triggers race detector warnings.

This PR protects the slice with an RWMutex and clones it before iteration to ensure safe concurrent access.

Fixes #1213

@Mazen050 Mazen050 marked this pull request as draft February 22, 2026 13:52
@Mazen050 Mazen050 marked this pull request as ready for review February 22, 2026 14:41
@pashagolub pashagolub changed the title [-] resolve monitoredSources race condition [-] resolve monitoredSources race condition, fixes #1213 Feb 22, 2026
Copy link
Collaborator

@pashagolub pashagolub left a comment

Choose a reason for hiding this comment

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

It would be perfect if you can add some tests

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure this file is somehow related to the fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry that's my bad, I forgot to mention that this specific test failed in CI after the fix, Also it didn't fail on my machine so its related to github's CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I attempted to fix it so that when you review we can see if my fix is good or we should take another approach.

@pashagolub pashagolub self-assigned this Feb 22, 2026
@pashagolub pashagolub added bug Something isn't working sources What sources and in what way to monitor labels Feb 22, 2026
@Mazen050
Copy link
Contributor Author

It would be perfect if you can add some tests

Sure thing, I was actually planning on adding tests for this specific file and had some tests already written. Will add these here asap.

@Mazen050
Copy link
Contributor Author

Hello @pashagolub

I have added tests increasing coverage by about 9%. Only two functions remaining with 0% coverage reapMetricMeasurements and Reap which both I think are hard. please let me know if we should make tests for them so that I can try again with them and either send another PR or add to this. Also if we were to make a test for Reap how would we go about it?

Also please let me know if there are any edge cases we can add tests for that are not necessarily related to coverage.

@Mazen050 Mazen050 requested a review from pashagolub February 24, 2026 00:01
Copy link
Collaborator

@0xgouda 0xgouda left a comment

Choose a reason for hiding this comment

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

@pashagolub, I wonder if, instead of adding additional locking overhead, we should deprecate configured_dbs and remove the writeMonitoredSources() function entirely. Almost all dashboards now rely on admin.all_distinct_dbname_metrics instead?

@pashagolub pashagolub force-pushed the bugfix/datarace-monitoredsources branch from d2fd661 to 2c23a05 Compare February 24, 2026 13:45
@pashagolub
Copy link
Collaborator

@pashagolub, I wonder if, instead of adding additional locking overhead, we should deprecate configured_dbs and remove the writeMonitoredSources() function entirely. Almost all dashboards now rely on admin.all_distinct_dbname_metrics instead?

We use it in Global Health dashboards. If we can get rid of it, I will support this decision

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working sources What sources and in what way to monitor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Data race in reaper.monitoredSources causing potential crash or inconsistent reads

3 participants