Skip to content

Add idempotency guards to patch_audit_functions() and start_blocking_io_detection() - #10

Merged
adriaardila merged 2 commits into
Feverup:masterfrom
hadizamani021:fix/setup-functions-idempotency-guard
Jul 20, 2026
Merged

Add idempotency guards to patch_audit_functions() and start_blocking_io_detection()#10
adriaardila merged 2 commits into
Feverup:masterfrom
hadizamani021:fix/setup-functions-idempotency-guard

Conversation

@hadizamani021

@hadizamani021 hadizamani021 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

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 the sys.audit hook (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:

TestSetupIdempotency covers all three setup functions (including detect_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() and start_blocking_io_detection() now match detect_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 another sys.audit hook (which would inflate blocking-event counts).

Tests: TestSetupIdempotency asserts 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.

…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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit daa7fb7. Configure here.

Comment thread tests/test_uvloop.py Outdated

@adriaardila adriaardila 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.

Confirmed! Thanks for the PR

@adriaardila
adriaardila merged commit 9d77ffb into Feverup:master Jul 20, 2026
5 checks passed
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.

patch_audit_functions() / start_blocking_io_detection() silently double-count events when called more than once

2 participants