Compare commits

...
2 changed files with 62 additions and 27 deletions

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
name: loop control hunt
on:
push:
branches: [hunt/loop-control-on-old-watcher]
env:
GITHUB_TOKEN: ${{ github.token }}
concurrency:
group: loop-control-hunt
cancel-in-progress: true
jobs:
hunt:
strategy:
fail-fast: false
max-parallel: 2
matrix:
os: [windows-2022, ubuntu-24.04]
runs-on: ${{ matrix.os }}
timeout-minutes: 30
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
with: { fetch-depth: 1 }
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
with: { python-version: '3.11' }
- name: Install
run: |
python -m pip install --upgrade pip
python -m pip install ".[dev]"
if [ "$RUNNER_OS" = "Linux" ]; then sudo apt-get update -q && sudo apt-get install -y -q xvfb; fi
- name: Fetch our published firefox binary
run: echo "INVPW_BINARY_PATH=$(python -m invisible_playwright fetch | tail -1)" >> "$GITHUB_ENV"
- name: The loop test with the OLD watcher, three times
run: |
set +e
for i in 1 2 3; do
python -m pytest tests/test_closing_one_of_four_browsers_does_not_hold_the_loop_e2e.py -m e2e -o addopts='' -q -p no:cacheprovider -rA 2>&1 | grep -E "PASSED|FAILED|charged|held" | cut -c1-240
done
- name: Close every Firefox left
if: always()
run: |
if [ "$RUNNER_OS" = "Linux" ]; then pkill -9 -f firefox || true; else taskkill //F //IM firefox.exe //T 2>/dev/null || true; fi
@@ -60,8 +60,8 @@ _CLIENT = textwrap.dedent("""
async def sentinel(self):
last = time.monotonic()
while not self.stop.is_set():
await asyncio.sleep(self.pause_once or 0.005)
self.pause_once = 0
wait, self.pause_once = self.pause_once or 0.005, 0
await asyncio.sleep(wait)
now = time.monotonic()
self.gaps.append(now - last)
last = self.beat[0] = now
@@ -87,24 +87,13 @@ _CLIENT = textwrap.dedent("""
for i, f in frames.items() if i != main_id and i != threading.get_ident()}}
def watcher(self):
# Only the part of a lateness during which the loop's thread was RUNNING counts. A loop the machine did not schedule waits in
# the selector and then, once woken, runs the callbacks that queued meanwhile (the pingers' replies): counting the whole
# lateness at that moment charged the machine's 90 ms to whoever ran first (candidate 43727128285, Windows: 94 ms, the main
# thread in `self._selector.select`, no blocking call on it). `busy_since` is when the thread last left the selector.
looked, busy_since = 0, None
looked = 0
while not self.over.is_set():
time.sleep(0.003)
now = time.monotonic()
late = now - self.beat[0]
late = time.monotonic() - self.beat[0]
level = int(late / 0.03)
if self.idle(sys._current_frames().get(main_id)):
busy_since = None
else:
if busy_since is None:
busy_since = now
held = now - max(self.beat[0], busy_since)
if held > 0.03:
self.held = max(self.held, held)
if late > 0.03 and not self.idle(sys._current_frames().get(main_id)):
self.held = max(self.held, late)
if level > looked:
self.holes.append({"late_ms": round(late * 1000), **self.stacks()})
looked = level
@@ -170,21 +159,22 @@ _CLIENT = textwrap.dedent("""
await asyncio.sleep(0.05)
await control.finish()
# The second control: a loop that is LATE without being held. The sentinel's next wake is 150 ms away, so the loop waits in its
# selector (as when the machine does not schedule it), and 130 ms in it runs 15 ms of a callback that was due then, while the
# sentinel is still 125 ms late. Only those 15 ms are the loop's thread at work: the lateness before them must not count.
# The second control: a loop that is LATE without being held. The sentinel's next wake is 300 ms away, so the loop waits in its
# selector (as when the machine does not schedule it), and 150 ms in it runs 45 ms of a callback that was due then, while the
# sentinel is still about 145 ms late. Only those 45 ms are the loop's thread at work (45, not less: a watcher on Windows sleeps
# in 15.6 ms ticks and would not see a shorter one): the 145 ms before them must not count.
unscheduled = Instrument()
await unscheduled.start()
await asyncio.sleep(0.05)
unscheduled.pause_once = 0.15
unscheduled.pause_once = 0.3
loop = asyncio.get_running_loop()
def burst():
end = time.monotonic() + 0.015
end = time.monotonic() + 0.045
while time.monotonic() < end:
pass
loop.call_later(0.13, burst)
await asyncio.sleep(0.4)
loop.call_later(0.15, burst)
await asyncio.sleep(0.5)
await unscheduled.finish()
# A slow disk: removing a profile takes 150 ms more (a scanner of files at work, a spinning disk, a loaded runner). On a fast
@@ -291,12 +281,14 @@ def test_the_instrument_sees_a_loop_that_is_held(report):
@pytest.mark.wall_clock
def test_the_instrument_does_not_charge_a_loop_the_machine_did_not_schedule(report):
"""The second control: a loop that waited 130 ms in its selector and then ran a 15 ms callback was not held by anybody.
"""The second control: a loop that waited 150 ms in its selector and then ran a 45 ms callback was held for those 45 ms, not 195.
Charging it the whole lateness made a loaded Windows runner fail the bound below with 94 ms and nothing on the loop's thread
(candidate 43727128285)."""
unscheduled = report["unscheduled"]
assert unscheduled["gap"] > 0.12, "the sentinel was not late: %r" % unscheduled
assert unscheduled["held"] < 0.03, "a loop late by the machine, not by a call, was charged %.0f ms" % (unscheduled["held"] * 1000)
assert unscheduled["gap"] > 0.25, "the sentinel was not late: %r" % unscheduled
assert unscheduled["held"] < 0.09, (
"a loop late by the machine for ~145 ms, then running a 45 ms callback, was charged %.0f ms: only the 45 ms the thread ran may count"
% (unscheduled["held"] * 1000))
@pytest.mark.wall_clock