[DBMON-6882] Make Postgres check teardown idempotent - #24852
Merged
Conversation
_finalize() now runs at most once. The check is already cancelled more than once in practice -- run_one_check() cancels and the integration_check fixture cancels again on teardown -- so teardown re-closed connections and re-tore down the async jobs each time. Nulling log.check also moves after the final log line. CheckLoggingAdapter.process reads back through that attribute for checks whose check_id was never resolved, so the trailing debug call raised AttributeError once debug logging was enabled. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
evalya-impact-summaryevalya impact analysis |
Contributor
🎉 All green!🧪 All tests passed 🚧 1 test that failed was ignored due to quarantine 🎯 Code Coverage (details) 🔗 Commit SHA: 937883c | Docs | Datadog PR Page | Give us feedback! |
Drop the logging-hazard test and the rationale comment on the idempotency test. Co-authored-by: Cursor <cursoragent@cursor.com>
eric-weaver
added a commit
that referenced
this pull request
Aug 13, 2026
An integration carrying its own copy of the protocol reaches this wrapper through super().run(), so both frames see the cancellation and call _finalize(). Rather than have the base wrapper special-case that nesting, Postgres's teardown was made idempotent (#24852), which is the guard the wrappers need while they coexist. Co-authored-by: Cursor <cursoragent@cursor.com>
3 tasks
eric-weaver
marked this pull request as ready for review
August 13, 2026 13:36
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 85b133b654
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
azhou-datadog
previously approved these changes
Aug 13, 2026
The first teardown nulls log.check, so a second call cannot log through the adapter -- which is the very hazard this change is fixing elsewhere. Co-authored-by: Cursor <cursoragent@cursor.com>
temporal-github-worker-1
Bot
dismissed
azhou-datadog’s stale review
August 13, 2026 13:46
Review from azhou-datadog is dismissed. Related teams and files:
- database-monitoring-agent
- postgres/datadog_checks/postgres/postgres.py
Contributor
Validation ReportAll 21 validations passed. Show details
|
azhou-datadog
approved these changes
Aug 13, 2026
eric-weaver
added a commit
that referenced
this pull request
Aug 13, 2026
The scenario it covered belongs to the integration that still wraps run(), and is exercised by the idempotency test in #24852. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
What does this PR do?
Makes
PostgreSql._finalize()run at most once, guarded by a_finalizedflag under theexisting
_cancel_lock, and movesself.log.check = Noneafter the final log statement.Motivation
Two independent problems on the teardown path, both surfaced while preparing #24844 (lifting
this protocol into
DatabaseCheck).Teardown is not idempotent, and the check is already finalized more than once in practice:
run_one_check()callscancel()and theintegration_checkfixture calls it again onteardown, so every integration test closes the main connection, calls
db_pool.close_all()and re-runs async job shutdown twice. It only survives today because each individual step
happens to tolerate a second call.
Nulling
log.checkbefore the trailing debug line is a latent crash.CheckLoggingAdapter.processre-readsself.check.check_idon every call for checks whosecheck_idwas never resolved, so_finalize()raisesAttributeError: 'NoneType' object has no attribute 'check_id'as soon as debug logging is enabled. Verified locally against anunmodified check.
This also unblocks #24844 from carrying transitional logic. When the base class gains the same
protocol,
PostgreSql.run()reaches it throughsuper().run(), so the inner and outer wrapperswould both finalize on an in-flight cancel. With teardown idempotent here, the base wrapper
needs no special case for integrations that have not yet migrated, and this guard moves into the
base once Postgres adopts it.
Review checklist (to be filled by reviewers)
qa/requiredif this PR needs QA validation, orqa/skip-qaif it does not. Exactly one of the two is required.backport/<branch-name>label to the PR and it will automatically open a backport PR once this one is merged