[Backport 7.80.x] [DBMON-6602] Avoid cleanup when cancel called while check running#23729
Merged
Conversation
Contributor
Author
Validation ReportAll 20 validations passed. Show details
|
Codecov Report❌ Patch coverage is Additional details and impacted files🚀 New features to boost your workflow:
|
🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: a79e913 | Docs | Datadog PR Page | Give us feedback! |
AAraKKe
approved these changes
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport 4c7e8bb from #23728.
What does this PR do?
Fixes a race condition where
cancel()could destroy check state (close database connections, null_query_manager,_db, etc.) whilecheck()is still running on another thread. This caused SIGSEGV crashes in libpq during cluster check rebalancing when multiple Postgres checks were unscheduled simultaneously.The fix splits cancel into two phases:
_cancel_async_jobs): sets cancel events on async job threads. Safe to run concurrently withcheck()._finalize): joins async job threads, closes connections, and nulls state. Only runs whencheck()is guaranteed idle.A lock coordinates
run()andcancel()to ensure_finalize()executes exactly once — either bycancel()directly (if the check is idle) or byrun()'s finally block (if the check is in-flight).The cleanup introduced in #23640 is required — it breaks reference cycles (check → async job → check, check → query manager → check, check → logger → check) and closes connections so that garbage collection can free the check object in a timely manner. The problem was not the cleanup itself but where it ran: directly inside
cancel(), which can execute concurrently withcheck(). This PR preserves all of that cleanup by moving it to_finalize(), which only runs whencheck()is guaranteed idle.Note: the base
AgentCheckclass currently lacks a formalized pattern for coordinatingcancel()with an in-flightrun(). Therun()/cancel()lock coordination is implemented directly in the Postgres check for now, with the intention of moving this into the base class (and potentially the Go-sideCheckWrapper) so all checks benefit from the same safety guarantees.Motivation
The base class documents that
cancel()"can be called while the check is running." PR #23640 added aggressive cleanup tocancel()(closing connections, nulling attributes) for GC improvements, which violated this contract. Whencancel()ran whilecheck()was mid-query via psycopg,_close_db()freed the underlying libpq socket. When the I/O completed andcheck()resumed, libpq dereferenced freed memory, producingSIGSEGV addr=0x0instead of a recoverable Python exception. This was observed during a cluster check rebalance (26 configs removed, 29 added) where 8 Postgres checks were cancelled in rapid succession.Review checklist (to be filled by reviewers)
qa/skip-qalabel if the PR doesn't need to be tested during QA.backport/<branch-name>label to the PR and it will automatically open a backport PR once this one is merged