Add idempotency guards to patch_audit_functions() and start_blocking_io_detection() - #10
Merged
adriaardila merged 2 commits intoJul 20, 2026
Conversation
…io_detection() Calling either twice in the same process silently double-wraps stdlib functions and/or double-registers the sys.audit hook, permanently double-counting every blocking event with no warning - e.g. when two independent conftest.py fixtures or an app factory both run setup. Mirrors the guard detect_slow_tasks() already had. Adds test coverage for all three setup functions (detect_slow_tasks() had none either), and resets the new flags in test_uvloop.py's fixture so its per-test setup stays isolated.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit daa7fb7. Configure here.
adriaardila
approved these changes
Jul 20, 2026
adriaardila
left a comment
Collaborator
There was a problem hiding this comment.
Confirmed! Thanks for the PR
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.

Fixes #9
Summary:
detect_slow_tasks()already guards against being called twice; the other two setup functions didn't, so a second call in the same process silently double-wraps stdlib functions and/or double-registers thesys.audithook (permanent, since hooks can't be removed), quadruple-counting blocking events with no warning. See #9 for the full writeup and reproduction.Changes:
Mirrors
detect_slow_tasks()'s existing guard on both functions.Tests:
TestSetupIdempotencycovers all three setup functions (includingdetect_slow_tasks(), which had no coverage either).test_uvloop.py's reset fixture now resets the two new flags too, so its tests keep getting fresh setup per test instead of silently no-op'ing.Note
Low Risk
Small defensive change to startup setup with tests; no auth, data, or API behavior changes beyond preventing duplicate instrumentation.
Overview
patch_audit_functions()andstart_blocking_io_detection()now matchdetect_slow_tasks(): a process-wide “already configured” flag makes repeat calls a no-op with a warning, instead of double-wrapping stdlib functions or registering anothersys.audithook (which would inflate blocking-event counts).Tests:
TestSetupIdempotencyasserts second calls leave patched function identity,trace_depth, and slow-task threshold unchanged and log the expected warnings. The uvloop reset fixture documents that the two new flags are not cleared between tests so the suite does not re-patch or re-register hooks every case.Reviewed by Cursor Bugbot for commit 3af6415. Bugbot is set up for automated code reviews on this repo. Configure here.