Skip to content

bug(ci): test_1081_physical_meter ceiling-clamp test fails under full-suite ordering — settings_service caches a pre-eviction db #2045

Open

Description

@obasilakis

Summary

tests/unit/test_1081_physical_meter.py::TestPerAgentMeter::test_available_floors_at_zero_over_ceiling[sqlite]
fails when the full unit suite runs, and passes 14/14 when its file runs alone.
pytest-randomly is installed, so file order is randomized per run — which makes
this a coin flip on ordering rather than a deterministic failure.

Found while verifying an unrelated branch (#2044 / ent#243). Pre-existing on
dev
— confirmed by running the identical command against a clean, unmodified
origin/dev worktree.

Evidence

Clean worktree at origin/dev @ 47f8843b, no changes of any kind:

$ git worktree add ~/wt-base --detach origin/dev
$ cd ~/wt-base/src/backend
$ python -m pytest ../../tests/unit -q -p no:warnings -x --timeout=120

FAILED ../../tests/unit/test_1081_physical_meter.py::TestPerAgentMeter::test_available_floors_at_zero_over_ceiling[sqlite]
1 failed, 2657 passed, 12 skipped in 92.58s

In isolation, same worktree:

$ python -m pytest ../../tests/unit/test_1081_physical_meter.py -q -p no:randomly
14 passed in 3.51s

Two independent full-suite runs both stopped at this same test after different
pass counts (2657 and 3821) — consistent with pytest-randomly drawing a
different seed each run and -x halting at the same landmine from different
positions.

Likely mechanism (structurally verified; not yet reduced to a minimal pair)

  • src/backend/services/settings_service.py:18 is from database import db — it
    binds the db object at module-import time.
  • tests/unit/test_1081_physical_meter.py deliberately evicts database (and
    db.connection, db.schedules, …) from sys.modules so production code
    re-resolves against the db_harness engine, and snapshots/restores those names
    around every test (Issue fix(tests): test_audit_log_unit.py — 50 collection ImportErrors when run in full suite #762 hygiene, _STUBBED_MODULE_NAMES).
  • services.settings_service is not in that snapshot list. If any earlier
    file in the session has already imported it, it keeps a reference to the
    pre-eviction db.
  • The failing test then does set_ceiling(2) — writing max_parallel_tasks_ceiling
    through the re-resolved harness database — while the clamp is read through
    settings_service's stale db. The ceiling lookup misses, clamp_to_ceiling
    falls back to the default, and the assertion st.max_parallel_tasks == 2 sees
    the unclamped value.

This fits the failing assertion exactly (it is the only test in the class that
depends on a ceiling written by the fixture and read back through
settings_service
), and it fits settings_service being deliberately
cache-free at the read layer (#506, --workers 2 consistency) — the value is
re-read from the DB every call, so a wrong db binding surfaces immediately.

Not yet proven: which file does the priming import. Pairwise runs of
test_1081_pull_mode, test_1069_voip_call_path_param, and
test_1615_ssh_password_removed ahead of it all pass, so the chain is longer
than two files.

Why it matters

CI pins three seeds (12345, 67890, 99999 in backend-unit-test.yml), so
whether this is caught depends on whether a pinned seed happens to order the
polluter ahead of this file. That is a latent source of unexplained red on
unrelated PRs, and of false green — the failure mode #1958 and #1895 are also
about.

Suggested fix

Smallest correct change: add services.settings_service (and audit for sibling
from database import db importers reachable from these tests) to
_STUBBED_MODULE_NAMES in tests/unit/test_1081_physical_meter.py, so the
snapshot/restore fixture covers it.

Worth considering more broadly, since this is the third instance of the class:
the from database import db object-binding idiom is what makes module eviction
load-bearing at all. A test-visible accessor (database.get_db()) would make
these files immune rather than each having to enumerate its own eviction set.

Repro

git worktree add ~/wt-base --detach origin/dev
cd ~/wt-base/src/backend
python -m pytest ../../tests/unit -q -p no:warnings -x --timeout=120

Re-run until an unlucky seed lands, or bisect with --randomly-seed=<n> to pin one.

Activity

  1. added
    priority-p2
    type-bug
    theme-devex
    on Aug 6, 2026
  2. obasilakis commented on Aug 6, 2026

    @obasilakis
    ContributorAuthor

    Data point: seed 12345 does not reproduce

    Full unit suite on clean origin/dev @ 47f8843b with the ordering pinned to one of CI's three seeds:

    $ python -m pytest ../../tests/unit -q --randomly-seed=12345 --timeout=120
    7801 passed, 18 skipped in 433.87s
    

    Fully green — test_available_floors_at_zero_over_ceiling passes at this seed.

    That is consistent with the analysis above rather than a contradiction: the failure needs the polluting file ordered ahead of test_1081_physical_meter.py, and seed 12345 happens not to do that. It also means a green pytest (…, seed 12345) job is not evidence this bug is absent — worth knowing before anyone closes it on a clean CI run.

    Unseeded runs (fresh random seed each time) reproduced it twice on clean dev, halting at the same test after different pass counts (2657 and 3821).

    A second ordering-dependent cluster showed up in the same sweep and is very likely the same root class — test_subscription_auto_switch_pingpong.py::TestHotReloadSwitch errored 7/7 under one random ordering, and passes 26/26 in isolation and at seed 12345. Not filed separately; noting it here since a fix to the from database import db binding idiom would likely cover both.

  3. added
    status-ready
    complexity-low
    on Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p2status-readytheme-devextype-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions