GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test(monitor): Stop background health checks in tests that drive the monitor by sentry-junior[bot] · Pull Request #8032 · getsentry/sentry-python

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Description

This fixes the flaky tests/test_monitor.py::test_segment_uses_downsampled_rate (assert 0.25 == 0.5, job). Its siblings test_transaction_uses_downsampled_rate and test_monitor_unhealthy have the same race.

Root cause
These tests call monitor.run() by hand and assert exact downsample_factor values. They also set monitor.interval = 0.1. The first monitor.is_healthy() starts the monitor's background thread, which then calls run() every 0.1 s as well. On a slow runner (the failure was in Gevent 3.10, where the test took about 0.34 s), the thread runs an extra unhealthy check before the sampling decision. The factor goes from 1 to 2, so the sample rate is 0.25.

Fix
These tests now call monitor.kill() instead of shortening the interval. The thread is_healthy() starts then exits right away, so only the test's own run() calls change the factor. The assertions are unchanged, and there are no retries, sleeps or skips. SDK code is unchanged; this was a test bug.

Reproduction: getsentry/repro#62 (sentry-sdk 2.72.0, Python 3.12). With random gaps of 0–0.2 s before sampling, the rate was wrong in 55 of 100 iterations. With gaps under 0.1 s it was 0 of 100, and with the background thread stopped it was 0 of 100.

Original tests, local runs (py3.14-common). Each run covered the 3 affected tests. A test-only -p plugin added a random wait before start_transaction / traces.start_span to imitate a slow runner; no test or SDK code was changed by it.

  • master, no wait: 10 of 10 runs passed. The race doesn't show up locally on its own.
  • master, 0–200 ms wait: 11 of 15 runs failed. test_segment_uses_downsampled_rate failed 6 times and test_transaction_uses_downsampled_rate 8 times.
  • This branch, 0–200 ms wait: 40 of 40 runs passed. The baseline per-test failure rate was about 40%, so 9 clean runs would already give 99% confidence.
  • This branch, 0–500 ms wait: 20 of 20 runs passed.
  • Regression checks: tests/test_monitor.py, tests/tracing/test_sampling.py and tests/tracing/test_decorator.py all passed (109 tests). ruff is clean. I haven't run the full common suite or the gevent setup locally; CI covers those.

Not changed here: test_trace_decorator_no_trx also failed once, with leftover monitor logs (downsampling with a factor of 10). The likely cause is in tests/conftest.py::sentry_init: inner() assigns client without nonlocal, so the client.close() in teardown never runs. Monitor threads from earlier tests (with the 0.1 s interval) then keep running. I'll handle that separately, because closing every test client affects the whole suite.

Issues

  • resolves: Flaky test: test_segment_uses_downsampled_rate races the monitor's background thread #8031

via johdax.

--

View Junior Session [Sentry]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sentry-junior @dingsdax
test(monitor): Stop background health checks in tests that drive the …
2828363
…monitor

The monitor tests call monitor.run() by hand and assert exact
downsample_factor values, but they also set monitor.interval = 0.1, so the
monitor's background thread runs extra health checks every 0.1 s. On a slow
runner that bumps the factor between the asserts (0.25 instead of 0.5).
Kill the background loop in these tests instead.

Fixes #8031

Co-Authored-By: Johannes Daxböck <[email protected]>
@github-actions

github-actions Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 133666 passed | ⏭️ 7276 skipped | Total: 140942 | Pass Rate: 94.84% | Execution Time: 447m 45s

📊 Comparison with Base Branch

Metric Change
Total Tests —
Passed Tests —
Failed Tests —
Skipped Tests —

✨ Test counts unchanged from base.

All tests are passing successfully.

✅ Patch coverage is 100.00% (no changed executable lines found; target 80%).
Project statement coverage is 90.23% (down 0.03 percentage points from base (c96bbef) to head (2828363)).

Coverage diff
@@            Coverage Diff             @@
##        master     #8032       +/-##
==========================================
- Coverage    90.26%    90.23%    -0.03%
==========================================
  Files          202       202         —
  Tracked lines     26660     26660         —
  Branches      9926      9926         —
==========================================
- Hits         24063     24055        -8
- Misses        2597      2605        +8
  Partials      1510      1510         —

Generated by Coverage Action

sentry-junior[bot] reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sentrivana
sentrivana approved these changes Oct 15, 2026
View reviewed changes
@sentrivana
sentrivana marked this pull request as ready for review October 15, 2026 12:31
@sentrivana
sentrivana requested a review from a team as a code owner October 15, 2026 12:31
@sentrivana
sentrivana merged commit ab069a5 into master Oct 15, 2026
167 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@sentrivana
sentrivana deleted the fix/monitor-tests-background-thread-race branch October 15, 2026 12:31
This was referenced Oct 22, 2026
publish: getsentry/[email protected] getsentry/publish#9970
Closed
publish: getsentry/[email protected] getsentry/publish#10029
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sentrivana sentrivana sentrivana approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Flaky test: test_segment_uses_downsampled_rate races the monitor's background thread

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@sentrivana
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.