Skip to content

test: improve robustness of test_shielded_cancel_sleep_time - #1264

Open
mcepl wants to merge 1 commit into
agronholm:masterfrom
openSUSE-Python:more_robust_test_shielded_cancel_sleep_time
Open

test: improve robustness of test_shielded_cancel_sleep_time#1264
mcepl wants to merge 1 commit into
agronholm:masterfrom
openSUSE-Python:more_robust_test_shielded_cancel_sleep_time

Conversation

@mcepl

@mcepl mcepl commented Aug 4, 2026

Copy link
Copy Markdown

Changes

Fixes: #1263

The test_shielded_cancel_sleep_time test was failing on certain architectures (notably ppc64le in OBS) because the CPU time consumed during the test window occasionally exceeded the wall-clock duration, leading to false-positive assertions of "CPU spinning".

The test is designed to ensure that cancelling a shielded task does not cause the event loop to enter a tight loop (spinning), consuming 100% CPU. However, on high-overhead systems, the combination of context switching and event loop management can push CPU usage slightly above the 1:1 ratio of wall-clock time.

Checklist

If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):

  • You've added tests (in tests/) which would fail without your patch NOT APPLICABLE
  • You've updated the documentation (in docs/), in case of behavior changes or new
    features NOT APPLICABLE
  • You've added a new changelog entry (in docs/versionhistory.rst).

If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.

Updating the changelog

If there are no entries after the last release, use **UNRELEASED** as the version.
If, say, your patch fixes issue #123, the entry should look like this:

- Fix big bad boo-boo in task groups
  (`#123 <https://github.com/agronholm/anyio/issues/123>`_; PR by @yourgithubaccount)

If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.

@mcepl
mcepl force-pushed the more_robust_test_shielded_cancel_sleep_time branch from 49775e8 to 0683119 Compare August 4, 2026 19:12

@agronholm agronholm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Change the timings if you like, but adding constants for this where they're only used in one place is just pointless churn. Drop the changelog entry, as this is an internal change. The comment also adds nothing useful, so drop that too.

@mcepl
mcepl force-pushed the more_robust_test_shielded_cancel_sleep_time branch from 0683119 to 821f4bd Compare August 8, 2026 15:01
@mcepl

mcepl commented Aug 8, 2026

Copy link
Copy Markdown
Author

Change the timings if you like, but adding constants for this where they're only used in one place is just pointless churn. Drop the changelog entry, as this is an internal change. The comment also adds nothing useful, so drop that too.

I would say just opposite, that hard coding random numbers into your code is crazy for the future maintenance, but as you wish.

The `test_shielded_cancel_sleep_time` test was failing on certain
architectures (notably ppc64le in the OBS server) because the CPU time
consumed during the test window occasionally exceeded the wall-clock
duration, leading to false-positive assertions of "CPU spinning".

The test is designed to ensure that cancelling a shielded task does not
cause the event loop to enter a tight loop (spinning), consuming 100%
CPU. However, on high-overhead systems, the combination of context
switching and event loop management can push CPU usage slightly above
the 1:1 ratio of wall-clock time.

Fixes: agronholm#1263
@mcepl
mcepl force-pushed the more_robust_test_shielded_cancel_sleep_time branch from 821f4bd to a1d4d4a Compare August 8, 2026 15:03
@agronholm

Copy link
Copy Markdown
Owner

Adding constants can be useful if the values are used in more than one location and need to be changed in lock-step. Otherwise it's just extra fluff, especially in test code.

@agronholm

Copy link
Copy Markdown
Owner

Your force-pushing (don't do that please) didn't remove the single-use constant.

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.

test_shielded_cancel_sleep_time is unstable on slow build machines and slow platforms (e.g., ppc64le)

2 participants